Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model from the Hub or local directory
|
| 6 |
+
model_name = "mjpsm/recommendation-overview-classification-model" # 🔁 Replace with your model path
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
id2label = model.config.id2label
|
| 10 |
+
|
| 11 |
+
# Inference function
|
| 12 |
+
def predict_tag(text):
|
| 13 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
logits = outputs.logits
|
| 17 |
+
predicted_class_id = torch.argmax(logits, dim=1).item()
|
| 18 |
+
predicted_label = id2label[predicted_class_id]
|
| 19 |
+
return predicted_label
|
| 20 |
+
|
| 21 |
+
# Gradio UI
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=predict_tag,
|
| 24 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter student reflection..."),
|
| 25 |
+
outputs="text",
|
| 26 |
+
title="🧠 Recommendation Overview Classifier",
|
| 27 |
+
description="Enter a student's reflection after a math game. The model will return a motivational recommendation tag.",
|
| 28 |
+
examples=[
|
| 29 |
+
"I got frustrated when I made a mistake but I didn’t give up.",
|
| 30 |
+
"I asked my classmate for help and it finally made sense.",
|
| 31 |
+
"It felt like budgeting in real life when I played that part of the game.",
|
| 32 |
+
"Even though I was confused, I tried a new strategy and it worked.",
|
| 33 |
+
],
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
demo.launch()
|