No project description provided
Project description
Transformers.js.py
Use Transformers.js on Pyodide and Pyodide-based frameworks such as stlite (Streamlit), PyScript, and so on.
The original Transformers can't be used on a browser environment. Transformers.js is a JavaScript version of Transformers installable on browsers, but we can't use it from Pyodide. This package is a thin wrapper of Transformers.js to proxy its API to Pyodide.
API
The API is more like Transformers.js than the original Transformers.
Transformers.js | Transformers.js.py |
---|---|
import { pipeline } from '@xenova/transformers';
// Allocate a pipeline for sentiment-analysis
let pipe = await pipeline('sentiment-analysis');
let out = await pipe('I love transformers!');
// [{'label': 'POSITIVE', 'score': 0.999817686}]
|
from transformers_js import import_transformers_js
transformers = await import_transformers_js()
pipeline = transformers.pipeline
# Allocate a pipeline for sentiment-analysis
pipe = await pipeline('sentiment-analysis')
out = await pipe(text)
# [{'label': 'POSITIVE', 'score': 0.999817686}]
|
Examples
stlite
import streamlit as st
from transformers_js import import_transformers_js
st.title("Sentiment analysis")
text = st.text_input("Input some text")
if text:
with st.spinner():
transformers = await import_transformers_js()
pipeline = transformers.pipeline
if "pipe" not in st.session_state:
st.session_state["pipe"] = await pipeline('sentiment-analysis')
pipe = st.session_state["pipe"]
out = await pipe(text)
st.write(out)
PyScript
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<input type="text" value="" id="text-input" />
<button py-click="run()" id="run-button">Run</button>
<py-config>
packages = ["transformers-js-py"]
</py-config>
<py-script>
import asyncio
from transformers_js import import_transformers_js
text_input = Element("text-input")
async def main(input_data):
transformers = await import_transformers_js()
pipeline = transformers.pipeline
pipe = await pipeline('sentiment-analysis')
out = await pipe(input_data)
print(out)
def run():
print("Start")
input_data = text_input.value
if input_data.strip() == "":
print("No data input.")
return
future = asyncio.ensure_future(main(input_data))
</py-script>
</body>
</html>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for transformers_js_py-0.1.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8b0693de92aae67408b7ea40f8d0cafd79b2eb608b77353cca0cd8464695391 |
|
MD5 | 8dae842fe5f128e3f7597851c679bea6 |
|
BLAKE2b-256 | 72e95862aa14979552a1a700e30b89d468c35c04c5f56d26252ca1fb661245e2 |