File size: 1,179 Bytes
5675d05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# image analyzer using open ai model

from smolagents import tool
from openai import OpenAI
import dotenv
dotenv.load_dotenv()

@tool
def analyze_image(question: str, image_url: str) -> str:
    """

    Analyze an image using OpenAI's API.

    Args:

        question (str): The question to ask about the image. eg. "What is in this image?"

        image_url (str): The URL of the image to analyze.

    """
    client = OpenAI()

    response = client.responses.create(
        model="gpt-4o-mini",
        input=[
            {
                "role": "user",
                "content": [
                    { "type": "input_text", "text": f"{question}" },
                    {
                        "type": "input_image",
                        "image_url": f"{image_url}",
                    }
                ]
            }
        ]
    )

    return response

if __name__ == "__main__":
    question = "What is the main subject of this image?"
    image_url = "https://agents-course-unit4-scoring.hf.space/files/cca530fc-4052-43b2-b130-b30968d8aa44"
    answer = analyze_image(question, image_url)
    print(f"Answer: {answer}")