Spaces:
Running
Running
from serpapi.google_search import GoogleSearch | |
from dotenv import load_dotenv | |
import os | |
from openai import OpenAI | |
import asyncio | |
import requests | |
from agents import enable_verbose_stdout_logging | |
enable_verbose_stdout_logging() | |
from agents import ( | |
Agent, | |
function_tool, | |
Runner, | |
WebSearchTool, | |
) | |
load_dotenv() | |
SERP_API_KEY = os.getenv("SERP_API_KEY") | |
openai_client = OpenAI() | |
def describe_all_images(images_results): | |
descriptions = [] | |
for image in images_results: | |
description = describe_thumbnail(image["thumbnail"]) | |
descriptions.append({ | |
"title": image["title"], | |
"link": image["link"], | |
"description": description | |
}) | |
break | |
return descriptions | |
def describe_thumbnail(image_url): | |
response = openai_client.responses.create( | |
model="gpt-4.1", | |
input=[{ | |
"role": "user", | |
"content": [ | |
{"type": "input_text", "text": "what's in this image? To what era/year does it belong?"}, | |
{ | |
"type": "input_image", | |
"image_url": image_url, | |
}, | |
], | |
}], | |
) | |
return response.output_text + "\n" | |
def search_google(item_to_find: str): | |
params = { | |
"engine": "google_images_light", | |
"q": item_to_find, | |
"api_key": SERP_API_KEY | |
} | |
search = GoogleSearch(params) | |
raw_results = search.get_dict()["images_results"] | |
images_results = [ | |
{ | |
"thumbnail": result["thumbnail"], | |
"link": result["link"], | |
"title": result["title"] | |
} | |
for result in raw_results | |
] | |
return raw_results | |
def seach_google_for_images(item_to_find: str) -> list: | |
""" | |
Search Google Images for a given item and return a list of image results, with links and descriptions. | |
Args: | |
item_to_find (str): The item or query to search for in Google Images. | |
Returns: | |
list: A list of dictionaries, each containing the image title, link, and a description generated by the image analysis. | |
""" | |
raw_results = search_google(item_to_find) | |
results_w_description = describe_all_images(raw_results) | |
return results_w_description | |
def wikipedia_lookup(query): | |
""" | |
Look up a query on Wikipedia and return the summary extract of the most relevant page. | |
Args: | |
query (str): The search term to look up on Wikipedia. | |
Returns: | |
str: The summary extract of the most relevant Wikipedia page, or a message if no page is found. | |
""" | |
# Step 1: Search | |
search_url = "https://en.wikipedia.org/w/rest.php/v1/search/title" | |
params = {"q": query, "limit": 1} | |
search_resp = requests.get(search_url, params=params) | |
search_resp.raise_for_status() | |
search_data = search_resp.json() | |
if not search_data.get("pages"): | |
return f"No Wikipedia page found for '{query}'" | |
page_key = search_data["pages"][0]["key"] | |
# Step 2: Fetch summary | |
summary_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{page_key}" | |
summary_resp = requests.get(summary_url) | |
summary_resp.raise_for_status() | |
return summary_resp.json().get("extract") | |
# if __name__ == "__main__": | |
# item_to_find = "mediterranean houses in the 16h century" | |
# results = search_google(item_to_find) | |
# desc = describe_all_images(results) | |
# print(desc[0]) | |
# # print(wikipedia_lookup("United States Constitution")) | |