ShubhamCoder01 commited on
Commit
fc5b227
·
verified ·
1 Parent(s): 1d2764c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # 🔹 Load your model from Hugging Face Hub
6
+ model_name = "ShubhamCoder01/stress-detection-model"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
9
+
10
+ # 🔹 Prediction function
11
+ def predict(text):
12
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
13
+ with torch.no_grad():
14
+ outputs = model(**inputs)
15
+ probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
16
+ predicted_class = torch.argmax(probs).item()
17
+ confidence = probs[0][predicted_class].item()
18
+
19
+ return {
20
+ "Predicted Label": str(predicted_class),
21
+ "Confidence": round(confidence, 4)
22
+ }
23
+
24
+ # 🔹 Gradio Interface
25
+ iface = gr.Interface(
26
+ fn=predict,
27
+ inputs=gr.Textbox(lines=3, placeholder="Enter a sentence..."),
28
+ outputs="json",
29
+ title="Stress Detection Model",
30
+ description="Enter some text and the model will predict if it indicates stress or not."
31
+ )
32
+
33
+ iface.launch()