Files changed (1) hide show
  1. Appy.py +139 -0
Appy.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from textblob import TextBlob
2
+ import gradio as gr
3
+ import matplotlib
4
+ matplotlib.use('Agg')
5
+ import matplotlib.pyplot as plt
6
+
7
+ def final_sentiment_app(text):
8
+ if not text.strip():
9
+ return "⚠️ Please enter some text!", None
10
+
11
+ sentences = [s.strip() for s in text.replace('!','!|').replace('?','?|').replace('.','.|').split('|') if s.strip()]
12
+
13
+ if len(sentences) == 0:
14
+ return "⚠️ No sentences found!", None
15
+
16
+ scores = []
17
+ colors = []
18
+ sentence_results = []
19
+ pos_count = neg_count = neu_count = 0
20
+ total_score = 0
21
+
22
+ for i, sentence in enumerate(sentences):
23
+ blob = TextBlob(sentence)
24
+ score = round(blob.sentiment.polarity, 2)
25
+ subjectivity = round(blob.sentiment.subjectivity, 2)
26
+ scores.append(score)
27
+ total_score += score
28
+
29
+ if score >= 0.5:
30
+ label = "🀩 Very Positive"
31
+ colors.append('#00e676')
32
+ pos_count += 1
33
+ elif score > 0.1:
34
+ label = "😊 Positive"
35
+ colors.append('#00c853')
36
+ pos_count += 1
37
+ elif score <= -0.5:
38
+ label = "😑 Very Negative"
39
+ colors.append('#ff1744')
40
+ neg_count += 1
41
+ elif score < -0.1:
42
+ label = "😞 Negative"
43
+ colors.append('#d50000')
44
+ neg_count += 1
45
+ else:
46
+ label = "😐 Neutral"
47
+ colors.append('#aa00ff')
48
+ neu_count += 1
49
+
50
+ sentence_results.append(
51
+ f"S{i+1} {label} | Score: {score} | Subjectivity: {subjectivity}\n"
52
+ f" β†’ \"{sentence}\""
53
+ )
54
+
55
+ avg = round(total_score / len(sentences), 2)
56
+
57
+ if avg >= 0.5: overall = "🀩 Very Positive"
58
+ elif avg > 0.1: overall = "😊 Positive"
59
+ elif avg <= -0.5: overall = "😑 Very Negative"
60
+ elif avg < -0.1: overall = "😞 Negative"
61
+ else: overall = "😐 Neutral"
62
+
63
+ fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 4))
64
+ fig.patch.set_facecolor('#0d0d1a')
65
+ fig.suptitle('Sentiment Analysis Report', color='white', fontsize=14, fontweight='bold')
66
+
67
+ ax1.set_facecolor('#1a1a2e')
68
+ x = range(len(sentences))
69
+ bars = ax1.bar(x, scores, color=colors, edgecolor='white', linewidth=0.5, width=0.5)
70
+ ax1.axhline(y=0, color='white', linewidth=1, linestyle='--', alpha=0.5)
71
+ ax1.set_title('Score Per Sentence', color='white', fontsize=11)
72
+ ax1.set_xticks(x)
73
+ ax1.set_xticklabels([f'S{i+1}' for i in x], color='white')
74
+ ax1.set_ylabel('Polarity (-1 to +1)', color='white')
75
+ ax1.set_ylim(-1.2, 1.2)
76
+ ax1.tick_params(colors='white')
77
+ for bar, score in zip(bars, scores):
78
+ ypos = bar.get_height()+0.05 if score >= 0 else bar.get_height()-0.12
79
+ ax1.text(bar.get_x()+bar.get_width()/2, ypos,
80
+ str(score), ha='center', color='white', fontsize=9, fontweight='bold')
81
+
82
+ ax2.set_facecolor('#1a1a2e')
83
+ pie_data = [(pos_count,'Positive\n','#00c853'),
84
+ (neg_count,'Negative\n','#ff1744'),
85
+ (neu_count,'Neutral\n','#aa00ff')]
86
+ pie_data = [(v,l,c) for v,l,c in pie_data if v > 0]
87
+ ax2.pie(
88
+ [d[0] for d in pie_data],
89
+ labels=[f"{d[1]}({d[0]})" for d in pie_data],
90
+ colors=[d[2] for d in pie_data],
91
+ autopct='%1.0f%%',
92
+ textprops={'color':'white','fontsize':10},
93
+ wedgeprops={'edgecolor':'white','linewidth':1.2},
94
+ startangle=90
95
+ )
96
+ ax2.set_title('Sentiment Distribution', color='white', fontsize=11)
97
+ plt.tight_layout()
98
+
99
+ report = "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
100
+ report += "πŸ“‹ SENTENCE BREAKDOWN\n"
101
+ report += "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
102
+ report += "\n".join(sentence_results)
103
+ report += "\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
104
+ report += "πŸ“Š FINAL SUMMARY\n"
105
+ report += "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n"
106
+ report += f"🎯 Overall Sentiment : {overall}\n"
107
+ report += f"πŸ“ˆ Average Score : {avg}\n"
108
+ report += f"βœ… Positive sentences : {pos_count}\n"
109
+ report += f"❌ Negative sentences : {neg_count}\n"
110
+ report += f"βž– Neutral sentences : {neu_count}\n"
111
+ report += f"πŸ“ Total sentences : {len(sentences)}\n"
112
+ report += f"πŸ“ Total words : {len(text.split())}\n"
113
+ report += "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
114
+
115
+ return report, fig
116
+
117
+ app = gr.Interface(
118
+ fn=final_sentiment_app,
119
+ inputs=gr.Textbox(
120
+ placeholder="Paste a paragraph, product review, tweet or any text...",
121
+ label="πŸ“ Enter Your Text",
122
+ lines=6
123
+ ),
124
+ outputs=[
125
+ gr.Textbox(label="πŸ“‹ Analysis Report", lines=18),
126
+ gr.Plot(label="πŸ“ˆ Visual Charts")
127
+ ],
128
+ title="🧠 Sentiment Analyzer Pro",
129
+ description="✨ AI-powered sentiment detection | Sentence-by-sentence breakdown | Visual charts | Built with Python & NLP",
130
+ examples=[
131
+ ["I love this college! The canteen food is terrible. Classes are okay. Teachers are amazing!"],
132
+ ["This phone is fantastic! Battery life is poor. Camera quality is outstanding. Delivery was late."],
133
+ ["I am so happy today! Work was stressful. But my friends made it better. Overall a good day!"],
134
+ ["The movie was boring. Acting was terrible. But the music was absolutely amazing!"]
135
+ ],
136
+ theme="soft"
137
+ )
138
+
139
+ app.launch()