chrisjcc commited on
Commit
b8c7ea0
·
verified ·
1 Parent(s): ad75af3

Debugging PDF Upload Error (#3)

Browse files

- Debugging PDF uploader error (7643ae067093b0094d7c74ad4d40dc5ba43a750e)

Files changed (1) hide show
  1. app.py +45 -54
app.py CHANGED
@@ -406,19 +406,25 @@ def query_agent(question: str, files: Optional[List[FilePayload]] = None) -> str
406
 
407
  agent = create_enhanced_agent()
408
 
 
 
 
 
 
 
409
  if files:
410
- # Try to use official types if available
411
  try:
412
- from strands.types.content import ImageContent, DocumentContent
413
- from strands.types.media import ImageSource, DocumentSource
414
-
415
- message_content = [{"text": question}]
 
416
 
417
  image_formats = {'png', 'jpeg', 'gif', 'webp', 'jpg'}
418
 
419
  for file_obj in files:
420
  try:
421
- # Remove header if present
422
  base64_data = file_obj.data
423
  if "," in base64_data:
424
  base64_data = base64_data.split(",")[1]
@@ -427,64 +433,49 @@ def query_agent(question: str, files: Optional[List[FilePayload]] = None) -> str
427
  fmt = file_obj.format.lower()
428
 
429
  if fmt in image_formats:
430
- # Handle Image
431
  image_block = ImageContent(
432
  format=fmt if fmt != 'jpg' else 'jpeg', # Normalize jpg
433
  source=ImageSource(bytes=file_bytes)
434
  )
435
- message_content.append({"image": image_block})
436
  else:
437
- # Handle Document
438
- doc_block = DocumentContent(
439
- format=fmt,
440
- name=file_obj.name,
441
- source=DocumentSource(bytes=file_bytes)
442
- )
443
- message_content.append({"document": doc_block})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
 
445
  except Exception as err:
446
  logger.error(f"Failed to process file {file_obj.name}: {err}")
447
 
448
- except ImportError:
449
- # Fallback for older versions or missing imports
450
- logger.info("Using legacy/dict construction (strands.types not found)")
451
- message_content = [{"text": question}]
452
-
453
- image_formats = {'png', 'jpeg', 'gif', 'webp', 'jpg'}
454
 
455
- for file_obj in files:
456
- try:
457
- base64_data = file_obj.data
458
- if "," in base64_data:
459
- base64_data = base64_data.split(",")[1]
460
-
461
- file_bytes = base64.b64decode(base64_data)
462
- fmt = file_obj.format.lower()
463
-
464
- if fmt in image_formats:
465
- message_content.append({
466
- "image": {
467
- "format": fmt if fmt != 'jpg' else 'jpeg',
468
- "source": {"bytes": file_bytes},
469
- },
470
- })
471
- else:
472
- message_content.append({
473
- "document": {
474
- "format": fmt,
475
- "name": file_obj.name,
476
- "source": {"bytes": file_bytes},
477
- },
478
- })
479
-
480
- except Exception as err:
481
- logger.error(f"Failed to process file: {err}")
482
 
483
- # Call agent with list payload
484
- result = agent(message_content)
485
- else:
486
- # Standard text-only call
487
- result = agent(question)
488
 
489
  logger.info("Query completed successfully")
490
  return str(result)
@@ -1088,4 +1079,4 @@ if __name__ == "__main__":
1088
  host="0.0.0.0",
1089
  port=7860,
1090
  reload=False
1091
- )
 
406
 
407
  agent = create_enhanced_agent()
408
 
409
+ # Base text content
410
+ combined_text = question
411
+
412
+ # List to hold image blocks
413
+ image_blocks = []
414
+
415
  if files:
 
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:
426
  try:
427
+ # Decode base64
428
  base64_data = file_obj.data
429
  if "," in base64_data:
430
  base64_data = base64_data.split(",")[1]
 
433
  fmt = file_obj.format.lower()
434
 
435
  if fmt in image_formats:
436
+ # Handle Image - Keep as rich content
437
  image_block = ImageContent(
438
  format=fmt if fmt != 'jpg' else 'jpeg', # Normalize jpg
439
  source=ImageSource(bytes=file_bytes)
440
  )
441
+ image_blocks.append({"image": image_block})
442
  else:
443
+ # Handle Document - Extract text and append to question
444
+ extracted_text = ""
445
+
446
+ if fmt == 'pdf':
447
+ try:
448
+ pdf_reader = pypdf.PdfReader(io.BytesIO(file_bytes))
449
+ for page in pdf_reader.pages:
450
+ extracted_text += page.extract_text() + "\n"
451
+ except Exception as pdf_err:
452
+ logger.error(f"PDF extraction failed for {file_obj.name}: {pdf_err}")
453
+ extracted_text = f"[Error extracting PDF text for {file_obj.name}]"
454
+ else:
455
+ # Try decoding as plain text (csv, txt, md, html, etc)
456
+ try:
457
+ extracted_text = file_bytes.decode('utf-8', errors='replace')
458
+ except Exception as dec_err:
459
+ logger.error(f"Text decoding failed for {file_obj.name}: {dec_err}")
460
+ extracted_text = f"[Error decoding text for {file_obj.name}]"
461
+
462
+ # Append to combined text
463
+ combined_text += f"\n\n--- Content from {file_obj.name} ---\n{extracted_text}\n-----------------------------------\n"
464
 
465
  except Exception as err:
466
  logger.error(f"Failed to process file {file_obj.name}: {err}")
467
 
468
+ except ImportError as ie:
469
+ logger.error(f"Missing dependency for file processing: {ie}")
470
+ return "Error: Server missing dependencies (pypdf or strands types) for file processing."
 
 
 
471
 
472
+ # Construct final payload
473
+ message_content = [{"text": combined_text}]
474
+ # Add any extracted images
475
+ message_content.extend(image_blocks)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
 
477
+ # Call agent with list payload
478
+ result = agent(message_content)
 
 
 
479
 
480
  logger.info("Query completed successfully")
481
  return str(result)
 
1079
  host="0.0.0.0",
1080
  port=7860,
1081
  reload=False
1082
+ )