Spaces:
Running
Running
File size: 5,664 Bytes
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 |
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
# Function to create a download link for a PDF
def generate_pdf(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(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(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="Menu to Braille Converter") as demo:
gr.Markdown("# Menu to Braille Converter")
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()
|