Wisaba commited on
Commit
4eaafb1
·
1 Parent(s): fedc39a

Add app.py file

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
+
5
+ # Load your fine-tuned model from the Hugging Face Hub
6
+ model_name = "Wisaba/emotion_roberta_weighted"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
10
+
11
+ # Move to GPU if available
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ model.to(device)
14
+
15
+ # Labels
16
+ emotion_labels = ["sadness", "joy", "love", "anger", "fear", "surprise"]
17
+
18
+ def classify_emotion(text):
19
+ # 1. Tokenize
20
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
21
+ inputs = {k: v.to(device) for k, v in inputs.items()}
22
+
23
+ # 2. Predict
24
+ with torch.no_grad():
25
+ outputs = model(**inputs)
26
+
27
+ # 3. Get Label
28
+ logits = outputs.logits
29
+ predicted_class_id = torch.argmax(logits, dim=-1).item()
30
+ return emotion_labels[predicted_class_id]
31
+
32
+ # Define the Gradio Interface
33
+ iface = gr.Interface(
34
+ fn=classify_emotion,
35
+ inputs=gr.Textbox(lines=2, placeholder="Type how you feel...", label="Text Input"),
36
+ outputs=gr.Textbox(label="Predicted Emotion"),
37
+ title="Emotion Analysis (RoBERTa)",
38
+ description="This model classifies text into 6 emotions: Sadness, Joy, Love, Anger, Fear, Surprise.",
39
+ examples=[
40
+ ["I am feeling so lonely and sad today."],
41
+ ["I'm incredibly excited about the new project!"],
42
+ ["Why did you do that? I'm so mad at you!"]
43
+ ]
44
+ )
45
+
46
+ # Launch
47
+ if __name__ == "__main__":
48
+ iface.launch()