File size: 1,250 Bytes
bf98816 f565598 bf98816 f565598 bf98816 ac2e5b4 f565598 ac2e5b4 bf98816 f565598 bf98816 f565598 ac2e5b4 f565598 ac2e5b4 f565598 ac2e5b4 bf98816 f565598 ac2e5b4 bf98816 |
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 |
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()
|