Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import onnxruntime as ort
|
4 |
+
from transformers import AutoTokenizer, AutoConfig
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
# Load model and tokenizer
|
8 |
+
repo_id = "iimran/EmotionDetection"
|
9 |
+
filename = "model.onnx"
|
10 |
+
|
11 |
+
# Download and setup ONNX model
|
12 |
+
onnx_model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
14 |
+
config = AutoConfig.from_pretrained(repo_id)
|
15 |
+
|
16 |
+
# Get label mapping
|
17 |
+
if hasattr(config, "id2label") and config.id2label and len(config.id2label) > 0:
|
18 |
+
id2label = config.id2label
|
19 |
+
else:
|
20 |
+
id2label = {
|
21 |
+
0: "anger",
|
22 |
+
1: "fear",
|
23 |
+
2: "joy",
|
24 |
+
3: "love",
|
25 |
+
4: "sadness",
|
26 |
+
5: "surprise",
|
27 |
+
6: "neutral"
|
28 |
+
}
|
29 |
+
|
30 |
+
# Create ONNX session
|
31 |
+
session = ort.InferenceSession(onnx_model_path)
|
32 |
+
|
33 |
+
def predict_emotion(text):
|
34 |
+
"""Predict emotion from text"""
|
35 |
+
# Tokenize input
|
36 |
+
inputs = tokenizer(
|
37 |
+
text,
|
38 |
+
return_tensors="np",
|
39 |
+
truncation=True,
|
40 |
+
padding="max_length",
|
41 |
+
max_length=256
|
42 |
+
)
|
43 |
+
|
44 |
+
# Prepare inputs
|
45 |
+
ort_inputs = {
|
46 |
+
"input_ids": inputs["input_ids"].astype(np.int64),
|
47 |
+
"attention_mask": inputs["attention_mask"].astype(np.int64)
|
48 |
+
}
|
49 |
+
|
50 |
+
# Run inference
|
51 |
+
outputs = session.run(None, ort_inputs)
|
52 |
+
logits = outputs[0]
|
53 |
+
predicted_class_id = int(np.argmax(logits, axis=-1)[0])
|
54 |
+
|
55 |
+
# Get label
|
56 |
+
predicted_label = id2label.get(str(predicted_class_id), id2label.get(predicted_class_id, str(predicted_class_id)))
|
57 |
+
|
58 |
+
# Format output
|
59 |
+
emotion_icons = {
|
60 |
+
"anger": "π ",
|
61 |
+
"fear": "π¨",
|
62 |
+
"joy": "π",
|
63 |
+
"love": "β€οΈ",
|
64 |
+
"sadness": "π’",
|
65 |
+
"surprise": "π²",
|
66 |
+
"neutral": "π"
|
67 |
+
}
|
68 |
+
|
69 |
+
icon = emotion_icons.get(predicted_label.lower(), "β")
|
70 |
+
return f"{icon} {predicted_label}"
|
71 |
+
|
72 |
+
# Create Gradio interface
|
73 |
+
demo = gr.Interface(
|
74 |
+
fn=predict_emotion,
|
75 |
+
inputs=gr.Textbox(label="Enter your text", placeholder="How are you feeling today?"),
|
76 |
+
outputs=gr.Label(label="Predicted Emotion"),
|
77 |
+
title="Emotion Detection",
|
78 |
+
description="Detect emotions in text using iimran/EmotionDetection model",
|
79 |
+
examples=[
|
80 |
+
["I'm so happy right now!"],
|
81 |
+
["This situation makes me really angry"],
|
82 |
+
["I feel anxious about the future"],
|
83 |
+
["What a beautiful day to be alive!"],
|
84 |
+
["That news shocked me completely"]
|
85 |
+
],
|
86 |
+
theme="soft"
|
87 |
+
)
|
88 |
+
|
89 |
+
# Run the app
|
90 |
+
if __name__ == "__main__":
|
91 |
+
demo.launch()
|