Spaces:
Sleeping

File size: 942 Bytes
e4c89da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import gradio as gr
from transformers import pipeline


# Return scores for both labels so Gradio can show a nice bar chart
classifier = pipeline("sentiment-analysis", return_all_scores=True)

def analyze(text: str):
    if not text.strip():
        return {}
    scores = classifier(text)[0]  # [{'label': 'NEGATIVE', 'score': ...}, {'label': 'POSITIVE', 'score': ...}]
    return {item["label"]: float(item["score"]) for item in scores}

demo = gr.Interface(
    fn=analyze,
    inputs=gr.Textbox(lines=4, label="Enter text"),
    outputs=gr.Label(num_top_classes=2, label="Sentiment"),
    title="Hugging Face Sentiment (Transformers + Gradio)",
    description="Type text and get POSITIVE/NEGATIVE scores.",
    examples=[
        ["I've been waiting for a Hugging Face course my whole life."],
        ["This is the worst thing ever."],
        ["Meh, it's okay—nothing special."]
    ],
)

if __name__ == "__main__":
    demo.launch()