Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,10 +27,9 @@ class ImageScraper:
|
|
| 27 |
self.PIXABAY_API_KEY = "48069976-37e20099248207cee12385560" # Replace with your key
|
| 28 |
self.stock_images = [
|
| 29 |
"https://images.pexels.com/photos/60504/security-protection-anti-virus-software-60504.jpeg",
|
| 30 |
-
|
| 31 |
]
|
| 32 |
-
|
| 33 |
-
|
| 34 |
try:
|
| 35 |
search_terms = [
|
| 36 |
query,
|
|
@@ -39,16 +38,16 @@ class ImageScraper:
|
|
| 39 |
"data protection",
|
| 40 |
"digital technology"
|
| 41 |
]
|
| 42 |
-
|
| 43 |
all_urls = []
|
| 44 |
for term in search_terms:
|
| 45 |
url = f"https://pixabay.com/api/?key={self.PIXABAY_API_KEY}&q={term.replace(' ', '+')}&image_type=photo&per_page=5"
|
| 46 |
response = requests.get(url)
|
| 47 |
data = response.json()
|
| 48 |
-
|
| 49 |
if 'hits' in data:
|
| 50 |
all_urls.extend([img['largeImageURL'] for img in data['hits']])
|
| 51 |
-
|
| 52 |
return list(set(all_urls))[:num_images] if all_urls else self.stock_images
|
| 53 |
|
| 54 |
except Exception as e:
|
|
@@ -59,20 +58,8 @@ class ImageScraper:
|
|
| 59 |
"""Return preset stock images relevant to digital security and technology"""
|
| 60 |
return [
|
| 61 |
"https://images.pexels.com/photos/60504/security-protection-anti-virus-software-60504.jpeg",
|
| 62 |
-
|
| 63 |
-
"https://images.pexels.com/photos/2582937/pexels-photo-2582937.jpeg",
|
| 64 |
-
"https://images.pexels.com/photos/7319074/pexels-photo-7319074.jpeg",
|
| 65 |
-
"https://images.pexels.com/photos/4164418/pexels-photo-4164418.jpeg",
|
| 66 |
-
"https://images.pexels.com/photos/3861969/pexels-photo-3861969.jpeg",
|
| 67 |
-
"https://images.pexels.com/photos/5473298/pexels-photo-5473298.jpeg",
|
| 68 |
-
"https://images.pexels.com/photos/4348401/pexels-photo-4348401.jpeg",
|
| 69 |
-
"https://images.pexels.com/photos/8386440/pexels-photo-8386440.jpeg",
|
| 70 |
-
"https://images.pexels.com/photos/5473950/pexels-photo-5473950.jpeg"
|
| 71 |
]
|
| 72 |
-
|
| 73 |
-
def get_images(self, query: str, num_images: int = 15) -> List[str]:
|
| 74 |
-
# Return preset stock images regardless of query
|
| 75 |
-
return self.get_stock_images()
|
| 76 |
|
| 77 |
def scrape_pexels(self, query: str) -> List[str]:
|
| 78 |
urls = []
|
|
|
|
| 27 |
self.PIXABAY_API_KEY = "48069976-37e20099248207cee12385560" # Replace with your key
|
| 28 |
self.stock_images = [
|
| 29 |
"https://images.pexels.com/photos/60504/security-protection-anti-virus-software-60504.jpeg",
|
|
|
|
| 30 |
]
|
| 31 |
+
|
| 32 |
+
def get_images(self, query: str, num_images: int = 15) -> List[str]:
|
| 33 |
try:
|
| 34 |
search_terms = [
|
| 35 |
query,
|
|
|
|
| 38 |
"data protection",
|
| 39 |
"digital technology"
|
| 40 |
]
|
| 41 |
+
|
| 42 |
all_urls = []
|
| 43 |
for term in search_terms:
|
| 44 |
url = f"https://pixabay.com/api/?key={self.PIXABAY_API_KEY}&q={term.replace(' ', '+')}&image_type=photo&per_page=5"
|
| 45 |
response = requests.get(url)
|
| 46 |
data = response.json()
|
| 47 |
+
|
| 48 |
if 'hits' in data:
|
| 49 |
all_urls.extend([img['largeImageURL'] for img in data['hits']])
|
| 50 |
+
|
| 51 |
return list(set(all_urls))[:num_images] if all_urls else self.stock_images
|
| 52 |
|
| 53 |
except Exception as e:
|
|
|
|
| 58 |
"""Return preset stock images relevant to digital security and technology"""
|
| 59 |
return [
|
| 60 |
"https://images.pexels.com/photos/60504/security-protection-anti-virus-software-60504.jpeg",
|
| 61 |
+
# Add other URLs here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def scrape_pexels(self, query: str) -> List[str]:
|
| 65 |
urls = []
|