Spaces:
Sleeping
Sleeping
attempt to fix keyError:7 for global object:state
Browse files
app.py
CHANGED
|
@@ -59,21 +59,15 @@ def process_pdf(pdf_file, state):
|
|
| 59 |
state
|
| 60 |
)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
state.db = None # Reset the database state
|
| 66 |
-
|
| 67 |
-
# Create a new session and directory for the user
|
| 68 |
-
state.session_id = str(uuid.uuid4())
|
| 69 |
-
state.vector_store_path = os.path.join(CHROMA_DB_PATH, state.session_id)
|
| 70 |
-
os.makedirs(state.vector_store_path)
|
| 71 |
|
| 72 |
# Extract text from the PDF using PyMuPDF (fitz)
|
| 73 |
doc = fitz.open(pdf_file.name)
|
| 74 |
text = ""
|
| 75 |
for page in doc:
|
| 76 |
-
text +=
|
| 77 |
doc.close()
|
| 78 |
|
| 79 |
# Split text into chunks
|
|
@@ -82,10 +76,10 @@ def process_pdf(pdf_file, state):
|
|
| 82 |
|
| 83 |
# Create a ChromaDB vector store from the documents
|
| 84 |
embeddings = GoogleGenerativeAIEmbeddings(model=EMBEDDING_MODEL)
|
| 85 |
-
|
| 86 |
documents=docs,
|
| 87 |
embedding=embeddings,
|
| 88 |
-
persist_directory=
|
| 89 |
)
|
| 90 |
|
| 91 |
gr.Info("PDF processed successfully! You can now ask questions about the document.")
|
|
@@ -94,11 +88,11 @@ def process_pdf(pdf_file, state):
|
|
| 94 |
return (
|
| 95 |
gr.update(interactive=False),
|
| 96 |
gr.update(visible=True),
|
| 97 |
-
|
| 98 |
)
|
| 99 |
except Exception as e:
|
| 100 |
# Clean up the directory in case of an error
|
| 101 |
-
if os.path.exists(state.vector_store_path):
|
| 102 |
shutil.rmtree(state.vector_store_path)
|
| 103 |
gr.Error(f"An error occurred: {str(e)}")
|
| 104 |
# Re-enable the file upload in case of error
|
|
|
|
| 59 |
state
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# If a file is uploaded, a new session should be started
|
| 63 |
+
# The new session object is returned to update the state
|
| 64 |
+
new_state = new_session()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Extract text from the PDF using PyMuPDF (fitz)
|
| 67 |
doc = fitz.open(pdf_file.name)
|
| 68 |
text = ""
|
| 69 |
for page in doc:
|
| 70 |
+
text += doc.get_text()
|
| 71 |
doc.close()
|
| 72 |
|
| 73 |
# Split text into chunks
|
|
|
|
| 76 |
|
| 77 |
# Create a ChromaDB vector store from the documents
|
| 78 |
embeddings = GoogleGenerativeAIEmbeddings(model=EMBEDDING_MODEL)
|
| 79 |
+
new_state.db = Chroma.from_documents(
|
| 80 |
documents=docs,
|
| 81 |
embedding=embeddings,
|
| 82 |
+
persist_directory=new_state.vector_store_path
|
| 83 |
)
|
| 84 |
|
| 85 |
gr.Info("PDF processed successfully! You can now ask questions about the document.")
|
|
|
|
| 88 |
return (
|
| 89 |
gr.update(interactive=False),
|
| 90 |
gr.update(visible=True),
|
| 91 |
+
new_state
|
| 92 |
)
|
| 93 |
except Exception as e:
|
| 94 |
# Clean up the directory in case of an error
|
| 95 |
+
if state and os.path.exists(state.vector_store_path):
|
| 96 |
shutil.rmtree(state.vector_store_path)
|
| 97 |
gr.Error(f"An error occurred: {str(e)}")
|
| 98 |
# Re-enable the file upload in case of error
|