petchutney commited on
Commit
e47af88
Β·
verified Β·
1 Parent(s): fcd3fb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -37
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 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()
 
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)