Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
|
@@ -7,11 +15,11 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
7 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
|
9 |
# Define emotion labels used by the model
|
10 |
-
emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval",
|
11 |
-
"caring", "confusion", "curiosity", "desire", "disappointment",
|
12 |
-
"disapproval", "disgust", "embarrassment", "excitement",
|
13 |
-
"fear", "gratitude", "grief", "joy", "love", "nervousness",
|
14 |
-
"optimism", "pride", "realization", "relief", "remorse",
|
15 |
"sadness", "surprise", "neutral"]
|
16 |
|
17 |
def predict_emotion(text):
|
@@ -21,34 +29,15 @@ def predict_emotion(text):
|
|
21 |
predicted_class = logits.argmax().item()
|
22 |
|
23 |
predicted_emotion = emotion_labels[predicted_class]
|
24 |
-
|
25 |
-
return predicted_emotion, confidence # Return the predicted emotion and confidence
|
26 |
-
|
27 |
-
def get_confidence_color(confidence):
|
28 |
-
# Define a color scale based on confidence
|
29 |
-
if confidence >= 0.8:
|
30 |
-
return "green"
|
31 |
-
elif confidence >= 0.5:
|
32 |
-
return "orange"
|
33 |
-
else:
|
34 |
-
return "red"
|
35 |
|
36 |
iface = gr.Interface(
|
37 |
fn=predict_emotion,
|
38 |
inputs=gr.Textbox(),
|
39 |
-
outputs=
|
40 |
-
gr.Textbox(),
|
41 |
-
"text",
|
42 |
-
gr.Textbox("Confidence Score:", default=""),
|
43 |
-
],
|
44 |
live=True,
|
45 |
title="Emotion Prediction",
|
46 |
description="Enter a sentence for emotion prediction.",
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
iface.style(
|
51 |
-
confidence_score=get_confidence_color
|
52 |
-
)
|
53 |
-
|
54 |
-
iface.launch()
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
import subprocess
|
4 |
+
|
5 |
+
# Install dependencies
|
6 |
+
subprocess.run(["pip", "install", "-r", "requirements.txt"])
|
7 |
+
|
8 |
+
# Rest of your code
|
9 |
import gradio as gr
|
10 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
11 |
|
|
|
15 |
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
16 |
|
17 |
# Define emotion labels used by the model
|
18 |
+
emotion_labels = ["admiration", "amusement", "anger", "annoyance", "approval",
|
19 |
+
"caring", "confusion", "curiosity", "desire", "disappointment",
|
20 |
+
"disapproval", "disgust", "embarrassment", "excitement",
|
21 |
+
"fear", "gratitude", "grief", "joy", "love", "nervousness",
|
22 |
+
"optimism", "pride", "realization", "relief", "remorse",
|
23 |
"sadness", "surprise", "neutral"]
|
24 |
|
25 |
def predict_emotion(text):
|
|
|
29 |
predicted_class = logits.argmax().item()
|
30 |
|
31 |
predicted_emotion = emotion_labels[predicted_class]
|
32 |
+
return predicted_emotion # Return the predicted emotion directly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
iface = gr.Interface(
|
35 |
fn=predict_emotion,
|
36 |
inputs=gr.Textbox(),
|
37 |
+
outputs="text",
|
|
|
|
|
|
|
|
|
38 |
live=True,
|
39 |
title="Emotion Prediction",
|
40 |
description="Enter a sentence for emotion prediction.",
|
41 |
)
|
42 |
|
43 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|