pratikshahp commited on
Commit
ed2e462
·
verified ·
1 Parent(s): 87e4702

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+ import numpy as np
5
+
6
+ # Load the tokenizer and model
7
+ tokenizer = AutoTokenizer.from_pretrained("michellejieli/emotion_text_classifier")
8
+ model = AutoModelForSequenceClassification.from_pretrained("michellejieli/emotion_text_classifier")
9
+
10
+ # Function to classify emotions
11
+ def classify_emotion(text):
12
+ inputs = tokenizer(text, return_tensors="pt")
13
+ outputs = model(**inputs)
14
+ logits = outputs.logits
15
+ probabilities = torch.nn.functional.softmax(logits, dim=-1)
16
+ probs = probabilities.detach().numpy()[0]
17
+ labels = ["anger", "happy", "sad", "afraid", "worried"]
18
+ results = {label: prob for label, prob in zip(labels, probs)}
19
+ return results
20
+
21
+ # Gradio interface setup
22
+ iface = gr.Interface(
23
+ fn=classify_emotion,
24
+ inputs=gr.Textbox(lines=2, placeholder="Enter a sentence to analyze emotions", label="Input Text"),
25
+ outputs=gr.Label(label="Emotion Probabilities"),
26
+ title="Emotion Classifier",
27
+ description="Enter a sentence and see the probabilities of different emotions."
28
+ )
29
+
30
+ if __name__ == "__main__":
31
+ iface.launch()