shaheerawan3 commited on
Commit
18012b9
·
verified ·
1 Parent(s): 34dc4b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -24,29 +24,25 @@ from io import BytesIO
24
 
25
  class ImageScraper:
26
  def __init__(self):
27
- self.headers = {
28
- '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'
29
- }
30
 
31
  def get_images(self, query: str, num_images: int = 15) -> List[str]:
32
  try:
33
- # Format query for tech-related images
34
  search_query = f"{query} digital technology security"
35
- url = f"https://www.bing.com/images/search?q={search_query.replace(' ', '+')}&first=1"
36
 
37
- response = requests.get(url, headers=self.headers)
38
- soup = BeautifulSoup(response.content, 'html.parser')
39
 
40
- urls = []
41
- for img in soup.find_all('img', class_='mimg'):
42
- src = img.get('src')
43
- if src and src.startswith('http'):
44
- urls.append(src)
45
-
46
- return urls[:num_images]
47
 
48
  except Exception as e:
49
- print(f"Image scraping error: {e}")
50
  return []
51
 
52
  def scrape_pexels(self, query: str) -> List[str]:
 
24
 
25
  class ImageScraper:
26
  def __init__(self):
27
+ # Get your free API key from https://pixabay.com/api/docs/
28
+ self.PIXABAY_API_KEY = "48069976-37e20099248207cee12385560" # Replace with your key
 
29
 
30
  def get_images(self, query: str, num_images: int = 15) -> List[str]:
31
  try:
32
+ # Add relevant keywords for tech/security images
33
  search_query = f"{query} digital technology security"
 
34
 
35
+ url = f"https://pixabay.com/api/?key={self.PIXABAY_API_KEY}&q={search_query.replace(' ', '+')}&image_type=photo&per_page={num_images}"
 
36
 
37
+ response = requests.get(url)
38
+ data = response.json()
39
+
40
+ if 'hits' in data:
41
+ return [img['largeImageURL'] for img in data['hits']]
42
+ return []
 
43
 
44
  except Exception as e:
45
+ print(f"Pixabay API error: {e}")
46
  return []
47
 
48
  def scrape_pexels(self, query: str) -> List[str]: