AR04 commited on
Commit
f9556fd
·
verified ·
1 Parent(s): f28e272

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -27
app.py CHANGED
@@ -1,39 +1,49 @@
1
  import gradio as gr
 
 
2
  from transformers import pipeline
3
 
4
- # Load your model
5
  classifier = pipeline(task="text-classification", model="AR04/Senti", top_k=None)
6
 
7
- # Function to classify emotions
8
- def classify_emotion(user_input, history):
9
- if not user_input.strip():
10
- return history
11
-
12
- # Run classifier
13
- outputs = classifier([user_input])[0]
14
-
15
- # Format as "emotion (score)"
16
- emotions = [f"{o['label']} ({o['score']:.2f})" for o in outputs[:5]] # top 5
17
- result_text = ", ".join(emotions)
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Append to chat history
20
- history.append(("🗨️ " + user_input, "🎭 " + result_text))
21
- return history
22
 
23
- # Gradio Chat UI
24
  with gr.Blocks() as demo:
25
- gr.Markdown("# 🎭 SentiChat — Emotion Classifier Demo\nType a message and see what emotions the model detects!")
 
 
 
 
 
 
 
26
 
27
- chatbot = gr.Chatbot(height=400)
28
- msg = gr.Textbox(placeholder="Type your message here...")
29
-
30
- clear = gr.Button("Clear Chat")
31
-
32
- msg.submit(classify_emotion, [msg, chatbot], chatbot).then(
33
- lambda: "", None, msg
34
- )
35
- clear.click(lambda: [], None, chatbot)
36
 
37
- # Launch app
38
  if __name__ == "__main__":
39
  demo.launch()
 
1
  import gradio as gr
2
+ import plotly.express as px
3
+ import pandas as pd
4
  from transformers import pipeline
5
 
6
+ # Load model
7
  classifier = pipeline(task="text-classification", model="AR04/Senti", top_k=None)
8
 
9
+ id2label = {
10
+ 0: "admiration", 1: "amusement", 2: "anger", 3: "annoyance", 4: "approval",
11
+ 5: "caring", 6: "confusion", 7: "curiosity", 8: "desire", 9: "disappointment",
12
+ 10: "disapproval", 11: "disgust", 12: "embarrassment", 13: "excitement",
13
+ 14: "fear", 15: "gratitude", 16: "grief", 17: "joy", 18: "love", 19: "nervousness",
14
+ 20: "optimism", 21: "pride", 22: "realization", 23: "relief", 24: "remorse",
15
+ 25: "sadness", 26: "surprise", 27: "neutral"
16
+ }
17
+
18
+ def classify_and_visualize(text):
19
+ outputs = classifier([text])[0] # list of dicts
20
+ # Convert to DataFrame
21
+ df = pd.DataFrame(outputs)
22
+ df = df.sort_values("score", ascending=False)
23
+
24
+ # Bar chart with plotly
25
+ fig = px.bar(
26
+ df,
27
+ x="label",
28
+ y="score",
29
+ title="Emotion Scores",
30
+ labels={"label": "Emotion", "score": "Probability"},
31
+ )
32
+ fig.update_layout(xaxis_tickangle=-45)
33
 
34
+ return df.to_dict("records"), fig
 
 
35
 
 
36
  with gr.Blocks() as demo:
37
+ gr.Markdown("## 🎭 Emotion Radar Chat App")
38
+ with gr.Row():
39
+ with gr.Column():
40
+ text_input = gr.Textbox(placeholder="Type a message...", lines=2)
41
+ submit_btn = gr.Button("Analyze")
42
+ with gr.Column():
43
+ result_json = gr.JSON()
44
+ result_plot = gr.Plot()
45
 
46
+ submit_btn.click(fn=classify_and_visualize, inputs=text_input, outputs=[result_json, result_plot])
 
 
 
 
 
 
 
 
47
 
 
48
  if __name__ == "__main__":
49
  demo.launch()