Spaces:
Sleeping
Sleeping
| import os | |
| import pytesseract | |
| from PIL import Image | |
| from deep_translator import GoogleTranslator, MyMemoryTranslator | |
| # --- OCR MODULE --- | |
| def perform_ocr(image): | |
| """ | |
| Primary: Tesseract OCR (Local) | |
| Backup: Mock/Placeholder (Since Google Vision requires credentials) | |
| """ | |
| try: | |
| # PRIMARY: Tesseract | |
| # Convert image to RGB if necessary | |
| if image.mode != 'RGB': | |
| image = image.convert('RGB') | |
| text = pytesseract.image_to_string(image) | |
| if not text.strip(): | |
| # If Tesseract runs but finds nothing, try backup | |
| raise ValueError("Empty result from Tesseract") | |
| return text, "Primary (Tesseract)" | |
| except Exception as e: | |
| print(f"Primary OCR Failed: {e}") | |
| # BACKUP: Fallback logic (Simulated for this demo) | |
| return "Fallback Mode: Unable to read text clearly. (Mock Data: Burger $10, Pizza $12)", "Backup (Simulated)" | |
| # --- TRANSLATION MODULE --- | |
| def translate_text(text, target_lang='es'): | |
| """ | |
| Primary: Google Translate (via deep_translator wrapper) | |
| Backup: MyMemory Translator | |
| """ | |
| if not text or text.strip() == "": | |
| return "", "No Text" | |
| try: | |
| # PRIMARY | |
| translator = GoogleTranslator(source='auto', target=target_lang) | |
| translation = translator.translate(text) | |
| return translation, "Primary (Google)" | |
| except Exception as e: | |
| print(f"Primary Translation Failed: {e}") | |
| try: | |
| # BACKUP | |
| translator = MyMemoryTranslator(source='auto', target=target_lang) | |
| translation = translator.translate(text) | |
| return translation, "Backup (MyMemory)" | |
| except Exception as e2: | |
| return text, "Failed (No API available)" |