Spaces:
Sleeping
Sleeping
| import base64 | |
| from google import genai | |
| from config import GEMINI_API_KEY | |
| import re | |
| client = genai.Client(api_key=GEMINI_API_KEY) | |
| def gemini_vision_answer(image, question, lang="en"): | |
| try: | |
| prompt = f"""Analyze this image carefully. | |
| Question: | |
| {question} | |
| You MUST respond strictly in the language code: {lang}. All text in your output must be translated to '{lang}'. | |
| However, keep the structural English keys intact (Caption, Final Answer, Explanation). | |
| Respond strictly following this exact structure without any deviations or markdown blocks. | |
| Caption: <short caption> | |
| Final Answer: <direct answer> | |
| Explanation: <detailed reasoning>""" | |
| response = client.models.generate_content( | |
| model="gemini-3.1-flash-lite-preview", | |
| contents=[image, prompt] | |
| ) | |
| text = response.text | |
| # Parse response using regex for better stability since LLMs can sometimes alter spacing | |
| caption_match = re.search(r'Caption:\s*(.*?)(?=Final Answer:|$)', text, re.IGNORECASE | re.DOTALL) | |
| answer_match = re.search(r'Final Answer:\s*(.*?)(?=Explanation:|$)', text, re.IGNORECASE | re.DOTALL) | |
| explanation_match = re.search(r'Explanation:\s*(.*)', text, re.IGNORECASE | re.DOTALL) | |
| caption = caption_match.group(1).strip() if caption_match else "Caption not generated correctly." | |
| answer = answer_match.group(1).strip() if answer_match else "Answer not generated correctly." | |
| explanation = explanation_match.group(1).strip() if explanation_match else "Explanation not generated correctly." | |
| # Filter out markdown bolds if they leaked from the LLM prompt completion | |
| caption = caption.replace("**", "") | |
| answer = answer.replace("**", "") | |
| return caption, answer, explanation | |
| except Exception as e: | |
| error_msg = str(e) | |
| if "429" in error_msg or "RESOURCE_EXHAUSTED" in error_msg: | |
| return ( | |
| "Rate Limit Exceeded", | |
| "You have hit the free tier quota limit for Google Gemini.", | |
| "Please wait or check your Google AI Studio dashboard for quota resets." | |
| ) | |
| print("GEMINI ERROR:", error_msg) | |
| return "Gemini Error", "Error", error_msg | |
| def extract_section(text, section_name): | |
| try: | |
| start = text.index(section_name) + len(section_name) | |
| end = text.find("\n", start) | |
| if end == -1: | |
| end = len(text) | |
| return text[start:end].strip() | |
| except: | |
| return "Not found" |