Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the model for emotion detection
|
| 5 |
+
classifier = pipeline(
|
| 6 |
+
"text-classification",
|
| 7 |
+
model='bhadresh-savani/distilbert-base-uncased-emotion',
|
| 8 |
+
return_all_scores=True
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def detect_emotions(emotion_input):
|
| 12 |
+
"""
|
| 13 |
+
Detect emotions in the input text using a pre-trained model.
|
| 14 |
+
Returns a dictionary mapping emotions to their respective scores.
|
| 15 |
+
"""
|
| 16 |
+
prediction = classifier(emotion_input)
|
| 17 |
+
output = {emotion["label"]: round(emotion["score"], 4) for emotion in prediction[0]}
|
| 18 |
+
return output
|
| 19 |
+
|
| 20 |
+
examples = [
|
| 21 |
+
["Every song on the radio reminds me of you."],
|
| 22 |
+
["There's an unfamiliar shadow in the corner of the room."]
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
css = """
|
| 26 |
+
footer {display: none !important;}
|
| 27 |
+
.output-markdown {display: none !important;}
|
| 28 |
+
.gr-button-primary {
|
| 29 |
+
z-index: 14;
|
| 30 |
+
height: 43px;
|
| 31 |
+
width: 130px;
|
| 32 |
+
left: 0px;
|
| 33 |
+
top: 0px;
|
| 34 |
+
padding: 0px;
|
| 35 |
+
cursor: pointer !important;
|
| 36 |
+
background: rgb(17, 20, 45) !important;
|
| 37 |
+
border: none !important;
|
| 38 |
+
text-align: center !important;
|
| 39 |
+
font-family: 'Poppins', sans-serif !important;
|
| 40 |
+
font-size: 14px !important;
|
| 41 |
+
font-weight: 500 !important;
|
| 42 |
+
color: rgb(255, 255, 255) !important;
|
| 43 |
+
line-height: 1 !important;
|
| 44 |
+
border-radius: 12px !important;
|
| 45 |
+
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
|
| 46 |
+
box-shadow: none !important;
|
| 47 |
+
}
|
| 48 |
+
.gr-button-primary:hover {
|
| 49 |
+
background: rgb(66, 133, 244) !important;
|
| 50 |
+
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
|
| 51 |
+
}
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
interface = gr.Interface(
|
| 55 |
+
fn=detect_emotions,
|
| 56 |
+
inputs=gr.Textbox(placeholder="Enter text here", label="Input", lines=2),
|
| 57 |
+
outputs=gr.Label(num_top_classes=5, label="Emotion"),
|
| 58 |
+
title="Emotion Analysis",
|
| 59 |
+
description="Enter a text to detect the underlying emotions using a DistilBERT-based model.",
|
| 60 |
+
examples=examples,
|
| 61 |
+
css=css
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
interface.launch()
|