CoolFace
Apppublic

npmaker/Final_Assignment

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
gemini_image_tool.py86 linesDownload Raw Back to root
1from google import genai2from google.genai import types3from smolagents.tools import Tool4import requests5import os6from dotenv import load_dotenv7from typing import Optional8 9load_dotenv()10 11class GeminiImageTool(Tool):12    """13    A tool that uses Google's Gemini AI model to analyze and respond to queries about images.14    15    This tool takes an image URL and a query about the image, sends them to the Gemini model,16    and returns the model's analysis or response.17    """18    19    name = "google_image_analysis"20    description = """Analyzes images using Google's Gemini AI model and answers questions 21                    about the image content. Provide an image URL and specify what you want22                    to know about the image."""23    inputs = {24        "query": {"type": "string", "description": "The question or instruction about what to analyze in the image"},25        "url": {"type": "string", "description": "URL path to the image file to be analyzed"}26    }27    output_type = "string"28    is_initialized = False29 30    def __init__(self, api_key: Optional[str] = None):31        """32        Initialize the GeminiImageTool with API credentials.33        34        Args:35            api_key: Optional Google API key. If not provided, will try to load from environment variables.36        """37        api_key = api_key or os.getenv("GOOGLE_API_KEY")38        if not api_key:39            raise ValueError("Google API key is required. Provide it directly or set GOOGLE_API_KEY environment variable.")40        41        self.client = genai.Client(api_key=api_key)42        self.model_name = "gemini-2.0-flash-exp"43 44    def forward(self, query: str, url: str) -> str:45        """46        Analyze an image according to the specified query.47        48        This method downloads the image from the provided URL, sends it along with49        the query to the Gemini model, and returns the model's response.50        51        Args:52            query: The question or instruction about what to analyze in the image53            url: URL path to the image file to be analyzed54            55        Returns:56            The text response from the Gemini model's analysis57            58        Raises:59            requests.RequestException: If there's an issue downloading the image60            ValueError: If the image cannot be processed61        """62        try:63            # Download the image64            response = requests.get(url, timeout=10)65            response.raise_for_status()  # Raise exception for bad responses66            image_bytes = response.content67            68            # Create an image part for the model69            image = types.Part.from_bytes(70                data=image_bytes, 71                mime_type=response.headers.get('Content-Type', 'image/jpeg')72            )73            74            # Generate content from the model75            model_response = self.client.models.generate_content(76                model=self.model_name,77                contents=[query, image],78            )79            80            return model_response.text81            82        except requests.RequestException as e:83            raise ValueError(f"Failed to download image from URL: {str(e)}")84        except Exception as e:85            raise ValueError(f"Error processing image with Gemini: {str(e)}")86