Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import httpx
|
| 2 |
+
import json
|
| 3 |
+
def safe_json_parse(text: str):
|
| 4 |
+
try:
|
| 5 |
+
return json.loads(text)
|
| 6 |
+
except:
|
| 7 |
+
return {"error": "Invalid JSON", "raw_output": text}
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def grade_answer_image(student_answer: str, model: str = "grader"):
|
| 12 |
+
url = "http://localhost:11434/api/generate" # Ollama API
|
| 13 |
+
payload = {
|
| 14 |
+
"model": model,
|
| 15 |
+
"prompt": f"Student Answer:\n{student_answer}",
|
| 16 |
+
"stream": False
|
| 17 |
+
}
|
| 18 |
+
with httpx.Client() as client:
|
| 19 |
+
response = client.post(url, json=payload, timeout=None)
|
| 20 |
+
output = response.json().get("response", "")
|
| 21 |
+
return safe_json_parse(output)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
from google import genai
|
| 26 |
+
|
| 27 |
+
def extract_ans(image_path):
|
| 28 |
+
client = genai.Client(api_key="AIzaSyCMdx_XOa_u6VqatyFTenwaaUg5VEJ3cDE")
|
| 29 |
+
|
| 30 |
+
myfile = client.files.upload(file=image_path)
|
| 31 |
+
|
| 32 |
+
response = client.models.generate_content(
|
| 33 |
+
model="gemini-2.5-flash", contents=["Extract text from this image", myfile]
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
return grade_answer_image(response.text)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
import gradio as gr
|
| 42 |
+
|
| 43 |
+
interface=gr.Interface(
|
| 44 |
+
fn=extract_ans,
|
| 45 |
+
inputs=gr.Image(type="filepath", label="Upload Image"),
|
| 46 |
+
outputs=gr.JSON(label="Grading Result (JSON Output from Model)"),
|
| 47 |
+
)
|
| 48 |
+
interface.launch(debug=True)
|