AyusmanSamasi commited on
Commit
50575f2
ยท
verified ยท
1 Parent(s): 80b0c2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -31
app.py CHANGED
@@ -1,24 +1,23 @@
1
  import gradio as gr
2
  import torch
3
- from modeling import BERTMultiLabel
4
  from transformers import AutoTokenizer
5
-
6
 
7
  LABELS = ["anger", "fear", "joy", "sadness", "surprise"]
8
 
9
- # ---- Load tokenizer ----
10
  tokenizer = AutoTokenizer.from_pretrained("./")
 
11
 
12
-
13
- model = BERTMultiLabel(model_name="microsoft/deberta-v3-base", num_labels=len(LABELS))
14
  state = torch.load("pytorch_model.bin", map_location="cpu")
15
  model.load_state_dict(state)
16
  model.eval()
17
 
18
 
 
19
  def predict(text):
20
- if text.strip() == "":
21
- return {"error": "Please enter text"}
22
 
23
  enc = tokenizer(
24
  text,
@@ -30,39 +29,124 @@ def predict(text):
30
 
31
  with torch.no_grad():
32
  logits = model(enc["input_ids"], enc["attention_mask"])
33
- probs = torch.sigmoid(logits).flatten().tolist()
 
 
 
34
 
35
- result = {label: float(f"{p:.4f}") for label, p in zip(LABELS, probs)}
36
- top_label = LABELS[int(torch.argmax(torch.tensor(probs)))]
 
 
 
 
 
37
 
38
  return {
39
- "Predicted Mood": top_label,
40
- "Scores": result
41
  }
42
 
43
 
44
- with gr.Blocks(title="Mood Detector") as app:
45
- gr.Markdown("# ๐Ÿง  DeBERTa Mood Detector")
46
- gr.Markdown("Enter any text to analyze the emotional tone.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  with gr.Row():
49
- # LEFT PANEL
 
50
  with gr.Column(scale=1):
51
- gr.Markdown("""
52
- ## ๐Ÿ“Œ Model Info
53
- - **Architecture:** DeBERTa-v3 Base
54
- - **Task:** Multi-label emotion classification
55
- - **Trained by:** You
56
- - **Dataset:** Custom text emotion dataset
57
- - **Output:** anger, fear, joy, sadness, surprise
58
- """)
59
-
60
- # RIGHT PANEL
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  with gr.Column(scale=2):
62
- user_input = gr.Textbox(label="Enter text here", placeholder="Type something like: 'I feel so happy today!'")
63
- analyze_button = gr.Button("Analyze Mood")
64
- output = gr.JSON(label="Result")
65
 
66
- analyze_button.click(predict, inputs=user_input, outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- app.launch()
 
1
  import gradio as gr
2
  import torch
 
3
  from transformers import AutoTokenizer
4
+ from modeling import BERTMultiLabel
5
 
6
  LABELS = ["anger", "fear", "joy", "sadness", "surprise"]
7
 
8
+ # Load tokenizer and model
9
  tokenizer = AutoTokenizer.from_pretrained("./")
10
+ model = BERTMultiLabel("microsoft/deberta-v3-base", num_labels=len(LABELS))
11
 
 
 
12
  state = torch.load("pytorch_model.bin", map_location="cpu")
13
  model.load_state_dict(state)
14
  model.eval()
15
 
16
 
17
+ # ---------- PREDICTION FUNCTION ----------
18
  def predict(text):
19
+ if not text.strip():
20
+ return {"error": "Please enter text."}
21
 
22
  enc = tokenizer(
23
  text,
 
29
 
30
  with torch.no_grad():
31
  logits = model(enc["input_ids"], enc["attention_mask"])
32
+ probs = torch.sigmoid(logits)[0].tolist()
33
+
34
+ scores = {label: round(p, 4) for label, p in zip(LABELS, probs)}
35
+ main_emotion = LABELS[int(torch.tensor(probs).argmax())]
36
 
37
+ emoji_map = {
38
+ "anger": "๐Ÿ˜ก",
39
+ "fear": "๐Ÿ˜จ",
40
+ "joy": "๐Ÿ˜Š",
41
+ "sadness": "๐Ÿ˜ข",
42
+ "surprise": "๐Ÿ˜ฎ",
43
+ }
44
 
45
  return {
46
+ "Predicted Mood": f"{emoji_map[main_emotion]} {main_emotion.capitalize()}",
47
+ "Scores": scores
48
  }
49
 
50
 
51
+ # ---------- UI LAYOUT ----------
52
+ with gr.Blocks(
53
+ title="Sentiment & Emotion Analyzer",
54
+ theme=gr.themes.Soft(
55
+ primary_hue="indigo",
56
+ secondary_hue="blue",
57
+ radius="lg",
58
+ text_size="lg"
59
+ )
60
+ ) as demo:
61
+
62
+ # ---- HEADER ----
63
+ gr.Markdown(
64
+ """
65
+ <div style="text-align:center; padding: 20px;">
66
+ <h1 style="font-size: 3rem; margin-bottom: 0;">๐ŸŽญ Multi-Label Emotion Detection</h1>
67
+ <p style="font-size:1.2rem; color:#666;">
68
+ Powered by DeBERTa-v3 โ€ข Fine-tuned on IIT Madras Deep Learning Competition Dataset
69
+ </p>
70
+ </div>
71
+ """
72
+ )
73
 
74
  with gr.Row():
75
+
76
+ # ---------- LEFT PANEL (FULL INFO) ----------
77
  with gr.Column(scale=1):
78
+
79
+ gr.Markdown(
80
+ """
81
+ <div style="
82
+ background:white; border-radius:16px; padding:20px;
83
+ box-shadow:0 4px 20px rgba(0,0,0,0.08); margin-bottom:20px;
84
+ ">
85
+ <h2>๐Ÿ“Œ Model Overview</h2>
86
+ <ul style="line-height:1.6;">
87
+ <li><b>Architecture:</b> DeBERTa-v3 Base</li>
88
+ <li><b>Task:</b> Multi-Label Emotion Classification</li>
89
+ <li><b>Labels:</b> anger, fear, joy, sadness, surprise</li>
90
+ <li><b>Training Framework:</b> PyTorch + HuggingFace</li>
91
+ <li><b>Loss:</b> BCEWithLogitsLoss</li>
92
+ <li><b>Optimizer:</b> AdamW (LR = 1e-5)</li>
93
+ </ul>
94
+ </div>
95
+
96
+ <div style="
97
+ background:white; border-radius:16px; padding:20px;
98
+ box-shadow:0 4px 20px rgba(0,0,0,0.08); margin-bottom:20px;
99
+ ">
100
+ <h2>๐Ÿ“Š Dataset Details</h2>
101
+ <p style="line-height:1.6;">
102
+ Dataset used in IIT Madras Deep Learning & GenAI (Sep 2025).
103
+ Multi-label emotion classification with 5 emotions:
104
+ </p>
105
+ <ul>
106
+ <li>๐Ÿ˜  Anger</li>
107
+ <li>๐Ÿ˜จ Fear</li>
108
+ <li>๐Ÿ˜Š Joy</li>
109
+ <li>๐Ÿ˜ข Sadness</li>
110
+ <li>๐Ÿ˜ฒ Surprise</li>
111
+ </ul>
112
+ <p><b>Evaluation Metric:</b> Macro F1-Score</p>
113
+ </div>
114
+
115
+ <div style="
116
+ background:white; border-radius:16px; padding:20px;
117
+ box-shadow:0 4px 20px rgba(0,0,0,0.08);
118
+ ">
119
+ <h2>๐Ÿ† Project Highlights</h2>
120
+ <ul style="line-height:1.6;">
121
+ <li>Competition Rank: <b>27 / 200</b></li>
122
+ <li>Public F1 Score: <b>87.80%</b></li>
123
+ <li>Private F1 Score: <b>87.00%</b></li>
124
+ <li>Experimented with CNN, GRU, BiLSTM, DistilBERT & DeBERTa</li>
125
+ </ul>
126
+ </div>
127
+ """
128
+ )
129
+
130
+ # ---------- RIGHT PANEL (ANALYZER) ----------
131
  with gr.Column(scale=2):
 
 
 
132
 
133
+ with gr.Group():
134
+ user_input = gr.Textbox(
135
+ label="Enter your text",
136
+ placeholder="Example: I feel amazing today! ๐ŸŽ‰",
137
+ lines=4
138
+ )
139
+ analyze_btn = gr.Button("๐ŸŽฏ Analyze Emotion", size="lg")
140
+ output = gr.JSON(label="Model Output")
141
+
142
+ analyze_btn.click(predict, inputs=user_input, outputs=output)
143
+
144
+ gr.Markdown(
145
+ """
146
+ <br><p style="text-align:center; color:#888;">
147
+ Built by <b>Ayusman Samasi</b> โ€ข IIT Madras Deep Learning & GenAI
148
+ </p>
149
+ """
150
+ )
151
 
152
+ demo.launch()