Soumik555 commited on
Commit
00262e5
·
1 Parent(s): 8a7f2d8

added gemini too

Browse files
Files changed (1) hide show
  1. gemini_report_generator.py +52 -39
gemini_report_generator.py CHANGED
@@ -286,55 +286,68 @@ def gemini_llm_chat(csv_url: str, query: str) -> Dict[str, Any]:
286
  }
287
 
288
 
289
- async def generate_csv_report(csv_url: str, query: str) -> FileBoxProps:
290
  try:
291
  result = gemini_llm_chat(csv_url, query)
292
- logger.info(f"Report generated successfully: {result}")
293
 
294
- # Prepare lists for files
295
  csv_files = []
296
  image_files = []
297
 
298
- # Assuming result contains 'csv_files' and 'image_files' keys
299
- if 'csv_files' in result:
300
- for csv_file in result['csv_files']:
301
- # Save CSV to Supabase
302
- public_url = await upload_file_to_supabase(
303
- file_path=csv_file['local_path'],
304
- file_name=csv_file['name']
305
- )
306
- csv_files.append(FileProps(
307
- fileName=csv_file['name'],
308
- filePath=public_url,
309
- fileType="csv"
310
- ))
311
- # Clean up local file
312
- os.remove(csv_file['local_path'])
313
-
314
- if 'image_files' in result:
315
- for img_file in result['image_files']:
316
- # Save image to Supabase
317
- public_url = await upload_file_to_supabase(
318
- file_path=img_file['local_path'],
319
- file_name=img_file['name']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
  )
321
- image_files.append(FileProps(
322
- fileName=img_file['name'],
323
- filePath=public_url,
324
- fileType="image"
325
- ))
326
- # Clean up local file
327
- os.remove(img_file['local_path'])
328
-
329
- return FileBoxProps(
330
- files=Files(
331
- csv_files=csv_files,
332
- image_files=image_files
333
  )
334
- )
335
-
 
336
  except Exception as e:
337
  logger.error(f"Report generation failed: {str(e)}")
 
 
 
338
  return FileBoxProps(
339
  files=Files(
340
  csv_files=[],
 
286
  }
287
 
288
 
289
+ async def generate_report(csv_url: str, query: str) -> FileBoxProps:
290
  try:
291
  result = gemini_llm_chat(csv_url, query)
292
+ logger.info(f"Raw result from gemini_llm_chat: {result}")
293
 
 
294
  csv_files = []
295
  image_files = []
296
 
297
+ # Check if we got the expected response structure
298
+ if isinstance(result, dict) and 'csv_files' in result and 'image_files' in result:
299
+ # Process CSV files
300
+ for csv_path in result['csv_files']:
301
+ if os.path.exists(csv_path):
302
+ file_name = os.path.basename(csv_path)
303
+ try:
304
+ public_url = await upload_file_to_supabase(
305
+ file_path=csv_path,
306
+ file_name=file_name
307
+ )
308
+ csv_files.append(FileProps(
309
+ fileName=file_name,
310
+ filePath=public_url,
311
+ fileType="csv"
312
+ ))
313
+ os.remove(csv_path) # Clean up
314
+ except Exception as upload_error:
315
+ logger.error(f"Failed to upload CSV {file_name}: {str(upload_error)}")
316
+ continue
317
+
318
+ # Process image files
319
+ for img_path in result['image_files']:
320
+ if os.path.exists(img_path):
321
+ file_name = os.path.basename(img_path)
322
+ try:
323
+ public_url = await upload_file_to_supabase(
324
+ file_path=img_path,
325
+ file_name=file_name
326
+ )
327
+ image_files.append(FileProps(
328
+ fileName=file_name,
329
+ filePath=public_url,
330
+ fileType="image"
331
+ ))
332
+ os.remove(img_path) # Clean up
333
+ except Exception as upload_error:
334
+ logger.error(f"Failed to upload image {file_name}: {str(upload_error)}")
335
+ continue
336
+
337
+ return FileBoxProps(
338
+ files=Files(
339
+ csv_files=csv_files,
340
+ image_files=image_files
341
  )
 
 
 
 
 
 
 
 
 
 
 
 
342
  )
343
+ else:
344
+ raise ValueError("Unexpected response format from gemini_llm_chat")
345
+
346
  except Exception as e:
347
  logger.error(f"Report generation failed: {str(e)}")
348
+ # Return empty response but log the files we found
349
+ if 'csv_files' in locals() and 'image_files' in locals():
350
+ logger.info(f"Files that were generated but not processed: CSV: {result.get('csv_files', [])}, Images: {result.get('image_files', [])}")
351
  return FileBoxProps(
352
  files=Files(
353
  csv_files=[],