File size: 3,892 Bytes
0c41507
e6769bb
 
0c41507
 
e6769bb
 
a1a37bc
e6769bb
a1a37bc
e6769bb
a1a37bc
 
e6769bb
 
 
 
 
 
a1a37bc
e6769bb
 
a1a37bc
e6769bb
a1a37bc
 
 
e6769bb
 
 
 
 
f199650
 
 
 
c9eb378
f199650
 
 
 
 
 
a1a37bc
 
e6769bb
a1a37bc
e6769bb
 
 
a1a37bc
e6769bb
a1a37bc
 
 
 
e6769bb
a1a37bc
 
 
e6769bb
a1a37bc
 
 
 
 
 
 
 
 
 
e6769bb
a1a37bc
e6769bb
 
 
 
 
 
 
 
 
 
 
 
 
 
f199650
 
a1a37bc
e6769bb
0c41507
2806aea
0c41507
f199650
e6769bb
a1a37bc
2806aea
a1a37bc
0c41507
 
 
e6769bb
 
a1a37bc
 
0c41507
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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()