Spaces:
No application file
No application file
Update App.py
Browse files
App.py
CHANGED
|
@@ -1,8 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
iface = gr.Interface(
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
from bs4 import BeautifulSoup
|
| 6 |
+
|
| 7 |
+
def search_images_ddg(search_term):
|
| 8 |
+
url = f"https://duckduckgo.com/?q={search_term}&t=h_&iar=images&iax=images&ia=images"
|
| 9 |
+
response = requests.get(url)
|
| 10 |
+
# 打印DuckDuckGo的响应
|
| 11 |
+
print(response.text)
|
| 12 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 13 |
+
image_elements = soup.find_all("img", class_="tile--img__img")
|
| 14 |
+
if not image_elements:
|
| 15 |
+
return None
|
| 16 |
+
image_urls = [img["src"] for img in image_elements]
|
| 17 |
+
return image_urls[0] if image_urls else None
|
| 18 |
+
|
| 19 |
+
def show_images(search_term):
|
| 20 |
+
url = search_images_ddg(search_term)
|
| 21 |
+
return url
|
| 22 |
|
| 23 |
+
iface = gr.Interface(
|
| 24 |
+
fn=show_images,
|
| 25 |
+
inputs=gr.components.Textbox(placeholder="Enter a search term..."),
|
| 26 |
+
outputs=gr.components.Image(type="filepath", label="Search Results")
|
| 27 |
+
)
|
| 28 |
|
| 29 |
+
iface.launch()
|