transformers_js_py
from transformers_js import import_transformers_js, as_url
import gradio as gr
transformers = await import_transformers_js()
pipeline = transformers.pipeline
pipe = await pipeline('zero-shot-image-classification')
async def classify(image, classes):
if not image:
return {}
classes = [x for c in classes.split(",") if (x := c.strip())]
if not classes:
return {}
data = await pipe(as_url(image), classes)
result = {item['label']: round(item['score'], 2) for item in data}
return result
demo = gr.Interface(
classify,
[
gr.Image(label="Input image", sources=["webcam"], type="filepath", streaming=True),
gr.Textbox(label="Classes separated by commas")
],
gr.Label(),
live=True
)
demo.launch()