Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
import numpy as np
|
|
|
|
|
4 |
|
5 |
-
# Load
|
6 |
-
ocr_pipe = pipeline("image-to-text", model="
|
7 |
-
|
|
|
|
|
8 |
|
9 |
def validate_answer(image, user_text, correct_answer):
|
10 |
-
|
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 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
with gr.Blocks() as demo:
|
32 |
-
gr.Markdown("
|
33 |
with gr.Row():
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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=[
|
46 |
-
outputs=[
|
|
|
|
|
47 |
)
|
48 |
|
49 |
-
demo.launch()
|
|
|
1 |
+
import time
|
2 |
+
print("Pre-loading models...")
|
3 |
+
start = time.time()
|
4 |
+
ocr_pipe = pipeline("image-to-text", model="facebook/nougat-base")
|
5 |
+
similarity_pipe = pipeline("feature-extraction", model="sentence-transformers/paraphrase-albert-small-v2")
|
6 |
+
print(f"Models loaded in {time.time()-start:.2f}s")
|
7 |
import gradio as gr
|
|
|
8 |
import numpy as np
|
9 |
+
from PIL import Image
|
10 |
+
from transformers import pipeline
|
11 |
|
12 |
+
# Load 100% open models (no Microsoft)
|
13 |
+
ocr_pipe = pipeline("image-to-text", model="facebook/nougat-base") # Best for academic handwriting
|
14 |
+
# Alternative OCR models: "mfrashad/arabic-handwriting-ocr", "TesseractOCR"
|
15 |
+
|
16 |
+
similarity_pipe = pipeline("feature-extraction", model="sentence-transformers/paraphrase-albert-small-v2") # Lightweight
|
17 |
|
18 |
def validate_answer(image, user_text, correct_answer):
|
19 |
+
outputs = ["", "", ""] # Initialize outputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
try:
|
22 |
+
# OCR Processing
|
23 |
+
if image:
|
24 |
+
img = Image.fromarray(image.astype('uint8'))
|
25 |
+
ocr_result = ocr_pipe(img)
|
26 |
+
user_text = ocr_result[0]['generated_text']
|
27 |
+
outputs[1] = f"π Extracted: {user_text}"
|
28 |
+
|
29 |
+
# Clarity Check (simple rule-based)
|
30 |
+
clarity = sum(c.isalnum() for c in user_text) / max(1, len(user_text))
|
31 |
+
outputs[0] = f"β
Clarity: {clarity:.0%}" if clarity > 0.5 else "β οΈ Unclear handwriting"
|
32 |
+
|
33 |
+
# Only compare if text is clear
|
34 |
+
if clarity > 0.5:
|
35 |
+
embeds = np.array(similarity_pipe([correct_answer, user_text]))
|
36 |
+
similarity = np.dot(embeds[0].mean(axis=0), embeds[1].mean(axis=0))
|
37 |
+
outputs[2] = f"π Similarity: {similarity:.1%}"
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
outputs = [f"β Error: {str(e)}"] * 3
|
41 |
+
|
42 |
+
return outputs
|
43 |
|
44 |
+
# Simple Interface
|
45 |
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("## Free Handwriting Validator")
|
47 |
with gr.Row():
|
48 |
+
gr.Image(label="Upload Answer", sources=["upload"], type="numpy")
|
49 |
+
gr.Textbox(label="Or Type Answer")
|
50 |
+
gr.Textbox(label="Correct Answer", value="Photosynthesis occurs in chloroplasts.")
|
51 |
+
gr.Button("Validate").click(
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
validate_answer,
|
53 |
+
inputs=[gr.Image(), gr.Textbox(), gr.Textbox()],
|
54 |
+
outputs=[gr.Textbox(label="Status"),
|
55 |
+
gr.Textbox(label="OCR Result"),
|
56 |
+
gr.Textbox(label="Comparison")]
|
57 |
)
|
58 |
|
59 |
+
demo.launch(debug=True)
|