Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,29 +24,25 @@ from io import BytesIO
|
|
| 24 |
|
| 25 |
class ImageScraper:
|
| 26 |
def __init__(self):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
}
|
| 30 |
|
| 31 |
def get_images(self, query: str, num_images: int = 15) -> List[str]:
|
| 32 |
try:
|
| 33 |
-
#
|
| 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 |
-
|
| 38 |
-
soup = BeautifulSoup(response.content, 'html.parser')
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return urls[:num_images]
|
| 47 |
|
| 48 |
except Exception as e:
|
| 49 |
-
print(f"
|
| 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]:
|