Spaces:
Sleeping
Sleeping
File size: 2,522 Bytes
2c5cb17 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 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" |