Package: transformers-js-py
Transformers.js.py 🤗UseTransformers.jsonPyodideand Pyodide-based frameworks such asJupyterLite,stlite (Streamlit),Shinylive (Shiny for Python),PyScript,HoloViz Panel, and so on.The originalTransformerscan't be used in a browser environment.Transformers.jsis a JavaScript version of Transformers that can be installed on browsers, but we can't use it from Pyodide.
This package isa thin wrapper of Transformers.js to proxy its API to Pyodide.APIThe API is more like Transformers.js than the original Transformers.Transformers.jsTransformers.js.pyimport{pipeline}from'@xenova/transformers';// Allocate a pipeline for sentiment-analysisletpipe=awaitpipeline('sentiment-analysis');letout=awaitpipe('I love transformers!');// [{'label': 'POSITIVE', 'score': 0.999817686}]fromtransformers_js_pyimportimport_transformers_jstransformers=awaitimport_transformers_js()pipeline=transformers.pipeline# Allocate a pipeline for sentiment-analysispipe=awaitpipeline('sentiment-analysis')out=awaitpipe('I love transformers!')# [{'label': 'POSITIVE', 'score': 0.999817686}]See theTransformers.js documentfor available features.Special Case:as_url()Certain methods of Transformers.js accept a URL as an input. However, when using Transformers.js.py on Pyodide, there may be instances where we want to pass a local file path from the virtual file system, rather than a URL. In such scenarios, theas_url()function can be used to convert a local file path into a URL.# Examplefromtransformers_jsimportimport_transformers_js,as_urltransformers=awaitimport_transformers_js()pipeline=transformers.pipelinepipe=awaitpipeline('image-classification')local_image_path="/path/to/image.jpg"input_url=as_url(local_image_path)# Converts a local file path into a URL that can be passed to `pipe()`result=awaitpipe(input_url)ExamplesJupyterLite👉Try this code snippet onhttps://jupyter.org/try-jupyter/lab/index.html%pipinstalltransformers_js_pyfromtransformers_js_pyimportimport_transformers_jstransformers=awaitimport_transformers_js()pipeline=transformers.pipelinepipe=awaitpipeline('sentiment-analysis')out=awaitpipe('I love transformers!')print(out)stlite (Serverless Streamlit)👉 Online Demo: try out this code online.importstreamlitasstfromtransformers_js_pyimportimport_transformers_jsst.title("Sentiment analysis")text=st.text_input("Input some text","I love transformers!")iftext:withst.spinner():transformers=awaitimport_transformers_js()pipeline=transformers.pipelineif"pipe"notinst.session_state:st.session_state["pipe"]=awaitpipeline('sentiment-analysis')pipe=st.session_state["pipe"]out=awaitpipe(text)st.write(out)Gradio-liteSave the following code as an HTML file and open it in a browser.<!DOCTYPE html><htmllang="en"><head><scripttype="module"crossoriginsrc="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"></script><linkrel="stylesheet"href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css"/><title>Transformers.js with Gradio-lite</title><style>html,body{margin:0;padding:0;height:100%;width:100%;}gradio-lite{height:100%;width:100%;}</style></head><body><gradio-lite><gradio-requirements>transformers_js_py</gradio-requirements><gradio-filename="app.py"entrypoint>import gradio as gr
from transformers_js_py import import_transformers_js

transformers = await import_transformers_js()
pipeline = transformers.pipeline

pipe = await pipeline('sentiment-analysis')

async def process(text):
    return await pipe(text)

demo = gr.Interface(fn=process, inputs="text", outputs="json")

demo.launch()</gradio-file></gradio-lite></body></html>👉 Online demoFor more details about Gradio-lite, please readGradio-Lite: Serverless Gradio Running Entirely in Your Browser (huggingface.co).Shinylive👉 Online demo: try out this code online.fromshinyimportApp,render,uifromtransformers_js_pyimportimport_transformers_jsapp_ui=ui.page_fluid(ui.input_text("text","Text input",placeholder="Enter text"),ui.output_text_verbatim("txt"),)defserver(input,output,session):@output@render.textasyncdeftxt():ifnotinput.text():return""transformers=awaitimport_transformers_js()pipeline=transformers.pipelinepipe=awaitpipeline('sentiment-analysis')out=awaitpipe(input.text())returnstr(out)app=App(app_ui,server,debug=True)PyScript👉Try this code snippet onhttps://pyscript.com/<html><head><linkrel="stylesheet"href="https://pyscript.net/latest/pyscript.css"/><scriptdefersrc="https://pyscript.net/latest/pyscript.js"></script></head><body><inputtype="text"value=""id="text-input"/><buttonpy-click="run()"id="run-button">Run</button><py-config>packages = ["transformers-js-py"]</py-config><py-script>import asyncio
        from transformers_js_py 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>PanelWithHoloViz Panelyou develop your app on your laptop and convert it toPyodideorPyScriptby runningpanel convert.Install the requirementspipinstallpaneltransformers_js_pyCreate theapp.pyfile in your favorite editor or IDE.importpanelaspnpn.extension(sizing_mode="stretch_width",design="material")@pn.cacheasyncdef_get_pipeline(model="sentiment-analysis"):fromtransformers_js_pyimportimport_transformers_jstransformers=awaitimport_transformers_js()returnawaittransformers.pipeline(model)text_input=pn.widgets.TextInput(placeholder="Send a message",name="Message")button=pn.widgets.Button(name="Send",icon="send",align="end",button_type="primary")@pn.depends(text_input,button)asyncdef_response(text,event):ifnottext:return{}pipe=await_get_pipeline()returnawaitpipe(text)pn.Column(text_input,button,pn.pane.JSON(_response,depth=2)).servable()Convert the app toPyodide. For more options likehot reloadcheck out thePanel Convertguide.panelconvertapp.py--topyodide-worker--outpyodide--requirementstransformers_js_pyNow serve the apppython-mhttp.server-dpyodideFinally you can try out the app by openinglocalhost:8000/app.htmlPanel Chat App ExampleYou can also usetransformers_js_pywithPanels Chat Components.importpanelaspnMODEL="sentiment-analysis"pn.chat.ChatMessage.default_avatars["hugging face"]="🤗"pn.extension(design="material")@pn.cacheasyncdef_get_pipeline(model):fromtransformers_js_pyimportimport_transformers_jstransformers=awaitimport_transformers_js()returnawaittransformers.pipeline(model)asyncdefcallback(contents:str,user:str,instance:pn.chat.ChatInterface):pipe=await_get_pipeline(MODEL)response=awaitpipe(contents)label,score=response[0]["label"],round(response[0]["score"],2)returnf"""I feel a{label}vibe here (score:{score})"""welcome_message=pn.chat.ChatMessage(f"I'm a Hugging Face Transformers `{MODEL}` model.\n\nPlease *send a message*!",user="Hugging Face",)pn.chat.ChatInterface(welcome_message,placeholder_text="Loading the model ...",callback=callback,callback_user="Hugging Face",).servable()For more chat examples seePanel Chat Examples.