import gradio as gr import gradio as gr import requests from bs4 import BeautifulSoup def search_images_ddg(search_term): url = f"https://duckduckgo.com/?q={search_term}&t=h_&iar=images&iax=images&ia=images" response = requests.get(url) # 打印DuckDuckGo的响应 print(response.text) soup = BeautifulSoup(response.text, 'html.parser') image_elements = soup.find_all("img", class_="tile--img__img") if not image_elements: return None image_urls = [img["src"] for img in image_elements] return image_urls[0] if image_urls else None def show_images(search_term): url = search_images_ddg(search_term) return url iface = gr.Interface( fn=show_images, inputs=gr.components.Textbox(placeholder="Enter a search term..."), outputs=gr.components.Image(type="filepath", label="Search Results") ) iface.launch()