Spaces:
Sleeping
Sleeping
Deploying on HF Space
Browse files- app.py +57 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
MODEL_ID = "tharakap/deberta-sentiment-analyser"
|
| 5 |
+
|
| 6 |
+
CUSTOM_ID_TO_LABEL = {
|
| 7 |
+
0: "anger",
|
| 8 |
+
1: "fear",
|
| 9 |
+
2: "joy",
|
| 10 |
+
3: "sadness",
|
| 11 |
+
4: "surprise"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def load_model_and_pipeline():
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 16 |
+
|
| 17 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 18 |
+
MODEL_ID,
|
| 19 |
+
id2label=CUSTOM_ID_TO_LABEL
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
return pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 23 |
+
try:
|
| 24 |
+
classifier = load_model_and_pipeline()
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error loading model: {e}")
|
| 27 |
+
classifier = pipeline("sentiment-analysis")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def predict_sentiment(text):
|
| 31 |
+
result = classifier(text)[0]
|
| 32 |
+
|
| 33 |
+
label = result['label'].upper()
|
| 34 |
+
score = result['score']
|
| 35 |
+
|
| 36 |
+
return f"Prediction: **{label}** (Confidence: {score:.4f})"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=predict_sentiment,
|
| 41 |
+
inputs=gr.Textbox(
|
| 42 |
+
lines=5,
|
| 43 |
+
placeholder="Enter text for emotion analysis...",
|
| 44 |
+
label="Input Text"
|
| 45 |
+
),
|
| 46 |
+
outputs="markdown",
|
| 47 |
+
title="DeBERTa Emotion Analysis Demo",
|
| 48 |
+
description=f"An emotion classification demo using the DeBERTa model: {MODEL_ID}.",
|
| 49 |
+
examples=[
|
| 50 |
+
["I am thrilled to hear the good news!"],
|
| 51 |
+
["I cannot believe what just happened; I am completely shocked."],
|
| 52 |
+
["I feel a deep sense of despair and helplessness right now."]
|
| 53 |
+
]
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0
|
| 2 |
+
transformers
|
| 3 |
+
torch
|