VethaNarayananG commited on
Commit
2bcc5f9
·
verified ·
1 Parent(s): 7e14015

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load models
5
+ sentiment = pipeline(
6
+ "sentiment-analysis",
7
+ model="cardiffnlp/twitter-roberta-base-sentiment-latest"
8
+ )
9
+
10
+ classifier = pipeline(
11
+ "zero-shot-classification",
12
+ model="facebook/bart-large-mnli"
13
+ )
14
+
15
+ labels = [
16
+ "Sports",
17
+ "Education",
18
+ "Technology",
19
+ "Politics",
20
+ "Finance",
21
+ "Health",
22
+ "Entertainment",
23
+ "Business",
24
+ "Travel",
25
+ "Food"
26
+ ]
27
+
28
+ def analyze(text):
29
+ if not text.strip():
30
+ return "Please enter some text.", "", "", ""
31
+
32
+ # Sentiment
33
+ s = sentiment(text)[0]
34
+ sent = s["label"]
35
+ score = round(s["score"] * 100, 2)
36
+
37
+ # Category
38
+ c = classifier(text, labels)
39
+ category = c["labels"][0]
40
+
41
+ explanation = f"The sentence is classified as '{sent}' and belongs to the '{category}' category."
42
+
43
+ return sent, category, f"{score}%", explanation
44
+
45
+
46
+ demo = gr.Interface(
47
+ fn=analyze,
48
+ inputs=gr.Textbox(
49
+ lines=4,
50
+ placeholder="Enter a sentence or paragraph..."
51
+ ),
52
+ outputs=[
53
+ gr.Text(label="Sentiment"),
54
+ gr.Text(label="Category"),
55
+ gr.Text(label="Confidence"),
56
+ gr.Textbox(label="Explanation")
57
+ ],
58
+ title="🧠 AI Sentiment & Topic Analyzer",
59
+ description="Analyze text to determine its sentiment and topic using Hugging Face Transformers."
60
+ )
61
+
62
+ demo.launch()