Spaces:
Running
Running
Nur Arifin Akbar commited on
Commit ·
b6de031
1
Parent(s): 764e5d9
Fix sample data loading by using static image files instead of dynamic generation
Browse files- app.py +12 -8
- generate_samples.py +84 -0
- sample_answer_key.png +0 -0
- sample_student_response.png +0 -0
app.py
CHANGED
|
@@ -180,16 +180,16 @@ Answer: Red, Blue, Yellow
|
|
| 180 |
"""
|
| 181 |
|
| 182 |
def load_sample_data():
|
| 183 |
-
"""Load sample data for demonstration purposes
|
| 184 |
global answer_key_text, student_response_text, recognizer
|
| 185 |
|
| 186 |
try:
|
| 187 |
# Check if API key is available
|
| 188 |
if not GEMINI_API_KEY:
|
| 189 |
return (
|
| 190 |
-
"❌ No API key found. Please
|
| 191 |
"",
|
| 192 |
-
"❌ No API key found. Please
|
| 193 |
""
|
| 194 |
)
|
| 195 |
|
|
@@ -197,11 +197,15 @@ def load_sample_data():
|
|
| 197 |
if recognizer is None:
|
| 198 |
recognizer = HandwritingRecognizer(GEMINI_API_KEY)
|
| 199 |
|
| 200 |
-
#
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
# Process through actual OCR
|
| 207 |
answer_key_text = recognizer.extract_text_from_image(answer_key_img)
|
|
|
|
| 180 |
"""
|
| 181 |
|
| 182 |
def load_sample_data():
|
| 183 |
+
"""Load sample data for demonstration purposes using pre-generated sample images."""
|
| 184 |
global answer_key_text, student_response_text, recognizer
|
| 185 |
|
| 186 |
try:
|
| 187 |
# Check if API key is available
|
| 188 |
if not GEMINI_API_KEY:
|
| 189 |
return (
|
| 190 |
+
"❌ No API key found. Please check your environment configuration.",
|
| 191 |
"",
|
| 192 |
+
"❌ No API key found. Please check your environment configuration.",
|
| 193 |
""
|
| 194 |
)
|
| 195 |
|
|
|
|
| 197 |
if recognizer is None:
|
| 198 |
recognizer = HandwritingRecognizer(GEMINI_API_KEY)
|
| 199 |
|
| 200 |
+
# Load pre-generated sample images
|
| 201 |
+
try:
|
| 202 |
+
answer_key_img = Image.open("sample_answer_key.png")
|
| 203 |
+
student_response_img = Image.open("sample_student_response.png")
|
| 204 |
+
except FileNotFoundError:
|
| 205 |
+
# Fallback: use the sample_images module if files don't exist
|
| 206 |
+
from sample_images import create_sample_answer_key_image, create_sample_student_response_image
|
| 207 |
+
answer_key_img = create_sample_answer_key_image()
|
| 208 |
+
student_response_img = create_sample_student_response_image()
|
| 209 |
|
| 210 |
# Process through actual OCR
|
| 211 |
answer_key_text = recognizer.extract_text_from_image(answer_key_img)
|
generate_samples.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Generate static sample images for the Handwriting Assessment App
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
+
|
| 7 |
+
def create_sample_answer_key_image():
|
| 8 |
+
"""Create a sample answer key image optimized for gemini3n series (768x768)."""
|
| 9 |
+
img_width, img_height = 768, 768
|
| 10 |
+
img = Image.new('RGB', (img_width, img_height), color='white')
|
| 11 |
+
draw = ImageDraw.Draw(img)
|
| 12 |
+
|
| 13 |
+
# Use default font to avoid font loading issues
|
| 14 |
+
try:
|
| 15 |
+
font_large = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 40)
|
| 16 |
+
font_medium = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 35)
|
| 17 |
+
except:
|
| 18 |
+
font_large = ImageFont.load_default()
|
| 19 |
+
font_medium = ImageFont.load_default()
|
| 20 |
+
|
| 21 |
+
# Sample answer key content
|
| 22 |
+
lines = [
|
| 23 |
+
("Question 1: What is the capital of France?", font_medium),
|
| 24 |
+
("Answer: Paris", font_large),
|
| 25 |
+
("", font_medium),
|
| 26 |
+
("Question 2: Calculate 15 + 27", font_medium),
|
| 27 |
+
("Answer: 42", font_large),
|
| 28 |
+
("", font_medium),
|
| 29 |
+
("Question 3: Name three primary colors", font_medium),
|
| 30 |
+
("Answer: Red, Blue, Yellow", font_large)
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
y_position = 80
|
| 34 |
+
for line_text, font in lines:
|
| 35 |
+
if line_text:
|
| 36 |
+
x_offset = 60
|
| 37 |
+
draw.text((x_offset, y_position), line_text, fill='black', font=font)
|
| 38 |
+
y_position += 80
|
| 39 |
+
|
| 40 |
+
return img
|
| 41 |
+
|
| 42 |
+
def create_sample_student_response_image():
|
| 43 |
+
"""Create a sample student response image optimized for gemini3n series (768x768)."""
|
| 44 |
+
img_width, img_height = 768, 768
|
| 45 |
+
img = Image.new('RGB', (img_width, img_height), color='white')
|
| 46 |
+
draw = ImageDraw.Draw(img)
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
font_large = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 40)
|
| 50 |
+
font_medium = ImageFont.truetype("/System/Library/Fonts/Arial.ttf", 35)
|
| 51 |
+
except:
|
| 52 |
+
font_large = ImageFont.load_default()
|
| 53 |
+
font_medium = ImageFont.load_default()
|
| 54 |
+
|
| 55 |
+
# Student response with slight variations
|
| 56 |
+
lines = [
|
| 57 |
+
("Question 1: What is the capital of France?", font_medium),
|
| 58 |
+
("Answer: Paris", font_large),
|
| 59 |
+
("", font_medium),
|
| 60 |
+
("Question 2: Calculate 15 + 27", font_medium),
|
| 61 |
+
("Answer: 42", font_large),
|
| 62 |
+
("", font_medium),
|
| 63 |
+
("Question 3: Name three primary colors", font_medium),
|
| 64 |
+
("Answer: Red, Blue, Yellow", font_large)
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
y_position = 80
|
| 68 |
+
for line_text, font in lines:
|
| 69 |
+
if line_text:
|
| 70 |
+
x_offset = 65
|
| 71 |
+
draw.text((x_offset, y_position), line_text, fill='black', font=font)
|
| 72 |
+
y_position += 80
|
| 73 |
+
|
| 74 |
+
return img
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
# Generate and save sample images
|
| 78 |
+
answer_key_img = create_sample_answer_key_image()
|
| 79 |
+
answer_key_img.save("sample_answer_key.png")
|
| 80 |
+
print("Created sample_answer_key.png")
|
| 81 |
+
|
| 82 |
+
student_response_img = create_sample_student_response_image()
|
| 83 |
+
student_response_img.save("sample_student_response.png")
|
| 84 |
+
print("Created sample_student_response.png")
|
sample_answer_key.png
ADDED
|
sample_student_response.png
ADDED
|