Upload 2 files
Browse files- app.py +34 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer
|
| 6 |
+
model_name = "ahmetyaylalioglu/text-emotion-classifier" # Replace with your actual model path
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Function to predict emotion
|
| 11 |
+
def predict_emotion(text):
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
|
| 16 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 17 |
+
prediction = torch.argmax(probabilities, dim=-1).item()
|
| 18 |
+
|
| 19 |
+
emotion = model.config.id2label[prediction]
|
| 20 |
+
confidence = probabilities[0][prediction].item()
|
| 21 |
+
|
| 22 |
+
return f"Emotion: {emotion}\nConfidence: {confidence:.2f}"
|
| 23 |
+
|
| 24 |
+
# Create Gradio interface
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=predict_emotion,
|
| 27 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="Emotion Classifier",
|
| 30 |
+
description="Enter some text and click 'Submit' to predict the emotion."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the app
|
| 34 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|