File size: 1,358 Bytes
2bcc5f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load models
sentiment = pipeline(
    "sentiment-analysis",
    model="cardiffnlp/twitter-roberta-base-sentiment-latest"
)

classifier = pipeline(
    "zero-shot-classification",
    model="facebook/bart-large-mnli"
)

labels = [
    "Sports",
    "Education",
    "Technology",
    "Politics",
    "Finance",
    "Health",
    "Entertainment",
    "Business",
    "Travel",
    "Food"
]

def analyze(text):
    if not text.strip():
        return "Please enter some text.", "", "", ""

    # Sentiment
    s = sentiment(text)[0]
    sent = s["label"]
    score = round(s["score"] * 100, 2)

    # Category
    c = classifier(text, labels)
    category = c["labels"][0]

    explanation = f"The sentence is classified as '{sent}' and belongs to the '{category}' category."

    return sent, category, f"{score}%", explanation


demo = gr.Interface(
    fn=analyze,
    inputs=gr.Textbox(
        lines=4,
        placeholder="Enter a sentence or paragraph..."
    ),
    outputs=[
        gr.Text(label="Sentiment"),
        gr.Text(label="Category"),
        gr.Text(label="Confidence"),
        gr.Textbox(label="Explanation")
    ],
    title="🧠 AI Sentiment & Topic Analyzer",
    description="Analyze text to determine its sentiment and topic using Hugging Face Transformers."
)

demo.launch()