MEOW commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,7 +56,8 @@ def load_documents(file_objs):
|
|
| 56 |
try:
|
| 57 |
if not file_objs:
|
| 58 |
return "Error: No files selected."
|
| 59 |
-
|
|
|
|
| 60 |
file_extractor = {
|
| 61 |
".pdf": PDFReader(),
|
| 62 |
".csv": CSVReader(),
|
|
@@ -67,6 +68,15 @@ def load_documents(file_objs):
|
|
| 67 |
".png": ImageReader(),
|
| 68 |
}
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
file_paths = get_files_from_input(file_objs)
|
| 71 |
documents = []
|
| 72 |
for file_path in file_paths:
|
|
@@ -91,14 +101,15 @@ def load_documents(file_objs):
|
|
| 91 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 92 |
|
| 93 |
# Create the index from the documents
|
| 94 |
-
index = VectorStoreIndex.from_documents(
|
| 95 |
|
| 96 |
# Create the query engine after the index is created
|
| 97 |
-
query_engine = index.as_query_engine(
|
| 98 |
return f"Successfully loaded {len(documents)} documents from {len(file_paths)} files."
|
| 99 |
except Exception as e:
|
| 100 |
return f"Error loading documents: {str(e)}"
|
| 101 |
|
|
|
|
| 102 |
# Function to handle chat interactions
|
| 103 |
def chat(message,history):
|
| 104 |
global query_engine
|
|
|
|
| 56 |
try:
|
| 57 |
if not file_objs:
|
| 58 |
return "Error: No files selected."
|
| 59 |
+
|
| 60 |
+
all_texts = [] # Accumulate text from all files
|
| 61 |
file_extractor = {
|
| 62 |
".pdf": PDFReader(),
|
| 63 |
".csv": CSVReader(),
|
|
|
|
| 68 |
".png": ImageReader(),
|
| 69 |
}
|
| 70 |
|
| 71 |
+
for file_obj in file_objs: # Iterate through file objects # to process correct file type
|
| 72 |
+
file_extension = os.path.splitext(file_obj.name)[1].lower()
|
| 73 |
+
if file_extension in file_extractor:
|
| 74 |
+
extractor = file_extractor[file_extension]
|
| 75 |
+
extracted_text = extractor.extract(file_obj)
|
| 76 |
+
all_texts.extend(extracted_text) # Add extracted text to the list
|
| 77 |
+
else:
|
| 78 |
+
print(f"Warning: Unsupported file type: {file_extension}")
|
| 79 |
+
|
| 80 |
file_paths = get_files_from_input(file_objs)
|
| 81 |
documents = []
|
| 82 |
for file_path in file_paths:
|
|
|
|
| 101 |
storage_context = StorageContext.from_defaults(vector_store=vector_store)
|
| 102 |
|
| 103 |
# Create the index from the documents
|
| 104 |
+
index = VectorStoreIndex.from_documents(all_texts, embedding=embeddings) # Create index inside the function after documents are loaded
|
| 105 |
|
| 106 |
# Create the query engine after the index is created
|
| 107 |
+
query_engine = index.as_query_engine()
|
| 108 |
return f"Successfully loaded {len(documents)} documents from {len(file_paths)} files."
|
| 109 |
except Exception as e:
|
| 110 |
return f"Error loading documents: {str(e)}"
|
| 111 |
|
| 112 |
+
|
| 113 |
# Function to handle chat interactions
|
| 114 |
def chat(message,history):
|
| 115 |
global query_engine
|