Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -47,6 +47,64 @@ def process_image(image, use_llm, use_context):
|
|
47 |
# Preprocess the image
|
48 |
preprocessed_img = preprocess_image(image)
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# Extract text using document AI
|
51 |
try:
|
52 |
result = extract_text_and_layout(preprocessed_img)
|
|
|
47 |
# Preprocess the image
|
48 |
preprocessed_img = preprocess_image(image)
|
49 |
|
50 |
+
# Extract text using document AI
|
51 |
+
try:
|
52 |
+
result = extract_text_and_layout(preprocessed_img)
|
53 |
+
|
54 |
+
if not result.get('words', []):
|
55 |
+
return "No text was extracted from the image.", "", "", None
|
56 |
+
|
57 |
+
raw_text = ' '.join(result['words'])
|
58 |
+
|
59 |
+
# Process text with LLM if enabled
|
60 |
+
if use_llm:
|
61 |
+
processed_result = process_menu_text(raw_text)
|
62 |
+
|
63 |
+
if processed_result['success']:
|
64 |
+
processed_text = processed_result['structured_text']
|
65 |
+
else:
|
66 |
+
processed_text = raw_text
|
67 |
+
else:
|
68 |
+
processed_text = raw_text
|
69 |
+
|
70 |
+
# Translate to Braille
|
71 |
+
braille_result = text_to_braille(processed_text, use_context=use_context)
|
72 |
+
|
73 |
+
if not braille_result['success']:
|
74 |
+
return processed_text, "", "Braille translation failed.", None
|
75 |
+
|
76 |
+
braille_text = braille_result['formatted_braille']
|
77 |
+
|
78 |
+
# Generate metadata
|
79 |
+
metadata = get_braille_metadata(processed_text)
|
80 |
+
metadata_text = f"Translation contains {metadata['word_count']} words, {metadata['character_count']} characters, {metadata['line_count']} lines."
|
81 |
+
|
82 |
+
# Store both Unicode and ASCII versions for later use
|
83 |
+
state_data = {
|
84 |
+
'original_text': processed_text,
|
85 |
+
'braille_text': braille_text,
|
86 |
+
'ascii_braille': braille_result.get('formatted_ascii', '')
|
87 |
+
}
|
88 |
+
|
89 |
+
# Return results
|
90 |
+
return processed_text, braille_text, metadata_text, state_data
|
91 |
+
|
92 |
+
except Exception as e:
|
93 |
+
return f"Error processing image: {str(e)}", "", "", None
|
94 |
+
|
95 |
+
|
96 |
+
def process_image1(image, use_llm, use_context):
|
97 |
+
"""Process the uploaded image and generate results."""
|
98 |
+
if image is None:
|
99 |
+
return "Please upload an image first.", "", "", None
|
100 |
+
|
101 |
+
# Convert to PIL Image if needed
|
102 |
+
if isinstance(image, np.ndarray):
|
103 |
+
image = Image.fromarray(image)
|
104 |
+
|
105 |
+
# Preprocess the image
|
106 |
+
preprocessed_img = preprocess_image(image)
|
107 |
+
|
108 |
# Extract text using document AI
|
109 |
try:
|
110 |
result = extract_text_and_layout(preprocessed_img)
|