minoosh commited on
Commit
5434155
·
1 Parent(s): 96b0271

adding app.py + requirements.txt

Browse files
Files changed (3) hide show
  1. .gitignore +2 -0
  2. app.py +55 -0
  3. requirements.txt +8 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ venv
2
+ keys.txt
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # Model name (you can swap this for another emotion model if you like)
6
+ model_name = "j-hartmann/emotion-english-distilroberta-base"
7
+ #minoosh/finetuned_bert-base-on-IEMOCAP_1
8
+
9
+ # Device
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+
12
+ # Load tokenizer and model
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+ model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
15
+ model.eval()
16
+
17
+ # Prediction function
18
+ def predict_emotion(text: str):
19
+ # Handle empty input
20
+ if not text or not text.strip():
21
+ return {"Error": "Please enter some text."}
22
+
23
+ # Tokenize
24
+ inputs = tokenizer(
25
+ text,
26
+ return_tensors="pt",
27
+ truncation=True,
28
+ padding=True,
29
+ max_length=256, # you can adjust this if needed
30
+ ).to(device)
31
+
32
+ with torch.no_grad():
33
+ outputs = model(**inputs)
34
+ probs = outputs.logits.softmax(dim=-1)[0]
35
+
36
+ # Map id -> label using model config
37
+ id2label = model.config.id2label
38
+ scores = {id2label[i]: float(probs[i]) for i in range(len(probs))}
39
+
40
+ # Sort by highest probability first (optional but nice in the UI)
41
+ scores = dict(sorted(scores.items(), key=lambda x: x[1], reverse=True))
42
+ return scores
43
+
44
+ # Gradio interface
45
+ demo = gr.Interface(
46
+ fn=predict_emotion,
47
+ inputs=gr.Textbox(lines=4, label="Enter text"),
48
+ outputs=gr.Label(label="Emotion Probabilities"),
49
+ title="Emotion Classifier",
50
+ description="Enter a sentence and see the predicted emotion distribution.",
51
+ allow_flagging="never",
52
+ )
53
+
54
+ if __name__ == "__main__":
55
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ torch==2.8.0
3
+ transformers==4.57.2
4
+ pydantic==2.9.1
5
+
6
+ huggingface-hub==0.36.0
7
+ tokenizers==0.22.1
8
+ safetensors==0.7.0