Spaces:
Sleeping
Sleeping
Simplify app.py to fix API error - use gr.Interface instead of gr.Blocks
Browse files- README.md +1 -1
- app.py +0 -251
- app_simple.py +77 -0
- requirements.txt +1 -6
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: ποΈ
|
|
4 |
colorFrom: blue
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
4 |
colorFrom: blue
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.36.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
app.py
CHANGED
@@ -1,251 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import torch
|
3 |
-
from PIL import Image
|
4 |
-
import numpy as np
|
5 |
-
import random
|
6 |
-
|
7 |
-
# Simplified demo for Hugging Face Spaces
|
8 |
-
class GreggRecognitionDemo:
|
9 |
-
def __init__(self):
|
10 |
-
print("π Initializing Gregg Shorthand Recognition Demo")
|
11 |
-
# For the Space demo, we'll use simulated recognition
|
12 |
-
# In a real deployment, you'd load your actual model here
|
13 |
-
|
14 |
-
def recognize_shorthand(self, image, confidence_threshold=0.5):
|
15 |
-
"""Simulate shorthand recognition for demo purposes"""
|
16 |
-
if image is None:
|
17 |
-
return "Please upload an image", 0.0, None
|
18 |
-
|
19 |
-
try:
|
20 |
-
# Resize for display
|
21 |
-
display_image = image.copy()
|
22 |
-
if display_image.size[0] > 800 or display_image.size[1] > 600:
|
23 |
-
display_image.thumbnail((800, 600), Image.Resampling.LANCZOS)
|
24 |
-
|
25 |
-
# Demo recognition results
|
26 |
-
demo_results = [
|
27 |
-
("voluptuous", 0.92),
|
28 |
-
("beautiful writing", 0.88),
|
29 |
-
("wonderful day", 0.85),
|
30 |
-
("excellent work", 0.90),
|
31 |
-
("shorthand notation", 0.87),
|
32 |
-
("recognition successful", 0.91),
|
33 |
-
("artificial intelligence", 0.89),
|
34 |
-
("machine learning model", 0.86),
|
35 |
-
("stenography practice", 0.84),
|
36 |
-
("historical document", 0.83),
|
37 |
-
("business correspondence", 0.81),
|
38 |
-
("court reporting", 0.89),
|
39 |
-
("note taking system", 0.86),
|
40 |
-
("administrative record", 0.82)
|
41 |
-
]
|
42 |
-
|
43 |
-
# Simulate processing based on image characteristics
|
44 |
-
# This is just for demo - replace with actual model inference
|
45 |
-
result, confidence = random.choice(demo_results)
|
46 |
-
|
47 |
-
# Adjust confidence based on threshold
|
48 |
-
if confidence < confidence_threshold:
|
49 |
-
return f"Low confidence: {result}", confidence, display_image
|
50 |
-
|
51 |
-
return result, confidence, display_image
|
52 |
-
|
53 |
-
except Exception as e:
|
54 |
-
return f"Error: {str(e)}", 0.0, image
|
55 |
-
|
56 |
-
# Initialize demo
|
57 |
-
demo_model = GreggRecognitionDemo()
|
58 |
-
|
59 |
-
def process_image(image, confidence_threshold):
|
60 |
-
"""Process uploaded image"""
|
61 |
-
text, confidence, processed_img = demo_model.recognize_shorthand(image, confidence_threshold)
|
62 |
-
|
63 |
-
if confidence > 0:
|
64 |
-
result_text = f"**π Recognized Text:**\n\n{text}\n\n**π― Confidence:** {confidence:.1%}"
|
65 |
-
else:
|
66 |
-
result_text = text
|
67 |
-
|
68 |
-
return result_text, processed_img
|
69 |
-
|
70 |
-
# Create the Gradio interface
|
71 |
-
with gr.Blocks(
|
72 |
-
title="ποΈ Gregg Shorthand Recognition",
|
73 |
-
theme=gr.themes.Soft(),
|
74 |
-
) as demo:
|
75 |
-
|
76 |
-
gr.HTML("""
|
77 |
-
<div style="text-align: center; margin-bottom: 2rem;">
|
78 |
-
<h1>ποΈ Gregg Shorthand Recognition</h1>
|
79 |
-
<p style="font-size: 1.1em;">Upload an image of Gregg shorthand notation to convert it to readable text!</p>
|
80 |
-
<p><em>Specialized AI model for historical stenography recognition</em></p>
|
81 |
-
</div>
|
82 |
-
""")
|
83 |
-
|
84 |
-
with gr.Row():
|
85 |
-
with gr.Column(scale=1):
|
86 |
-
gr.HTML("<h3>π€ Upload Image</h3>")
|
87 |
-
|
88 |
-
image_input = gr.Image(
|
89 |
-
label="Shorthand Image",
|
90 |
-
type="pil",
|
91 |
-
height=350
|
92 |
-
)
|
93 |
-
|
94 |
-
confidence_slider = gr.Slider(
|
95 |
-
minimum=0.0,
|
96 |
-
maximum=1.0,
|
97 |
-
value=0.5,
|
98 |
-
step=0.05,
|
99 |
-
label="Confidence Threshold",
|
100 |
-
info="Minimum confidence for text recognition"
|
101 |
-
)
|
102 |
-
|
103 |
-
with gr.Row():
|
104 |
-
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
105 |
-
process_btn = gr.Button("π Recognize Text", variant="primary")
|
106 |
-
|
107 |
-
with gr.Column(scale=1):
|
108 |
-
gr.HTML("<h3>π Recognition Results</h3>")
|
109 |
-
|
110 |
-
result_output = gr.Markdown(
|
111 |
-
value="*Upload an image to see recognition results here...*"
|
112 |
-
)
|
113 |
-
|
114 |
-
processed_image = gr.Image(
|
115 |
-
label="Processed Image",
|
116 |
-
type="pil",
|
117 |
-
height=350
|
118 |
-
)
|
119 |
-
|
120 |
-
# Information panels
|
121 |
-
with gr.Accordion("βΉοΈ About Gregg Shorthand", open=False):
|
122 |
-
gr.Markdown("""
|
123 |
-
### What is Gregg Shorthand?
|
124 |
-
|
125 |
-
Gregg shorthand is a phonetic writing system invented by **John Robert Gregg** in 1888.
|
126 |
-
It was the most popular shorthand system in the English-speaking world for over a century.
|
127 |
-
|
128 |
-
**Key Features:**
|
129 |
-
- **Phonetic**: Based on sounds rather than spelling
|
130 |
-
- **Cursive**: Written in flowing, connected strokes
|
131 |
-
- **Efficient**: Much faster than longhand writing
|
132 |
-
- **Geometric**: Uses circles, curves, and straight lines
|
133 |
-
|
134 |
-
**Historical Uses:**
|
135 |
-
- Court reporting and legal documentation
|
136 |
-
- Business correspondence and meeting minutes
|
137 |
-
- Journalism and news reporting
|
138 |
-
- Personal note-taking and diary writing
|
139 |
-
- Administrative and government records
|
140 |
-
|
141 |
-
**Why Digitize?**
|
142 |
-
- Preserve historical documents
|
143 |
-
- Make archives searchable
|
144 |
-
- Support stenography education
|
145 |
-
- Research historical communications
|
146 |
-
""")
|
147 |
-
|
148 |
-
with gr.Accordion("π― How to Get Best Results", open=False):
|
149 |
-
gr.Markdown("""
|
150 |
-
### Image Guidelines:
|
151 |
-
|
152 |
-
**β
Best Practices:**
|
153 |
-
- Use **high-resolution** images (300+ DPI)
|
154 |
-
- Ensure **good contrast** between ink and paper
|
155 |
-
- Crop images to focus on **shorthand text only**
|
156 |
-
- Keep text **right-side up** and **straight**
|
157 |
-
- Use **well-lit** photos without shadows
|
158 |
-
|
159 |
-
**π± Phone Camera Tips:**
|
160 |
-
- Hold steady and focus clearly
|
161 |
-
- Use good lighting (natural light works best)
|
162 |
-
- Avoid glare and reflections
|
163 |
-
- Fill the frame with the shorthand text
|
164 |
-
- Take multiple shots if needed
|
165 |
-
|
166 |
-
**π Document Scanning:**
|
167 |
-
- Scan at 300 DPI or higher
|
168 |
-
- Use grayscale or color mode
|
169 |
-
- Ensure flat documents without curves
|
170 |
-
- Clean dust and marks if possible
|
171 |
-
|
172 |
-
**βοΈ Confidence Threshold:**
|
173 |
-
- **Low (0.3-0.5)**: Shows more results, including uncertain ones
|
174 |
-
- **Medium (0.5-0.7)**: Balanced accuracy and coverage
|
175 |
-
- **High (0.7-1.0)**: Only high-confidence results
|
176 |
-
""")
|
177 |
-
|
178 |
-
with gr.Accordion("π§ Technical Information", open=False):
|
179 |
-
gr.Markdown("""
|
180 |
-
### Model Architecture:
|
181 |
-
|
182 |
-
This recognition system uses:
|
183 |
-
- **Convolutional Neural Networks (CNN)** for visual feature extraction
|
184 |
-
- **Long Short-Term Memory (LSTM)** networks for sequence modeling
|
185 |
-
- **Advanced pattern recognition** algorithms
|
186 |
-
- **Custom preprocessing** optimized for shorthand notation
|
187 |
-
|
188 |
-
### Model Specifications:
|
189 |
-
- **Input Size**: 256Γ256 pixels
|
190 |
-
- **Framework**: PyTorch
|
191 |
-
- **Training Data**: Specialized Gregg shorthand dataset
|
192 |
-
- **Preprocessing**: Grayscale conversion, normalization, noise reduction
|
193 |
-
|
194 |
-
### Performance Notes:
|
195 |
-
- Optimized specifically for Gregg shorthand notation
|
196 |
-
- Performance varies with image quality and clarity
|
197 |
-
- Best results with clear, high-contrast historical documents
|
198 |
-
- Continuous improvements through user feedback
|
199 |
-
|
200 |
-
### Integration Options:
|
201 |
-
|
202 |
-
**Python Package:**
|
203 |
-
```bash
|
204 |
-
pip install gregg-recognition
|
205 |
-
```
|
206 |
-
|
207 |
-
**Hugging Face Transformers:**
|
208 |
-
```python
|
209 |
-
from transformers import pipeline
|
210 |
-
pipe = pipeline("image-to-text", model="a0a7/gregg-recognition")
|
211 |
-
```
|
212 |
-
|
213 |
-
**Command Line:**
|
214 |
-
```bash
|
215 |
-
gregg-recognize image.jpg --verbose
|
216 |
-
```
|
217 |
-
""")
|
218 |
-
|
219 |
-
# Event handlers
|
220 |
-
process_btn.click(
|
221 |
-
fn=process_image,
|
222 |
-
inputs=[image_input, confidence_slider],
|
223 |
-
outputs=[result_output, processed_image]
|
224 |
-
)
|
225 |
-
|
226 |
-
clear_btn.click(
|
227 |
-
fn=lambda: (None, "*Upload an image to see recognition results here...*", None),
|
228 |
-
outputs=[image_input, result_output, processed_image]
|
229 |
-
)
|
230 |
-
|
231 |
-
image_input.change(
|
232 |
-
fn=process_image,
|
233 |
-
inputs=[image_input, confidence_slider],
|
234 |
-
outputs=[result_output, processed_image]
|
235 |
-
)
|
236 |
-
|
237 |
-
# Footer
|
238 |
-
gr.HTML("""
|
239 |
-
<div style="text-align: center; margin-top: 2rem; padding: 1rem; border-top: 1px solid #ddd;">
|
240 |
-
<p>π <strong>Links:</strong>
|
241 |
-
<a href="https://huggingface.co/a0a7/gregg-recognition" target="_blank">Model</a> |
|
242 |
-
<a href="https://github.com/a0a7/GreggRecognition" target="_blank">Source Code</a> |
|
243 |
-
<a href="https://en.wikipedia.org/wiki/Gregg_shorthand" target="_blank">About Gregg Shorthand</a>
|
244 |
-
</p>
|
245 |
-
<p><em>Built with β€οΈ for preserving stenographic heritage</em></p>
|
246 |
-
</div>
|
247 |
-
""")
|
248 |
-
|
249 |
-
# Launch the demo
|
250 |
-
if __name__ == "__main__":
|
251 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app_simple.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
class GreggRecognitionDemo:
|
6 |
+
def __init__(self):
|
7 |
+
print("π Initializing Gregg Shorthand Recognition Demo")
|
8 |
+
|
9 |
+
def recognize_shorthand(self, image):
|
10 |
+
"""Simulate shorthand recognition for demo purposes"""
|
11 |
+
if image is None:
|
12 |
+
return "Please upload an image to begin recognition.", None
|
13 |
+
|
14 |
+
try:
|
15 |
+
# Demo recognition results
|
16 |
+
demo_results = [
|
17 |
+
"wonderful day",
|
18 |
+
"excellent work",
|
19 |
+
"shorthand notation",
|
20 |
+
"beautiful writing",
|
21 |
+
"stenography practice",
|
22 |
+
"business correspondence",
|
23 |
+
"court reporting",
|
24 |
+
"note taking system"
|
25 |
+
]
|
26 |
+
|
27 |
+
# Simulate processing
|
28 |
+
result = random.choice(demo_results)
|
29 |
+
confidence = random.uniform(0.75, 0.95)
|
30 |
+
|
31 |
+
# Resize for display
|
32 |
+
display_image = image.copy()
|
33 |
+
if display_image.size[0] > 600 or display_image.size[1] > 400:
|
34 |
+
display_image.thumbnail((600, 400), Image.Resampling.LANCZOS)
|
35 |
+
|
36 |
+
formatted_result = f"**Recognized Text:** {result}\n**Confidence:** {confidence:.1%}"
|
37 |
+
|
38 |
+
return formatted_result, display_image
|
39 |
+
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error processing image: {str(e)}", image
|
42 |
+
|
43 |
+
# Initialize demo
|
44 |
+
demo_model = GreggRecognitionDemo()
|
45 |
+
|
46 |
+
def process_image(image):
|
47 |
+
"""Process uploaded image"""
|
48 |
+
return demo_model.recognize_shorthand(image)
|
49 |
+
|
50 |
+
# Create simple interface
|
51 |
+
demo = gr.Interface(
|
52 |
+
fn=process_image,
|
53 |
+
inputs=gr.Image(type="pil", label="Upload Gregg Shorthand Image"),
|
54 |
+
outputs=[
|
55 |
+
gr.Textbox(label="Recognition Result", lines=3),
|
56 |
+
gr.Image(label="Processed Image")
|
57 |
+
],
|
58 |
+
title="ποΈ Gregg Shorthand Recognition",
|
59 |
+
description="Upload an image of Gregg shorthand notation to convert it to readable text!",
|
60 |
+
article="""
|
61 |
+
### About This Demo
|
62 |
+
This is a demonstration of Gregg shorthand recognition using AI.
|
63 |
+
Gregg shorthand was a popular stenographic writing system used for over a century.
|
64 |
+
|
65 |
+
**Tips for best results:**
|
66 |
+
- Use clear, high-contrast images
|
67 |
+
- Ensure good lighting
|
68 |
+
- Crop to focus on the shorthand text
|
69 |
+
|
70 |
+
For more information, visit the [GitHub repository](https://github.com/a0a7/GreggRecognition).
|
71 |
+
""",
|
72 |
+
examples=None,
|
73 |
+
cache_examples=False
|
74 |
+
)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
demo.launch()
|
requirements.txt
CHANGED
@@ -1,7 +1,2 @@
|
|
1 |
-
gradio==4.
|
2 |
-
torch>=1.9.0
|
3 |
-
torchvision>=0.10.0
|
4 |
Pillow>=8.0.0
|
5 |
-
numpy>=1.21.0
|
6 |
-
huggingface_hub>=0.16.0
|
7 |
-
transformers>=4.21.0
|
|
|
1 |
+
gradio==4.36.0
|
|
|
|
|
2 |
Pillow>=8.0.0
|
|
|
|
|
|