petchutney commited on
Commit
fcd3fb0
·
verified ·
1 Parent(s): 60e525b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import numpy as np
4
+
5
+ # Load FREE models
6
+ ocr_pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")
7
+ similarity_pipe = pipeline("feature-extraction", model="sentence-transformers/all-MiniLM-L6-v2")
8
+
9
+ def validate_answer(image, user_text, correct_answer):
10
+ # OCR for handwritten text
11
+ if image:
12
+ ocr_result = ocr_pipe(image)
13
+ user_text = ocr_result[0]['generated_text']
14
+
15
+ # Check clarity (rule-based)
16
+ clarity = sum(c.isalnum() for c in user_text) / max(1, len(user_text))
17
+ if clarity < 0.7:
18
+ return "⚠️ Handwriting unclear", "", ""
19
+
20
+ # Semantic comparison
21
+ embeddings = similarity_pipe([correct_answer, user_text])
22
+ similarity = np.dot(embeddings[0], embeddings[1])
23
+
24
+ return (
25
+ f"✅ Clarity: {clarity:.0%}",
26
+ f"📝 Extracted: {user_text}",
27
+ f"🔍 Similarity: {similarity:.0%}"
28
+ )
29
+
30
+ # Create interface
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("# Free Answer Validator")
33
+ with gr.Row():
34
+ image_input = gr.Image(label="Upload Handwritten Answer", type="pil")
35
+ text_input = gr.Textbox(label="Or Type Answer Here")
36
+ correct_input = gr.Textbox(label="Correct Answer", value="The Earth revolves around the Sun.")
37
+ submit_btn = gr.Button("Validate")
38
+
39
+ clarity_out = gr.Textbox(label="Clarity Check")
40
+ extracted_out = gr.Textbox(label="Extracted Text")
41
+ similarity_out = gr.Textbox(label="Similarity Score")
42
+
43
+ submit_btn.click(
44
+ validate_answer,
45
+ inputs=[image_input, text_input, correct_input],
46
+ outputs=[clarity_out, extracted_out, similarity_out]
47
+ )
48
+
49
+ demo.launch()