Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +46 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load models
|
6 |
+
model_paths = {
|
7 |
+
"BERT": "models/bert_model",
|
8 |
+
"XLNet": "models/xlnet_model",
|
9 |
+
"GPT-2": "models/gpt2_model"
|
10 |
+
}
|
11 |
+
|
12 |
+
models = {}
|
13 |
+
tokenizers = {}
|
14 |
+
|
15 |
+
for name, path in model_paths.items():
|
16 |
+
tokenizers[name] = AutoTokenizer.from_pretrained(path)
|
17 |
+
models[name] = AutoModelForSequenceClassification.from_pretrained(path)
|
18 |
+
models[name].eval()
|
19 |
+
|
20 |
+
# Emotion labels (adjust based on your dataset!)
|
21 |
+
labels = ["anger", "joy", "sadness", "fear", "love", "surprise"]
|
22 |
+
|
23 |
+
def classify(text, model_choice):
|
24 |
+
tokenizer = tokenizers[model_choice]
|
25 |
+
model = models[model_choice]
|
26 |
+
|
27 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
28 |
+
with torch.no_grad():
|
29 |
+
outputs = model(**inputs)
|
30 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
31 |
+
top_prob, top_idx = torch.max(probs, dim=1)
|
32 |
+
|
33 |
+
return f"Predicted: {labels[top_idx.item()]} ({top_prob.item():.2f})"
|
34 |
+
|
35 |
+
iface = gr.Interface(
|
36 |
+
fn=classify,
|
37 |
+
inputs=[
|
38 |
+
gr.Textbox(lines=3, placeholder="Enter text here...", label="Text"),
|
39 |
+
gr.Radio(choices=["BERT", "XLNet", "GPT-2"], label="Choose Model")
|
40 |
+
],
|
41 |
+
outputs="text",
|
42 |
+
title="Emotion Classifier (BERT / XLNet / GPT-2)"
|
43 |
+
)
|
44 |
+
|
45 |
+
if __name__ == "__main__":
|
46 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
gradio
|