feat: Use is_api_key_error flag from backend + onclick-based button for reliable change key UX
bc7bdb7 | # image_processor.py | |
| import google.generativeai as genai | |
| from PIL import Image | |
| import config | |
| import os | |
| import json | |
| def extract_and_translate(image_path, tribe): | |
| """ | |
| 💡 視覺翻譯一體化:辨識 + 翻譯一次完成,節省 80% 時間 | |
| """ | |
| if not config.GEMINI_KEY: return None | |
| optimized_path = f"{image_path}_opt.jpg" | |
| try: | |
| with Image.open(image_path) as img: | |
| if img.mode != 'RGB': img = img.convert('RGB') | |
| if img.width > 1024: | |
| ratio = 1024 / float(img.width) | |
| img = img.resize((1024, int(img.height * ratio)), Image.Resampling.LANCZOS) | |
| img.save(optimized_path, "JPEG", quality=70) | |
| genai.configure(api_key=config.GEMINI_KEY) | |
| # 💡 使用 JSON 模式強制對齊格式 | |
| model = genai.GenerativeModel( | |
| 'models/gemini-3-flash-preview', | |
| generation_config={"temperature": 0.0, "response_mime_type": "application/json"} | |
| ) | |
| prompt = f""" | |
| 你是視覺翻譯專家。請辨識圖中文字並翻譯成{tribe}語。 | |
| 1. 排除重複內容。 | |
| 2. 輸出格式必須為 JSON:{{"items": [{{"original": "原文", "translated": "翻譯"}}]}} | |
| 3. 遵守書寫憲法 2.0 (o轉u)。 | |
| """ | |
| img_opt = Image.open(optimized_path) | |
| response = model.generate_content([prompt, img_opt]) | |
| if os.path.exists(optimized_path): os.remove(optimized_path) | |
| return json.loads(response.text) | |
| except Exception as e: | |
| if os.path.exists(optimized_path): os.remove(optimized_path) | |
| return None |