File size: 1,253 Bytes
61eff92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
from transformers import pipeline

# Load a tiny, fast model — runs on CPU in seconds
sentiment = pipeline(
    "sentiment-analysis",
    model="distilbert-base-uncased-finetuned-sst-2-english",
)


def analyze(text: str) -> str:
    if not text.strip():
        return "Please enter some text."
    result = sentiment(text)[0]
    label = result["label"].capitalize()
    score = round(result["score"] * 100, 1)
    emoji = "😊" if label == "Positive" else "😞"
    return f"{emoji} **{label}** ({score}% confidence)"


demo = gr.Interface(
    fn=analyze,
    inputs=gr.Textbox(
        label="Your text",
        placeholder="Type anything… e.g. 'I love building AI apps!'",
        lines=3,
    ),
    outputs=gr.Markdown(label="Sentiment"),
    title="🤗 Hello World — AI Sentiment Analysis",
    description=(
        "Type a sentence and the model will tell you whether it sounds "
        "**positive** or **negative**. Powered by DistilBERT on 🤗 Transformers."
    ),
    examples=[
        ["I love learning about machine learning!"],
        ["This assignment is really frustrating."],
        ["The weather is okay today."],
    ],
    cache_examples=False,
)

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