Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,19 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
# Define the function to use the model for predictions
|
4 |
def classify_emotion(text):
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
# Validate the input
|
9 |
def validate_input(text):
|
@@ -19,7 +29,6 @@ interface = gr.Interface(
|
|
19 |
title="Emotion Classifier",
|
20 |
description="Enter some text and let the model predict the emotion.",
|
21 |
examples=["I am feeling great today!", "I am so sad and depressed.", "I am excited about the new project."],
|
22 |
-
theme="huggingface"
|
23 |
)
|
24 |
|
25 |
# Add some custom CSS to improve the look and feel
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load the model and tokenizer once during initialization
|
6 |
+
model_name = "AnkitAI/deberta-xlarge-base-emotions-classifier"
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
# Define the function to use the model for predictions
|
11 |
def classify_emotion(text):
|
12 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
13 |
+
outputs = model(**inputs)
|
14 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
15 |
+
labels = ["joy", "anger", "sadness", "fear", "surprise", "love"] # Adjust based on the actual labels used by the model
|
16 |
+
return {labels[i]: float(probs[0][i]) for i in range(len(labels))}
|
17 |
|
18 |
# Validate the input
|
19 |
def validate_input(text):
|
|
|
29 |
title="Emotion Classifier",
|
30 |
description="Enter some text and let the model predict the emotion.",
|
31 |
examples=["I am feeling great today!", "I am so sad and depressed.", "I am excited about the new project."],
|
|
|
32 |
)
|
33 |
|
34 |
# Add some custom CSS to improve the look and feel
|