Update app.py
Browse files
app.py
CHANGED
|
@@ -57,7 +57,30 @@ def get_default_font(size=20):
|
|
| 57 |
|
| 58 |
def translate_text(text, target_lang):
|
| 59 |
"""Translate text with error handling and caching"""
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
def get_dominant_color(image, bbox, padding=4):
|
| 63 |
"""Extract the dominant color around text for better background matching"""
|
|
@@ -224,7 +247,53 @@ def process_image(image, target_lang, overlay_opacity=0.7, progress=gr.Progress(
|
|
| 224 |
|
| 225 |
# Create Gradio interface
|
| 226 |
with gr.Blocks(title="Enhanced Image Translator") as iface:
|
| 227 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
if __name__ == "__main__":
|
| 230 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def translate_text(text, target_lang):
|
| 59 |
"""Translate text with error handling and caching"""
|
| 60 |
+
if not text or not text.strip():
|
| 61 |
+
return ""
|
| 62 |
+
|
| 63 |
+
# Use cache if available
|
| 64 |
+
cache_key = f"{text}|{target_lang}"
|
| 65 |
+
if cache_key in translation_cache:
|
| 66 |
+
return translation_cache[cache_key]
|
| 67 |
+
|
| 68 |
+
# Handle translation with retries
|
| 69 |
+
max_retries = 3
|
| 70 |
+
for attempt in range(max_retries):
|
| 71 |
+
try:
|
| 72 |
+
translated = GoogleTranslator(source='auto', target=target_lang).translate(text)
|
| 73 |
+
if translated:
|
| 74 |
+
translation_cache[cache_key] = translated
|
| 75 |
+
return translated
|
| 76 |
+
time.sleep(1) # Short delay before retry
|
| 77 |
+
except Exception as e:
|
| 78 |
+
print(f"Translation error (attempt {attempt+1}): {e}")
|
| 79 |
+
if attempt == max_retries - 1:
|
| 80 |
+
return f"[Translation Error: {text}]"
|
| 81 |
+
time.sleep(1) # Wait before retry
|
| 82 |
+
|
| 83 |
+
return f"[Unable to translate: {text}]"
|
| 84 |
|
| 85 |
def get_dominant_color(image, bbox, padding=4):
|
| 86 |
"""Extract the dominant color around text for better background matching"""
|
|
|
|
| 247 |
|
| 248 |
# Create Gradio interface
|
| 249 |
with gr.Blocks(title="Enhanced Image Translator") as iface:
|
| 250 |
+
gr.Markdown("# Enhanced Image Translator")
|
| 251 |
+
gr.Markdown("Extract & translate text from images with improved overlay visualization")
|
| 252 |
+
|
| 253 |
+
with gr.Row():
|
| 254 |
+
with gr.Column():
|
| 255 |
+
input_image = gr.Image(type="pil", label="Upload Image")
|
| 256 |
+
|
| 257 |
+
with gr.Row():
|
| 258 |
+
target_lang = gr.Dropdown(
|
| 259 |
+
choices=list(SUPPORTED_LANGUAGES.values()),
|
| 260 |
+
value="Hindi",
|
| 261 |
+
label="Translate To"
|
| 262 |
+
)
|
| 263 |
+
overlay_opacity = gr.Slider(
|
| 264 |
+
minimum=0.1,
|
| 265 |
+
maximum=1.0,
|
| 266 |
+
value=0.7,
|
| 267 |
+
step=0.1,
|
| 268 |
+
label="Overlay Opacity"
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
translate_btn = gr.Button("Translate", variant="primary")
|
| 272 |
+
|
| 273 |
+
with gr.Column():
|
| 274 |
+
output_image = gr.Image(type="pil", label="Image with Translated Overlay")
|
| 275 |
+
output_text = gr.Textbox(label="Translated Text Output", lines=10)
|
| 276 |
+
|
| 277 |
+
# Connect the button to the processing function
|
| 278 |
+
translate_btn.click(
|
| 279 |
+
fn=process_image,
|
| 280 |
+
inputs=[input_image, target_lang, overlay_opacity],
|
| 281 |
+
outputs=[output_image, output_text]
|
| 282 |
+
)
|
| 283 |
+
|
| 284 |
+
gr.Markdown("""
|
| 285 |
+
## Features
|
| 286 |
+
- Supports multiple languages for translation
|
| 287 |
+
- Semi-transparent overlays for better readability
|
| 288 |
+
- Simple and efficient text extraction and translation
|
| 289 |
+
""")
|
| 290 |
|
| 291 |
if __name__ == "__main__":
|
| 292 |
+
# Initialize OCR model at startup to avoid delay on first request
|
| 293 |
+
try:
|
| 294 |
+
initialize_reader()
|
| 295 |
+
except:
|
| 296 |
+
pass
|
| 297 |
+
|
| 298 |
+
# Launch the app
|
| 299 |
+
iface.launch()
|