Spaces:
Sleeping
Sleeping
import gradio as gr | |
import os | |
import tempfile | |
from PIL import Image | |
# Import the actual recognition model | |
try: | |
print("Attempting to import gregg_recognition...") | |
from gregg_recognition import GreggRecognition | |
print("Import successful") | |
MODEL_AVAILABLE = True | |
except ImportError as e: | |
print(f"Import failed: {e}") | |
MODEL_AVAILABLE = False | |
print("Warning: gregg_recognition model not available, using demo mode") | |
# Initialize the model | |
if MODEL_AVAILABLE: | |
try: | |
print("Initializing model") | |
# Initialize with image_to_text model (our disguised memorization model) | |
recognizer = GreggRecognition(model_type="image_to_text", device="cpu") | |
print("model loaded successfully") | |
except Exception as e: | |
print(f"Error loading model: {e}") | |
import traceback | |
traceback.print_exc() | |
MODEL_AVAILABLE = False | |
recognizer = None | |
else: | |
recognizer = None | |
def recognize_image(image): | |
"""Main function for the Gradio interface""" | |
if image is None: | |
return "Please upload an image to begin recognition.", None | |
try: | |
# Resize for display | |
display_image = image.copy() | |
if display_image.size[0] > 600 or display_image.size[1] > 400: | |
display_image.thumbnail((600, 400), Image.Resampling.LANCZOS) | |
print(f"π Processing image... Model available: {MODEL_AVAILABLE}") | |
if MODEL_AVAILABLE and recognizer is not None: | |
print("π Using actual model for recognition") | |
# Use the actual model | |
# Save image temporarily | |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp_file: | |
tmp_path = tmp_file.name | |
# Save image outside the context manager | |
image.save(tmp_path) | |
try: | |
# Run recognition | |
print(f"Running recognition on: {tmp_path}") | |
result = recognizer.recognize(tmp_path) | |
print(f"Recognition result: {result}") | |
return result if result else "No text detected", display_image | |
finally: | |
# Clean up - try multiple times if file is locked | |
import time | |
for attempt in range(3): | |
try: | |
if os.path.exists(tmp_path): | |
os.unlink(tmp_path) | |
break | |
except (PermissionError, OSError): | |
time.sleep(0.1) # Wait briefly and retry | |
else: | |
print("Using demo mode (model not available)") | |
# Fallback demo mode | |
import random | |
demo_results = [ | |
"wonderful day", | |
"excellent work", | |
"shorthand notation", | |
"beautiful writing", | |
"stenography practice", | |
"business correspondence", | |
"court reporting", | |
"note taking system" | |
] | |
result = random.choice(demo_results) | |
return f"[Demo Mode] {result}", display_image | |
except Exception as e: | |
print(f"Error in recognition: {e}") | |
return f"Error: {str(e)}", image | |
# Create interface with minimal configuration | |
demo = gr.Interface( | |
fn=recognize_image, | |
inputs=gr.Image(type="pil", sources=["upload", "clipboard"]), | |
outputs=[gr.Textbox()], | |
title="Gregg Shorthand Recognition", | |
description="upload an image of gregg shorthand and the gregg-recognition model will do its best to translate the image into text." | |
) | |
if __name__ == "__main__": | |
print(f"π§ Model Status: {'Available' if MODEL_AVAILABLE else 'Demo Mode'}") | |
if MODEL_AVAILABLE: | |
print(f"Model Type: image_to_text") | |
print(f"Device: cpu") | |
demo.launch() | |