Spaces:
Sleeping
Sleeping
File size: 2,774 Bytes
7bbce45 2b3583b 9a894d2 d10bbd6 9a894d2 a4a096c 42a1748 9a894d2 42a1748 9a894d2 358038a 9a894d2 42a1748 d10bbd6 9a894d2 59a00f5 d723026 9a894d2 0623b1b c7d77fa 9a894d2 0623b1b d10bbd6 7bbce45 9a894d2 0623b1b 9a894d2 |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import gradio as gr
from smolagents import CodeAgent,HfApiModel, DuckDuckGoSearchTool, tool
from smolagents.agents import ActionStep
from time import sleep
import helium
from PIL import Image
from io import BytesIO
import os
# Define a function to search images and return the image path for display
@tool
def search_images_for_presentation(query: str) -> str:
"""
Searches for images related to the given query on the web (using DuckDuckGo search).
Args:
query: The query to search for images or logos.
"""
helium.go_to("https://duckduckgo.com/")
search_box = helium.find(S("input[type='text']"))
search_box.write(query)
search_box.press(Keys.ENTER)
# Wait for search results to load
sleep(3)
# Scroll down and search for images related to the query
search_item_ctrl_f("image", nth_result=1)
# Simulate an image being found (as example)
image_path = "path/to/some_image.png" # Replace with actual image capture logic
return image_path
@tool
def save_image_from_browser(image_url: str) -> str:
"""
Saves the image from the given URL to a file.
Args:
image_url: The URL of the image to be saved.
Returns:
str: The path to the saved image.
"""
image_path = f"/path/to/save/directory/{image_url.split('/')[-1]}" # Example: use image's filename from URL
# Add image download and save logic here
return image_path
# Initialize agent (same as before)
def initialize_agent(model):
return CodeAgent(
tools=[DuckDuckGoSearchTool(), save_image_from_browser, search_images_for_presentation],
model=model,
max_steps=20,
verbosity_level=2,
)
find_images_for_presentation_prompt = "Find images related to presentations, such as slides, logos, or visuals. Search for the following: "
def run_agent(query: str):
model = HfApiModel(model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/')
agent = initialize_agent(model)
result = agent.run(find_images_for_presentation_prompt + query)
return result
# Gradio interface to show images
def gradio_interface(query: str):
# Call the agent to run the query and return the image
image_paths = run_agent(query)
images = [Image.open(image_path) for image_path in image_paths]
# Return the image to Gradio for display
return images
# Set up Gradio interface
gr.Interface(
fn=gradio_interface,
inputs="text", # Text input for the search query
outputs=gr.Gallery(label="Found Images").style(grid=3), # Image output to display the fetched image
title="Figure, Image & Logo Finder",
description="Enter a query to search for relevant images, logos, or figures for your presentation."
).launch()
|