PicFinder / app.py
szili2011's picture
Update app.py
f565598 verified
import gradio as gr
import requests
from bs4 import BeautifulSoup
import re
def search_image(query):
# Create the URL for the image search
url = f"https://www.google.com/search?hl=en&tbm=isch&q={query}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(url, headers=headers)
# Parse the HTML response
soup = BeautifulSoup(response.text, 'html.parser')
# Find image URLs from the <img> tags
image_tags = soup.find_all("img")
# Extract the src URLs of the images
image_urls = []
for img in image_tags:
img_url = img.get("src")
if img_url and img_url.startswith("http"):
# Append image URL to the list
image_urls.append(img_url)
# Return the first 5 high-quality images
return image_urls[:5]
# Create the Gradio interface
iface = gr.Interface(
fn=search_image,
inputs=gr.Textbox(placeholder="Type something to search for..."),
outputs=gr.Gallery(label="Search Results", show_label=True),
title="HD Image Search",
description="Type in a search term to find HD images."
)
# Launch the app
iface.launch()