jasvir-singh1021 commited on
Commit
591185f
·
verified ·
1 Parent(s): b69fa82

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # Emotions
6
+ emotions = ["Anger", "Love", "Fear", "Joy", "Sadness", "Surprise"]
7
+
8
+ # Load fine-tuned model
9
+ model_path = "./model"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
11
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
12
+
13
+ def predict_emotions(comment):
14
+ inputs = tokenizer(comment, return_tensors="pt", truncation=True)
15
+ outputs = model(**inputs)
16
+ scores = torch.sigmoid(outputs.logits)[0].detach().numpy()
17
+ return {emotion: float(scores[i]) for i, emotion in enumerate(emotions)}
18
+
19
+ demo = gr.Interface(
20
+ fn=predict_emotions,
21
+ inputs=gr.Textbox(lines=4, placeholder="Enter GitHub comment here..."),
22
+ outputs=gr.Label(num_top_classes=6),
23
+ title="GitHub Comment Emotion Detector",
24
+ description="Detects Anger, Love, Fear, Joy, Sadness, and Surprise in GitHub comments."
25
+ )
26
+
27
+ if __name__ == "__main__":
28
+ demo.launch()