szili2011 commited on
Commit
ac2e5b4
·
verified ·
1 Parent(s): 19bf737

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -3,27 +3,36 @@ import requests
3
  from bs4 import BeautifulSoup
4
 
5
  def search_image(query):
6
- # Use a search engine to find images
7
  url = f"https://www.google.com/search?hl=en&tbm=isch&q={query}"
8
- response = requests.get(url)
 
 
 
9
 
10
- # Parse the HTML
11
  soup = BeautifulSoup(response.text, 'html.parser')
12
 
13
- # Find the image URLs
14
  image_tags = soup.find_all("img")
15
 
16
- # Get the URLs of the images (skip the first one, as it's a placeholder)
17
- image_urls = [img['src'] for img in image_tags[1:6]] # Get the first 5 images
18
- return image_urls
 
 
 
 
 
 
19
 
20
  # Create the Gradio interface
21
  iface = gr.Interface(
22
  fn=search_image,
23
  inputs=gr.Textbox(placeholder="Type something to search for..."),
24
  outputs=gr.Gallery(label="Search Results"),
25
- title="Image Search",
26
- description="Type in a search term to find images."
27
  )
28
 
29
  # Launch the app
 
3
  from bs4 import BeautifulSoup
4
 
5
  def search_image(query):
6
+ # Format the search URL for images
7
  url = f"https://www.google.com/search?hl=en&tbm=isch&q={query}"
8
+ headers = {
9
+ "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"
10
+ }
11
+ response = requests.get(url, headers=headers)
12
 
13
+ # Parse the HTML response
14
  soup = BeautifulSoup(response.text, 'html.parser')
15
 
16
+ # Find all image tags and their data attributes for high-quality images
17
  image_tags = soup.find_all("img")
18
 
19
+ # Extract URLs; for Google Images, we need to look for larger images
20
+ image_urls = []
21
+ for img in image_tags[1:]:
22
+ img_url = img.get("src")
23
+ if img_url and "http" in img_url:
24
+ image_urls.append(img_url)
25
+
26
+ # Return the first 5 high-quality images
27
+ return image_urls[:5]
28
 
29
  # Create the Gradio interface
30
  iface = gr.Interface(
31
  fn=search_image,
32
  inputs=gr.Textbox(placeholder="Type something to search for..."),
33
  outputs=gr.Gallery(label="Search Results"),
34
+ title="HD Image Search",
35
+ description="Type in a search term to find HD images."
36
  )
37
 
38
  # Launch the app