Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the text classification model pipeline
|
5 |
+
classifier = pipeline("text-classification", model='isom5240ust/bert-base-uncased-emotion', return_all_scores=True)
|
6 |
+
|
7 |
+
def classify_text(text):
|
8 |
+
"""
|
9 |
+
Classifies the input text and returns the label and score of the most likely emotion.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
text (str): The text to classify.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
tuple: A tuple containing the label and score of the most likely emotion.
|
16 |
+
"""
|
17 |
+
results = classifier(text)[0]
|
18 |
+
|
19 |
+
max_score = float('-inf')
|
20 |
+
max_label = ''
|
21 |
+
|
22 |
+
for result in results:
|
23 |
+
if result['score'] > max_score:
|
24 |
+
max_score = result['score']
|
25 |
+
max_label = result['label']
|
26 |
+
|
27 |
+
return max_label, max_score
|
28 |
+
|
29 |
+
# Gradio interface
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=classify_text,
|
32 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter text here..."),
|
33 |
+
outputs=[
|
34 |
+
gr.Label(label="Label"),
|
35 |
+
gr.Number(label="Score")
|
36 |
+
],
|
37 |
+
title="Text Classification",
|
38 |
+
description="Classification for 6 emotions: sadness, joy, love, anger, fear, surprise"
|
39 |
+
)
|
40 |
+
|
41 |
+
iface.launch()
|