workshop-demo / app.py
lewtun's picture
lewtun HF Staff
Create app.py
e760e21
raw
history blame
904 Bytes
import gradio as gr
from transformers import pipeline
pipe = pipeline("text-classification", model="lewtun/xlm-roberta-base-finetuned-marc-500-samples")
def predict(text):
label2star = {"LABEL_0": "⭐", "LABEL_1": "⭐⭐", "LABEL_2": "⭐⭐⭐", "LABEL_3": "⭐⭐⭐⭐", "LABEL_4": "⭐⭐⭐⭐⭐"}
preds = pipe(text)[0]
return label2star[preds["label"]], round(preds["score"], 5)
gradio_ui = gr.Interface(
fn=predict,
title="Review analysis",
description="Enter some review text about an Amazon product and check what the model predicts for it's star rating.",
inputs=[
gr.inputs.Textbox(lines=5, label="Paste some text here"),
],
outputs=[
gr.outputs.Textbox(label="Label"),
gr.outputs.Textbox(label="Score"),
],
examples=[
["I love this book!", "The Hunger Games is the worst book ever"],
],
)
gradio_ui.launch()