|
|
import httpx |
|
|
import json |
|
|
def safe_json_parse(text: str): |
|
|
try: |
|
|
return json.loads(text) |
|
|
except: |
|
|
return {"error": "Invalid JSON", "raw_output": text} |
|
|
|
|
|
|
|
|
|
|
|
def grade_answer_image(student_answer: str, model: str = "grader"): |
|
|
url = "http://localhost:11434/api/generate" |
|
|
payload = { |
|
|
"model": model, |
|
|
"prompt": f"Student Answer:\n{student_answer}", |
|
|
"stream": False |
|
|
} |
|
|
with httpx.Client() as client: |
|
|
response = client.post(url, json=payload, timeout=None) |
|
|
output = response.json().get("response", "") |
|
|
return safe_json_parse(output) |
|
|
|
|
|
|
|
|
|
|
|
from google import genai |
|
|
|
|
|
def extract_ans(image_path): |
|
|
client = genai.Client(api_key="AIzaSyCMdx_XOa_u6VqatyFTenwaaUg5VEJ3cDE") |
|
|
|
|
|
myfile = client.files.upload(file=image_path) |
|
|
|
|
|
response = client.models.generate_content( |
|
|
model="gemini-2.5-flash", contents=["Extract text from this image", myfile] |
|
|
) |
|
|
|
|
|
|
|
|
return grade_answer_image(response.text) |
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
|
|
|
|
interface=gr.Interface( |
|
|
fn=extract_ans, |
|
|
inputs=gr.Image(type="filepath", label="Upload Image"), |
|
|
outputs=gr.JSON(label="Grading Result (JSON Output from Model)"), |
|
|
) |
|
|
interface.launch(debug=True) |