Spaces:
Sleeping
Sleeping
File size: 12,309 Bytes
93c4f75 9d0323b 93c4f75 9d0323b 93c4f75 23217eb 93c4f75 97648fc 23217eb 97648fc 93c4f75 bf28af1 23217eb 913eaaf 23217eb 8da2ce6 93c4f75 bf28af1 93c4f75 f53e8ba 93c4f75 c50b116 93c4f75 |
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
import gradio as gr
import numpy as np
from PIL import Image
import io
import base64
# Import our custom modules
from utils.image_preprocessing import preprocess_image
from models.document_ai import extract_text_and_layout
from models.text_processor import process_menu_text
from models.braille_translator import text_to_braille, get_braille_metadata
from utils.pdf_generator import create_braille_pdf, create_braille_pdf_with_comparison
def generate_pdf(original_text, braille_text, title, comparison=False):
"""Generate a PDF file with Braille content."""
try:
if comparison:
pdf_buffer = create_braille_pdf_with_comparison(original_text, braille_text, title)
else:
pdf_buffer = create_braille_pdf(original_text, braille_text, title)
return pdf_buffer
except Exception as e:
print(f"Error in generate_pdf: {str(e)}")
raise
# Function to create a download link for a PDF
def generate_pdf1(original_text, braille_text, title, comparison=False):
"""Generate a PDF file with Braille content."""
if comparison:
pdf_buffer = create_braille_pdf_with_comparison(original_text, braille_text, title)
else:
pdf_buffer = create_braille_pdf(original_text, braille_text, title)
return pdf_buffer
def process_image_v2(image, use_llm, use_context):
"""Process the uploaded image and generate results."""
if image is None:
return "Please upload an image first.", "", "", None
# Convert to PIL Image if needed
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
# Preprocess the image
preprocessed_img = preprocess_image(image)
# Extract text using document AI
try:
result = extract_text_and_layout(preprocessed_img)
if not result.get('words', []):
return "No text was extracted from the image.", "", "", None
raw_text = ' '.join(result['words'])
# Process text with LLM if enabled
if use_llm:
processed_result = process_menu_text(raw_text)
if processed_result['success']:
processed_text = processed_result['structured_text']
else:
processed_text = raw_text
else:
processed_text = raw_text
# Translate to Braille
braille_result = text_to_braille(processed_text, use_context=use_context)
if not braille_result['success']:
return processed_text, "", "Braille translation failed.", None
braille_text = braille_result['formatted_braille']
# Generate metadata
metadata = get_braille_metadata(processed_text)
metadata_text = f"Translation contains {metadata['word_count']} words, {metadata['character_count']} characters, {metadata['line_count']} lines."
# Store both Unicode and ASCII versions for later use
state_data = {
'original_text': processed_text,
'braille_text': braille_text,
'ascii_braille': braille_result.get('formatted_ascii', '')
}
# Return results
return processed_text, braille_text, metadata_text, state_data
except Exception as e:
return f"Error processing image: {str(e)}", "", "", None
def process_image(image, use_llm, use_context):
"""Process the uploaded image and generate results."""
if image is None:
return "Please upload an image first.", "", "", None
# Convert to PIL Image if needed
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
# Preprocess the image
preprocessed_img = preprocess_image(image)
# Extract text using document AI
try:
result = extract_text_and_layout(preprocessed_img)
if not result.get('words', []):
return "No text was extracted from the image.", "", "", None
raw_text = ' '.join(result['words'])
# Process text with LLM if enabled
if use_llm:
processed_result = process_menu_text(raw_text)
if processed_result['success']:
processed_text = processed_result['structured_text']
else:
processed_text = raw_text
else:
processed_text = raw_text
# Translate to Braille
braille_result = text_to_braille(processed_text, use_context=use_context)
if not braille_result['success']:
return processed_text, "", "Braille translation failed.", None
braille_text = braille_result['formatted_braille']
# Generate metadata
metadata = get_braille_metadata(processed_text)
metadata_text = f"Translation contains {metadata['word_count']} words, {metadata['character_count']} characters, {metadata['line_count']} lines."
# Return results
return processed_text, braille_text, metadata_text, (processed_text, braille_text)
except Exception as e:
return f"Error processing image: {str(e)}", "", "", None
def create_pdf_v2(state, pdf_title, pdf_type):
"""Create a PDF file for download."""
if state is None:
return None
# Extract data from state
try:
original_text = state['original_text']
ascii_braille = state['ascii_braille']
# If ASCII version is not available, use the Unicode version
if not ascii_braille:
ascii_braille = state['braille_text']
except:
# Fallback for backward compatibility
if isinstance(state, tuple) and len(state) == 2:
original_text, braille_text = state
ascii_braille = braille_text
else:
return None
comparison = (pdf_type == "Side-by-Side Comparison")
try:
pdf_buffer = generate_pdf(original_text, ascii_braille, pdf_title, comparison)
# Create a temporary file to save the PDF
temp_file_path = f"/tmp/{pdf_title.replace(' ', '_').lower()}.pdf"
# Write the buffer to a file
with open(temp_file_path, "wb") as f:
f.write(pdf_buffer.getvalue())
return temp_file_path
except Exception as e:
print(f"Error generating PDF: {str(e)}")
return None
def create_pdf(state, pdf_title, pdf_type):
"""Create a PDF file for download."""
if state is None:
return None
# Extract data from state
try:
original_text = state['original_text']
braille_text = state['braille_text'] # Use Unicode Braille text
except:
# Fallback for backward compatibility
if isinstance(state, tuple) and len(state) == 2:
original_text, braille_text = state
else:
return None
comparison = (pdf_type == "Side-by-Side Comparison")
try:
pdf_buffer = generate_pdf(original_text, braille_text, pdf_title, comparison)
# Create a temporary file to save the PDF
temp_file_path = f"/tmp/{pdf_title.replace(' ', '_').lower()}.pdf"
# Write the buffer to a file
with open(temp_file_path, "wb") as f:
f.write(pdf_buffer.getvalue())
return temp_file_path
except Exception as e:
print(f"Error generating PDF: {str(e)}")
return None
def create_pdf_v1_working(state, pdf_title, pdf_type):
"""Create a PDF file for download."""
if state is None or len(state) != 2:
return None
original_text, braille_text = state
# Get ASCII representation for PDF
try:
braille_result = text_to_braille(original_text, use_context=False)
ascii_braille = braille_result.get('formatted_ascii', braille_text)
except:
ascii_braille = braille_text
comparison = (pdf_type == "Side-by-Side Comparison")
try:
pdf_buffer = generate_pdf(original_text, ascii_braille, pdf_title, comparison)
# Create a temporary file to save the PDF
temp_file_path = f"/tmp/{pdf_title.replace(' ', '_').lower()}.pdf"
# Write the buffer to a file
with open(temp_file_path, "wb") as f:
f.write(pdf_buffer.getvalue())
return temp_file_path
except Exception as e:
print(f"Error generating PDF: {str(e)}")
return None
def create_pdf2(state, pdf_title, pdf_type):
"""Create a PDF file for download."""
if state is None or len(state) != 2:
return None
original_text, braille_text = state
comparison = (pdf_type == "Side-by-Side Comparison")
try:
pdf_buffer = generate_pdf(original_text, braille_text, pdf_title, comparison)
# Create a temporary file to save the PDF
temp_file_path = f"/tmp/{pdf_title.replace(' ', '_').lower()}.pdf"
# Write the buffer to a file
with open(temp_file_path, "wb") as f:
f.write(pdf_buffer.getvalue())
return temp_file_path
except Exception as e:
print(f"Error generating PDF: {str(e)}")
return None
def create_pdf1(state, pdf_title, pdf_type):
"""Create a PDF file for download."""
if state is None or len(state) != 2:
return None
original_text, braille_text = state
comparison = (pdf_type == "Side-by-Side Comparison")
pdf_buffer = generate_pdf(original_text, braille_text, pdf_title, comparison)
# Return the file for download
return pdf_buffer
# Create the Gradio interface
with gr.Blocks(title="English Menu to Braille Menu Converter") as demo:
gr.Markdown("# English Menu to Braille Menu")
gr.Markdown("Upload a menu image to convert it to Braille text")
with gr.Row():
with gr.Column(scale=1):
# Input components
image_input = gr.Image(type="pil", label="Upload Menu Image")
with gr.Row():
use_llm = gr.Checkbox(label="Use AI for text processing", value=True)
use_context = gr.Checkbox(label="Use AI for context enhancement", value=True)
process_button = gr.Button("Process Menu")
with gr.Column(scale=2):
# Output components
processed_text = gr.Textbox(label="Processed Text", lines=8)
braille_output = gr.Textbox(label="Braille Translation", lines=10)
metadata_output = gr.Markdown()
# Hidden state for PDF generation
state = gr.State()
# PDF download section
with gr.Group():
gr.Markdown("### Download Options")
pdf_title = gr.Textbox(label="PDF Title", value="Menu in Braille")
pdf_type = gr.Radio(
["Sequential (Text then Braille)", "Side-by-Side Comparison"],
label="PDF Format",
value="Sequential (Text then Braille)"
)
pdf_button = gr.Button("Generate PDF")
pdf_output = gr.File(label="Download PDF")
# Set up event handlers
process_button.click(
process_image,
inputs=[image_input, use_llm, use_context],
outputs=[processed_text, braille_output, metadata_output, state]
)
pdf_button.click(
create_pdf,
inputs=[state, pdf_title, pdf_type],
outputs=[pdf_output]
)
# Add examples
gr.Examples(
examples=["assets/sample_menus/menu1.jpg", "assets/sample_menus/menu2.jpg"],
inputs=image_input
)
# Add about section
with gr.Accordion("About", open=False):
gr.Markdown("""
This application converts menu images to Braille text using AI technologies:
- Document AI for text extraction
- LLMs for text processing and enhancement
- Braille translation with formatting
- PDF generation for download
Created as a demonstration of AI-powered accessibility tools.
""")
# Launch the app
if __name__ == "__main__":
demo.launch()
|