Spaces:
Running
Running
Update app.py
Browse files
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
|
77 |
print("π Processing document for QA...")
|
78 |
-
validation_error = validate_file_type(file)
|
79 |
-
if validation_error:
|
80 |
-
return validation_error
|
81 |
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
|
|
86 |
text = extract_text_from_pdf(file_bytes)
|
87 |
-
elif
|
88 |
text = extract_text_with_tika(file_bytes)
|
89 |
-
elif
|
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!"
|