chrisjcc commited on
Commit
f91e457
·
1 Parent(s): ad92b4b

Better CSV and DOCX support

Browse files
Files changed (1) hide show
  1. app.py +49 -2
app.py CHANGED
@@ -416,10 +416,18 @@ def query_agent(question: str, files: Optional[List[FilePayload]] = None) -> str
416
  try:
417
  # Import necessary types and libraries inside logic to avoid top-level failures if missing
418
  import io
 
419
  import pypdf
420
  from strands.types.content import ImageContent
421
  from strands.types.media import ImageSource
422
 
 
 
 
 
 
 
 
423
  image_formats = {'png', 'jpeg', 'gif', 'webp', 'jpg'}
424
 
425
  for file_obj in files:
@@ -461,8 +469,47 @@ def query_agent(question: str, files: Optional[List[FilePayload]] = None) -> str
461
  except Exception as pdf_err:
462
  logger.error(f"PDF extraction failed for {file_obj.name}: {pdf_err}")
463
  extracted_text = f"[Error extracting PDF text for {file_obj.name}: {str(pdf_err)}]"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
464
  else:
465
- # Try decoding as plain text (csv, txt, md, html, etc)
466
  try:
467
  extracted_text = file_bytes.decode('utf-8', errors='replace')
468
  except Exception as dec_err:
@@ -477,7 +524,7 @@ def query_agent(question: str, files: Optional[List[FilePayload]] = None) -> str
477
 
478
  except ImportError as ie:
479
  logger.error(f"Missing dependency for file processing: {ie}")
480
- return "Error: Server missing dependencies (pypdf or strands types) for file processing."
481
 
482
  # Construct final payload
483
  message_content = [{"text": combined_text}]
 
416
  try:
417
  # Import necessary types and libraries inside logic to avoid top-level failures if missing
418
  import io
419
+ import csv
420
  import pypdf
421
  from strands.types.content import ImageContent
422
  from strands.types.media import ImageSource
423
 
424
+ # Try import python-docx
425
+ try:
426
+ import docx
427
+ except ImportError:
428
+ docx = None
429
+ logger.warning("python-docx not installed. DOCX support disabled.")
430
+
431
  image_formats = {'png', 'jpeg', 'gif', 'webp', 'jpg'}
432
 
433
  for file_obj in files:
 
469
  except Exception as pdf_err:
470
  logger.error(f"PDF extraction failed for {file_obj.name}: {pdf_err}")
471
  extracted_text = f"[Error extracting PDF text for {file_obj.name}: {str(pdf_err)}]"
472
+
473
+ elif fmt == 'docx':
474
+ if docx:
475
+ try:
476
+ doc = docx.Document(io.BytesIO(file_bytes))
477
+ full_text = []
478
+ for para in doc.paragraphs:
479
+ full_text.append(para.text)
480
+ extracted_text = '\n'.join(full_text)
481
+
482
+ if not extracted_text.strip():
483
+ extracted_text = "[No text found in this DOCX file.]"
484
+
485
+ except Exception as docx_err:
486
+ logger.error(f"DOCX extraction failed for {file_obj.name}: {docx_err}")
487
+ extracted_text = f"[Error extracting DOCX text for {file_obj.name}: {str(docx_err)}]"
488
+ else:
489
+ extracted_text = "[DOCX support is not available. Please install python-docx.]"
490
+
491
+ elif fmt == 'csv':
492
+ try:
493
+ # Decode bytes to string
494
+ csv_text = file_bytes.decode('utf-8', errors='replace')
495
+ csv_file = io.StringIO(csv_text)
496
+ csv_reader = csv.reader(csv_file)
497
+
498
+ rows = []
499
+ for row in csv_reader:
500
+ rows.append(','.join(row))
501
+
502
+ extracted_text = '\n'.join(rows)
503
+
504
+ if not extracted_text.strip():
505
+ extracted_text = "[Empty CSV file.]"
506
+
507
+ except Exception as csv_err:
508
+ logger.error(f"CSV extraction failed for {file_obj.name}: {csv_err}")
509
+ extracted_text = f"[Error extracting CSV text for {file_obj.name}: {str(csv_err)}]"
510
+
511
  else:
512
+ # Try decoding as plain text (txt, md, html, etc)
513
  try:
514
  extracted_text = file_bytes.decode('utf-8', errors='replace')
515
  except Exception as dec_err:
 
524
 
525
  except ImportError as ie:
526
  logger.error(f"Missing dependency for file processing: {ie}")
527
+ return "Error: Server missing dependencies (pypdf, python-docx, or strands types) for file processing."
528
 
529
  # Construct final payload
530
  message_content = [{"text": combined_text}]