shaheerawan3 commited on
Commit
5ed82ea
·
verified ·
1 Parent(s): f567d4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -20
app.py CHANGED
@@ -201,6 +201,10 @@ class ImageScraper:
201
  ]
202
 
203
  def get_images(self, query: str, num_images: int = 15) -> Dict[str, List[Dict[str, str]]]:
 
 
 
 
204
  try:
205
  # Ensure query is not empty
206
  if not query.strip():
@@ -216,67 +220,91 @@ class ImageScraper:
216
  'lang': 'en'
217
  }
218
 
219
- print(f"Querying Pixabay API with params: {params}") # Debug log
220
- response = requests.get(base_url, params=params)
221
- print(f"API Response status: {response.status_code}") # Debug log
222
 
223
  if response.status_code == 200:
224
  data = response.json()
225
  hits = data.get('hits', [])
226
- print(f"Found {len(hits)} images") # Debug log
227
 
228
  if not hits:
229
  # If no results, try backup keywords
230
  backup_keywords = ["digital security", "technology", "cyber security", "business technology"]
231
  for keyword in backup_keywords:
232
  params['q'] = keyword
233
- backup_response = requests.get(base_url, params=params)
234
  if backup_response.status_code == 200:
235
  backup_data = backup_response.json()
236
  hits.extend(backup_data.get('hits', []))
237
  if hits:
238
  break
239
-
240
- # Structure response
241
  result = {
242
  'primary': [],
243
  'secondary': [],
244
  'general': []
245
  }
246
-
247
- # Ensure we have at least some images
248
  if not hits:
249
- result['general'] = [{'url': url, 'keyword': 'technology', 'relevance': 'Fallback', 'tags': 'technology'}
250
- for url in self.get_stock_images()]
 
 
 
 
 
 
 
251
  return result
252
-
253
- # Categorize found images
254
  for hit in hits[:num_images]:
255
  image_data = {
256
  'url': hit['largeImageURL'],
257
  'keyword': query,
258
- 'relevance': 'Primary match',
259
  'tags': hit.get('tags', '')
260
  }
261
 
262
- if len(result['primary']) < num_images // 3:
 
263
  result['primary'].append(image_data)
264
  elif len(result['secondary']) < num_images // 3:
265
  result['secondary'].append(image_data)
266
  else:
267
  result['general'].append(image_data)
268
 
 
 
 
 
 
 
 
 
 
269
  return result
270
 
271
  except Exception as e:
272
- print(f"Error in get_images: {str(e)}") # Debug log
273
  # Return stock images as fallback
 
274
  return {
275
- 'general': [{'url': url, 'keyword': 'technology', 'relevance': 'Fallback', 'tags': 'technology'}
276
- for url in self.get_stock_images()]
 
 
 
 
 
 
277
  }
278
-
279
-
280
  def get_fallback_keywords(self) -> List[Dict[str, str]]:
281
  """Return fallback keywords if AI extraction fails"""
282
  return [
 
201
  ]
202
 
203
  def get_images(self, query: str, num_images: int = 15) -> Dict[str, List[Dict[str, str]]]:
204
+ """
205
+ Get images from Pixabay API with enhanced error handling and categorization
206
+ Returns a dictionary with categorized image results
207
+ """
208
  try:
209
  # Ensure query is not empty
210
  if not query.strip():
 
220
  'lang': 'en'
221
  }
222
 
223
+ print(f"Querying Pixabay API with params: {params}")
224
+ response = requests.get(base_url, params=params, headers=self.headers)
225
+ print(f"API Response status: {response.status_code}")
226
 
227
  if response.status_code == 200:
228
  data = response.json()
229
  hits = data.get('hits', [])
230
+ print(f"Found {len(hits)} images")
231
 
232
  if not hits:
233
  # If no results, try backup keywords
234
  backup_keywords = ["digital security", "technology", "cyber security", "business technology"]
235
  for keyword in backup_keywords:
236
  params['q'] = keyword
237
+ backup_response = requests.get(base_url, params=params, headers=self.headers)
238
  if backup_response.status_code == 200:
239
  backup_data = backup_response.json()
240
  hits.extend(backup_data.get('hits', []))
241
  if hits:
242
  break
243
+
244
+ # Structure response with categories
245
  result = {
246
  'primary': [],
247
  'secondary': [],
248
  'general': []
249
  }
250
+
251
+ # If no hits found even after backup keywords, return stock images
252
  if not hits:
253
+ stock_images = self.get_stock_images()
254
+ result['general'] = [
255
+ {
256
+ 'url': url,
257
+ 'keyword': 'technology',
258
+ 'relevance': 'Fallback',
259
+ 'tags': 'technology'
260
+ } for url in stock_images
261
+ ]
262
  return result
263
+
264
+ # Process and categorize the found images
265
  for hit in hits[:num_images]:
266
  image_data = {
267
  'url': hit['largeImageURL'],
268
  'keyword': query,
269
+ 'relevance': 'Primary match' if query.lower() in hit.get('tags', '').lower() else 'Related',
270
  'tags': hit.get('tags', '')
271
  }
272
 
273
+ # Distribute images across categories
274
+ if len(result['primary']) < num_images // 3 and query.lower() in hit.get('tags', '').lower():
275
  result['primary'].append(image_data)
276
  elif len(result['secondary']) < num_images // 3:
277
  result['secondary'].append(image_data)
278
  else:
279
  result['general'].append(image_data)
280
 
281
+ # Ensure we have some results in each category
282
+ if not result['primary'] and result['secondary']:
283
+ result['primary'] = result['secondary'][:num_images//3]
284
+ result['secondary'] = result['secondary'][num_images//3:]
285
+
286
+ if not result['secondary'] and result['general']:
287
+ result['secondary'] = result['general'][:num_images//3]
288
+ result['general'] = result['general'][num_images//3:]
289
+
290
  return result
291
 
292
  except Exception as e:
293
+ print(f"Error in get_images: {str(e)}")
294
  # Return stock images as fallback
295
+ stock_images = self.get_stock_images()
296
  return {
297
+ 'general': [
298
+ {
299
+ 'url': url,
300
+ 'keyword': 'technology',
301
+ 'relevance': 'Fallback',
302
+ 'tags': 'technology'
303
+ } for url in stock_images
304
+ ]
305
  }
306
+
307
+
308
  def get_fallback_keywords(self) -> List[Dict[str, str]]:
309
  """Return fallback keywords if AI extraction fails"""
310
  return [