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

Update app.py

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