Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -171,13 +171,18 @@ def upload_to_firebase(user_id, file):
|
|
| 171 |
content = convert_file_to_md(file) # Ensure this function extracts content correctly
|
| 172 |
if not content:
|
| 173 |
return None, "Failed to extract content from the file."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
doc_id = str(uuid.uuid4())
|
| 176 |
document_data = {"content": content, "name": file.name}
|
| 177 |
|
| 178 |
# Save document to Firebase
|
| 179 |
db.child("users").child(user_id).child("KnowledgeBase").child(doc_id).set(document_data)
|
| 180 |
-
|
| 181 |
# Add content to the knowledge base
|
| 182 |
if "knowledge_base" not in st.session_state:
|
| 183 |
st.session_state["knowledge_base"] = []
|
|
@@ -190,15 +195,16 @@ def upload_to_firebase(user_id, file):
|
|
| 190 |
return content, None
|
| 191 |
|
| 192 |
|
|
|
|
| 193 |
def index_document_content(doc_content, doc_id):
|
| 194 |
"""
|
| 195 |
Indexes the document content by splitting it into chunks and creating embeddings.
|
| 196 |
"""
|
| 197 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=
|
| 198 |
texts = text_splitter.split_text(doc_content)
|
| 199 |
|
| 200 |
# Create embeddings for each chunk
|
| 201 |
-
embeddings = OpenAIEmbeddings(
|
| 202 |
doc_metadata = [{"doc_id": doc_id, "chunk_id": i} for i in range(len(texts))]
|
| 203 |
vector_store = FAISS.from_texts(texts, embeddings, metadatas=doc_metadata)
|
| 204 |
|
|
@@ -544,13 +550,17 @@ def download_link(content, filename):
|
|
| 544 |
|
| 545 |
|
| 546 |
def fetch_documents():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
try:
|
| 548 |
-
|
| 549 |
-
st.session_state["documents"] =
|
|
|
|
|
|
|
| 550 |
except Exception as e:
|
| 551 |
st.sidebar.error(f"Error fetching documents: {e}")
|
| 552 |
-
st.session_state["documents"] = {}
|
| 553 |
-
|
| 554 |
|
| 555 |
|
| 556 |
# Function to delete a document from Firebase
|
|
@@ -778,16 +788,7 @@ def side():
|
|
| 778 |
if uploaded_file:
|
| 779 |
try:
|
| 780 |
content, _= upload_to_firebase(st.session_state["wix_user_id"], uploaded_file)
|
| 781 |
-
|
| 782 |
-
doc_id = str(uuid.uuid4())
|
| 783 |
-
db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").child(doc_id).set(
|
| 784 |
-
{"content": content, "name": uploaded_file.name}
|
| 785 |
-
)
|
| 786 |
-
st.session_state["documents"][doc_id] = {"content": content, "name": uploaded_file.name}
|
| 787 |
-
st.sidebar.success(f"Document '{uploaded_file.name}' uploaded successfully!")
|
| 788 |
-
st.rerun()
|
| 789 |
-
else:
|
| 790 |
-
st.sidebar.warning("File upload failed.")
|
| 791 |
except Exception as e:
|
| 792 |
st.sidebar.error(f"Error uploading document: {e}")
|
| 793 |
else:
|
|
|
|
| 171 |
content = convert_file_to_md(file) # Ensure this function extracts content correctly
|
| 172 |
if not content:
|
| 173 |
return None, "Failed to extract content from the file."
|
| 174 |
+
|
| 175 |
+
existing_files = st.session_state.get("documents", {})
|
| 176 |
+
for doc_id, doc_data in existing_files.items():
|
| 177 |
+
if doc_data["name"] == file.name and doc_data["content"] == content:
|
| 178 |
+
return None, f"File '{file.name}' already exists."
|
| 179 |
|
| 180 |
doc_id = str(uuid.uuid4())
|
| 181 |
document_data = {"content": content, "name": file.name}
|
| 182 |
|
| 183 |
# Save document to Firebase
|
| 184 |
db.child("users").child(user_id).child("KnowledgeBase").child(doc_id).set(document_data)
|
| 185 |
+
fetch_documents()
|
| 186 |
# Add content to the knowledge base
|
| 187 |
if "knowledge_base" not in st.session_state:
|
| 188 |
st.session_state["knowledge_base"] = []
|
|
|
|
| 195 |
return content, None
|
| 196 |
|
| 197 |
|
| 198 |
+
|
| 199 |
def index_document_content(doc_content, doc_id):
|
| 200 |
"""
|
| 201 |
Indexes the document content by splitting it into chunks and creating embeddings.
|
| 202 |
"""
|
| 203 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
| 204 |
texts = text_splitter.split_text(doc_content)
|
| 205 |
|
| 206 |
# Create embeddings for each chunk
|
| 207 |
+
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002", api_key=openai_api_key)
|
| 208 |
doc_metadata = [{"doc_id": doc_id, "chunk_id": i} for i in range(len(texts))]
|
| 209 |
vector_store = FAISS.from_texts(texts, embeddings, metadatas=doc_metadata)
|
| 210 |
|
|
|
|
| 550 |
|
| 551 |
|
| 552 |
def fetch_documents():
|
| 553 |
+
"""
|
| 554 |
+
Fetch all documents for the current user from Firebase and update session state.
|
| 555 |
+
"""
|
| 556 |
+
user_id = st.session_state["wix_user_id"]
|
| 557 |
try:
|
| 558 |
+
documents = db.child("users").child(user_id).child("KnowledgeBase").get()
|
| 559 |
+
st.session_state["documents"] = {
|
| 560 |
+
doc.key(): doc.val() for doc in documents.each()
|
| 561 |
+
} if documents.each() else {}
|
| 562 |
except Exception as e:
|
| 563 |
st.sidebar.error(f"Error fetching documents: {e}")
|
|
|
|
|
|
|
| 564 |
|
| 565 |
|
| 566 |
# Function to delete a document from Firebase
|
|
|
|
| 788 |
if uploaded_file:
|
| 789 |
try:
|
| 790 |
content, _= upload_to_firebase(st.session_state["wix_user_id"], uploaded_file)
|
| 791 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 792 |
except Exception as e:
|
| 793 |
st.sidebar.error(f"Error uploading document: {e}")
|
| 794 |
else:
|