Spaces:
Runtime error
Runtime error
Created app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load Phi-4 model from Hugging Face
|
| 6 |
+
model_name = "microsoft/phi-4"
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_name,
|
| 9 |
+
torch_dtype=torch.float16,
|
| 10 |
+
device_map="auto"
|
| 11 |
+
)
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
|
| 14 |
+
# Ensure tokenizer compatibility
|
| 15 |
+
if tokenizer.pad_token_id is None:
|
| 16 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 17 |
+
|
| 18 |
+
# Define the homework checker function
|
| 19 |
+
def check_homework(exercise, solution):
|
| 20 |
+
prompt = f"""
|
| 21 |
+
Exercise: {exercise}
|
| 22 |
+
Solution: {solution}
|
| 23 |
+
Task: Validate the solution to the math problem provided by the user.
|
| 24 |
+
If correct, confirm it; if incorrect, provide corrections and reasoning.
|
| 25 |
+
"""
|
| 26 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 27 |
+
outputs = model.generate(**inputs, max_new_tokens=1024)
|
| 28 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
# Gradio UI
|
| 32 |
+
interface = gr.Interface(
|
| 33 |
+
fn=check_homework,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Textbox(lines=2, placeholder="Enter the exercise...", label="Exercise"),
|
| 36 |
+
gr.Textbox(lines=1, placeholder="Enter your solution...", label="Your Solution")
|
| 37 |
+
],
|
| 38 |
+
outputs=gr.Textbox(label="Feedback"),
|
| 39 |
+
title="AI Homework Checker",
|
| 40 |
+
description="Validate and improve your math homework with AI.",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
interface.launch()
|