profplate commited on
Commit
022d9b4
·
verified ·
1 Parent(s): a116c51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +174 -20
app.py CHANGED
@@ -4,33 +4,187 @@ from transformers import pipeline
4
  # Load a sentiment analysis model (works on free CPU)
5
  analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
6
 
7
- def check_mood(text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  if not text or not text.strip():
9
- return "Paste some text above first!"
10
 
11
  result = analyzer(text)[0]
12
  label = result["label"]
13
  score = result["score"]
14
 
 
15
  if label == "POSITIVE":
16
- emoji = "😊" if score > 0.9 else "🙂"
 
 
 
 
 
 
 
 
 
 
 
 
17
  else:
18
- emoji = "😢" if score > 0.9 else "😐"
19
-
20
- return f"{emoji} {label}\n\nConfidence: {score:.0%}"
21
-
22
- demo = gr.Interface(
23
- fn=check_mood,
24
- inputs=gr.Textbox(lines=8, placeholder="Paste any text here — a song lyric, a diary entry, a text from a friend..."),
25
- outputs=gr.Textbox(label="Mood Reading"),
26
- title="Mood Meter",
27
- description="Paste any text and this AI will tell you whether it feels POSITIVE or NEGATIVE — and how confident it is. Does the model agree with how YOU feel about the text?",
28
- examples=[
29
- ["I can't believe how lucky I am to have friends like you. Every day feels like an adventure and I wouldn't trade it for anything in the world."],
30
- ["I don't know why I even bother anymore. Nothing I do seems to matter and nobody notices when I try my hardest."],
31
- ["Dear future me, I hope you figured it out. I hope you're not still lying awake at 2am wondering if you made the right choice."],
32
- ["Walking into school on the first day felt like stepping onto another planet. Everyone already knew each other and I just stood there holding my backpack straps."],
33
- ],
34
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  demo.launch()
 
4
  # Load a sentiment analysis model (works on free CPU)
5
  analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
6
 
7
+ # --- Visual Mood Gauge using gr.HTML ---
8
+
9
+ GAUGE_HTML = """
10
+ <div class="mood-gauge">
11
+ <div class="gauge-title">Mood Reading</div>
12
+ <div class="gauge-track">
13
+ <div class="gauge-fill" style="width: ${value.percent}%; background: ${value.color};"></div>
14
+ </div>
15
+ <div class="gauge-labels">
16
+ <span class="label-neg">NEGATIVE</span>
17
+ <span class="label-pos">POSITIVE</span>
18
+ </div>
19
+ <div class="gauge-result" style="color: ${value.color};">
20
+ <span class="gauge-emoji">${value.emoji}</span>
21
+ <span class="gauge-label">${value.label}</span>
22
+ </div>
23
+ <div class="gauge-confidence">${value.confidence}</div>
24
+ <div class="gauge-hint">${value.hint}</div>
25
+ </div>
26
+ """
27
+
28
+ GAUGE_CSS = """
29
+ .mood-gauge {
30
+ font-family: 'Segoe UI', system-ui, sans-serif;
31
+ max-width: 480px;
32
+ margin: 0 auto;
33
+ padding: 28px 24px;
34
+ text-align: center;
35
+ }
36
+ .gauge-title {
37
+ font-size: 13px;
38
+ text-transform: uppercase;
39
+ letter-spacing: 2px;
40
+ color: #888;
41
+ margin-bottom: 20px;
42
+ }
43
+ .gauge-track {
44
+ height: 28px;
45
+ background: #e8e8e8;
46
+ border-radius: 14px;
47
+ overflow: hidden;
48
+ position: relative;
49
+ box-shadow: inset 0 2px 4px rgba(0,0,0,0.1);
50
+ }
51
+ .gauge-fill {
52
+ height: 100%;
53
+ border-radius: 14px;
54
+ transition: width 0.8s ease, background 0.8s ease;
55
+ box-shadow: 0 2px 8px rgba(0,0,0,0.15);
56
+ }
57
+ .gauge-labels {
58
+ display: flex;
59
+ justify-content: space-between;
60
+ margin-top: 6px;
61
+ font-size: 10px;
62
+ letter-spacing: 1px;
63
+ color: #aaa;
64
+ }
65
+ .gauge-result {
66
+ margin-top: 24px;
67
+ display: flex;
68
+ align-items: center;
69
+ justify-content: center;
70
+ gap: 12px;
71
+ }
72
+ .gauge-emoji {
73
+ font-size: 48px;
74
+ line-height: 1;
75
+ }
76
+ .gauge-label {
77
+ font-size: 28px;
78
+ font-weight: 700;
79
+ letter-spacing: 1px;
80
+ }
81
+ .gauge-confidence {
82
+ margin-top: 8px;
83
+ font-size: 18px;
84
+ color: #666;
85
+ }
86
+ .gauge-hint {
87
+ margin-top: 16px;
88
+ font-size: 13px;
89
+ color: #999;
90
+ font-style: italic;
91
+ min-height: 20px;
92
+ }
93
+ """
94
+
95
+ GAUGE_JS = """
96
+ // Animate the fill bar on load
97
+ const fill = element.querySelector('.gauge-fill');
98
+ if (fill) {
99
+ const target = fill.style.width;
100
+ fill.style.width = '0%';
101
+ requestAnimationFrame(() => {
102
+ requestAnimationFrame(() => {
103
+ fill.style.width = target;
104
+ });
105
+ });
106
+ }
107
+ """
108
+
109
+ # Default state (before any analysis)
110
+ DEFAULT_STATE = {
111
+ "percent": 50,
112
+ "color": "#ccc",
113
+ "emoji": "...",
114
+ "label": "",
115
+ "confidence": "Paste some text and click Submit",
116
+ "hint": "",
117
+ }
118
+
119
+ def check_mood(text, gauge_state):
120
  if not text or not text.strip():
121
+ return DEFAULT_STATE
122
 
123
  result = analyzer(text)[0]
124
  label = result["label"]
125
  score = result["score"]
126
 
127
+ # Map to a 0-100 gauge: 0 = fully negative, 100 = fully positive
128
  if label == "POSITIVE":
129
+ percent = 50 + (score * 50)
130
+ color = "#2ecc71" if score > 0.9 else "#82c91e"
131
+ emoji = "\U0001f60a" if score > 0.9 else "\U0001f642"
132
+ else:
133
+ percent = 50 - (score * 50)
134
+ color = "#e74c3c" if score > 0.9 else "#e67e22"
135
+ emoji = "\U0001f622" if score > 0.9 else "\U0001f610"
136
+
137
+ # Confidence hint for students
138
+ if score > 0.95:
139
+ hint = "The model is very sure about this one."
140
+ elif score > 0.8:
141
+ hint = "Pretty confident, but not 100%. Do you agree?"
142
  else:
143
+ hint = "The model isn't sure. This text might be ambiguous!"
144
+
145
+ return {
146
+ "percent": round(percent),
147
+ "color": color,
148
+ "emoji": emoji,
149
+ "label": label,
150
+ "confidence": f"Confidence: {score:.0%}",
151
+ "hint": hint,
152
+ }
153
+
154
+ # --- Build the app with gr.Blocks ---
155
+
156
+ with gr.Blocks(title="Mood Meter") as demo:
157
+ gr.Markdown("# Mood Meter")
158
+ gr.Markdown(
159
+ "Paste any text and this AI will tell you whether it feels **POSITIVE** or **NEGATIVE** "
160
+ "— and how confident it is. Does the model agree with how YOU feel about the text?"
161
+ )
162
+
163
+ text_input = gr.Textbox(
164
+ lines=6,
165
+ placeholder="Paste any text here — a song lyric, a diary entry, a text from a friend...",
166
+ label="Your text",
167
+ )
168
+
169
+ submit_btn = gr.Button("Check the Mood", variant="primary")
170
+
171
+ gauge = gr.HTML(
172
+ value=DEFAULT_STATE,
173
+ html_template=GAUGE_HTML,
174
+ css_template=GAUGE_CSS,
175
+ js_on_load=GAUGE_JS,
176
+ )
177
+
178
+ submit_btn.click(fn=check_mood, inputs=[text_input, gauge], outputs=gauge)
179
+
180
+ gr.Examples(
181
+ examples=[
182
+ "I can't believe how lucky I am to have friends like you. Every day feels like an adventure and I wouldn't trade it for anything in the world.",
183
+ "I don't know why I even bother anymore. Nothing I do seems to matter and nobody notices when I try my hardest.",
184
+ "Dear future me, I hope you figured it out. I hope you're not still lying awake at 2am wondering if you made the right choice.",
185
+ "Walking into school on the first day felt like stepping onto another planet. Everyone already knew each other and I just stood there holding my backpack straps.",
186
+ ],
187
+ inputs=[text_input],
188
+ )
189
 
190
  demo.launch()