File size: 2,436 Bytes
6b71e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import gradio as gr
from transformers import pipeline
classifier = pipeline(
    "sentiment-analysis",
    model="Umranz/distilbert-yelp-sentiment",
    tokenizer="Umranz/distilbert-yelp-sentiment"
)

def predict_single(text):
    if not text.strip():
        return "⚠️ Please enter some text."
    result = classifier(text)[0]
    label  = result["label"]
    score  = result["score"] * 100
    emoji  = "😊" if label == "POSITIVE" else "😞"
    return f"{emoji} {label}  ({score:.1f}% confidence)"

def predict_batch(text):
    if not text.strip():
        return "⚠️ Please enter at least one review."
    lines   = [l.strip() for l in text.strip().split("\n") if l.strip()]
    results = classifier(lines)
    output  = ""
    for line, res in zip(lines, results):
        label = res["label"]
        score = res["score"] * 100
        emoji = "😊" if label == "POSITIVE" else "😞"
        output += f"{emoji} **{label}** ({score:.1f}%) β€” {line}\n\n"
    return output.strip()

single_tab = gr.Interface(
    fn=predict_single,
    inputs=gr.Textbox(
        lines=4,
        placeholder="e.g. The movie was absolutely fantastic!",
        label="Your Review"
    ),
    outputs=gr.Textbox(label="Sentiment Result"),
    title="🎬 Sentiment Analyzer",
    description="Fine-tuned **DistilBERT** on Yelp reviews. Enter a review to analyze its sentiment.",
    examples=[
        ["This movie was absolutely fantastic! The acting was brilliant."],
        ["Terrible movie. It was boring and a complete waste of time."],
        ["Not bad, actually enjoyed it more than expected."],
        ["Best restaurant I've visited in years, amazing food!"],
        ["Waited 2 hours and the food was cold. Never going back."],
    ],
    cache_examples=False
)
batch_tab = gr.Interface(
    fn=predict_batch,
    inputs=gr.Textbox(
        lines=8,
        placeholder="Enter one review per line:\nGreat food!\nWorst experience ever.",
        label="Reviews (one per line)"
    ),
    outputs=gr.Markdown(label="Results"),
    title="πŸ“‹ Batch Sentiment Analyzer",
    description="Analyze multiple reviews at once β€” one review per line.",
    examples=[
        ["Amazing experience!\nTerrible service.\nNot bad overall.\nAbsolutely loved it!"],
    ],
    cache_examples=False
)

app = gr.TabbedInterface(
    interface_list=[single_tab, batch_tab],
    tab_names=["Single Review", "Batch Reviews"]
)

app.launch()