Spaces:
Sleeping
Sleeping
Create app_1.py
Browse files
app_1.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, tool
|
3 |
+
from smolagents.agents import ActionStep
|
4 |
+
from time import sleep
|
5 |
+
import helium
|
6 |
+
from helium import S, Keys
|
7 |
+
import requests
|
8 |
+
from bs4 import BeautifulSoup
|
9 |
+
|
10 |
+
|
11 |
+
# Define a function to extract image URLs from DuckDuckGo search
|
12 |
+
@tool
|
13 |
+
def search_images_for_presentation(query: str):
|
14 |
+
"""
|
15 |
+
Searches for images related to the given query on DuckDuckGo and returns a list of image URLs.
|
16 |
+
"""
|
17 |
+
helium.go_to("https://duckduckgo.com/")
|
18 |
+
search_box = helium.find(S("input[type='text']"))
|
19 |
+
search_box.write(query + " image")
|
20 |
+
search_box.press(Keys.ENTER)
|
21 |
+
|
22 |
+
# Wait for search results to load
|
23 |
+
sleep(3)
|
24 |
+
|
25 |
+
# Extract image links from the page
|
26 |
+
soup = BeautifulSoup(helium.get_driver().page_source, "html.parser")
|
27 |
+
images = [img["src"] for img in soup.find_all("img") if "src" in img.attrs]
|
28 |
+
|
29 |
+
return images[:5] # Return the top 5 image URLs
|
30 |
+
|
31 |
+
|
32 |
+
# Initialize agent
|
33 |
+
def initialize_agent(model):
|
34 |
+
return CodeAgent(
|
35 |
+
tools=[DuckDuckGoSearchTool(), search_images_for_presentation],
|
36 |
+
model=model,
|
37 |
+
max_steps=20,
|
38 |
+
verbosity_level=2,
|
39 |
+
)
|
40 |
+
|
41 |
+
|
42 |
+
# Function to run the agent and return image links
|
43 |
+
def run_agent(query: str):
|
44 |
+
model = HfApiModel(model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/")
|
45 |
+
agent = initialize_agent(model)
|
46 |
+
image_urls = agent.run(query)
|
47 |
+
return image_urls
|
48 |
+
|
49 |
+
|
50 |
+
# Gradio interface function
|
51 |
+
def gradio_interface(query: str):
|
52 |
+
image_urls = run_agent(query)
|
53 |
+
|
54 |
+
# Format output as clickable links
|
55 |
+
links = [f'<a href="{url}" target="_blank">{url}</a>' for url in image_urls]
|
56 |
+
return "\n".join(links)
|
57 |
+
|
58 |
+
|
59 |
+
# Set up Gradio interface
|
60 |
+
gr.Interface(
|
61 |
+
fn=gradio_interface,
|
62 |
+
inputs="text",
|
63 |
+
outputs="html",
|
64 |
+
title="Figure, Image & Logo Finder",
|
65 |
+
description="Enter a query to search for relevant images, logos, or figures for your presentation.",
|
66 |
+
).launch()
|