ikraamkb commited on
Commit
0f8e09c
Β·
verified Β·
1 Parent(s): a078426

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -73,20 +73,31 @@ def extract_text_from_excel(excel_bytes):
73
  except Exception as e:
74
  return f"❌ Error reading Excel: {str(e)}"
75
 
76
- def answer_question_from_document(file: UploadFile, question: str):
77
  print("πŸ“‚ Processing document for QA...")
78
- validation_error = validate_file_type(file)
79
- if validation_error:
80
- return validation_error
81
 
82
- file_ext = file.filename.split(".")[-1].lower()
83
- file_bytes = file.file.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- if file_ext == "pdf":
 
86
  text = extract_text_from_pdf(file_bytes)
87
- elif file_ext in ["docx", "pptx"]:
88
  text = extract_text_with_tika(file_bytes)
89
- elif file_ext == "xlsx":
90
  text = extract_text_from_excel(file_bytes)
91
  else:
92
  return "❌ Unsupported file format!"
 
73
  except Exception as e:
74
  return f"❌ Error reading Excel: {str(e)}"
75
 
76
+ def answer_question_from_document(file, question: str):
77
  print("πŸ“‚ Processing document for QA...")
 
 
 
78
 
79
+ # Ensure file is not None
80
+ if not file:
81
+ return "❌ No file uploaded."
82
+
83
+ ext = file.name.split(".")[-1].lower()
84
+ print(f"πŸ” Validating file type: {ext}")
85
+ if ext not in ALLOWED_EXTENSIONS:
86
+ return f"❌ Unsupported file format: {ext}"
87
+
88
+ # Read file contents
89
+ try:
90
+ with open(file.name, "rb") as f:
91
+ file_bytes = f.read()
92
+ except Exception as e:
93
+ return f"❌ Error reading file: {str(e)}"
94
 
95
+ # Extract text based on file type
96
+ if ext == "pdf":
97
  text = extract_text_from_pdf(file_bytes)
98
+ elif ext in ["docx", "pptx"]:
99
  text = extract_text_with_tika(file_bytes)
100
+ elif ext == "xlsx":
101
  text = extract_text_from_excel(file_bytes)
102
  else:
103
  return "❌ Unsupported file format!"