agent_course_final / image_analyzer.py
George Sergia
Add new tools. Fix bugs
b11304e
raw
history blame
1.33 kB
from typing import Optional
from google import genai
from google.genai import types
import requests
import os
def analyze_image(image_url: str, analysis_prompt: Optional[str] = None) -> str:
"""
Analyze images using Google Gemini API. Creates summary of the image content. Supports various iamge formats including PNG, JPG, JPEG, etc.
Args:
image_url (str): Url path to the image file to anlyze
analysis_prompt (Optional[str]): Optional prompt for specific analysis focus
Returns:
str: Text containing analysis results
"""
try:
# Initialize Google Gen client
gemini_llm = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
print(f"Analyzing image from URL {image_url}")
text=analysis_prompt or "Provide a detailed description of this image."
image_bytes = requests.get(image_url).content
image = types.Part.from_bytes(data=image_bytes, mime_type="image/png")
# Get response from Gemini Flash 2.0 Vision
response = gemini_llm.models.generate_content(
model="gemini-2.0-flash",
contents=[text, image],
)
print(response.text)
return response.text
except Exception as e:
return {"error": f"Error analyzing image: {str(e)}"}