Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -173,13 +173,18 @@ def upload_to_firebase(user_id, file):
|
|
| 173 |
content = convert_file_to_md(file) # Ensure this function extracts content correctly
|
| 174 |
if not content:
|
| 175 |
return None, "Failed to extract content from the file."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
doc_id = str(uuid.uuid4())
|
| 178 |
document_data = {"content": content, "name": file.name}
|
| 179 |
|
| 180 |
# Save document to Firebase
|
| 181 |
db.child("users").child(user_id).child("KnowledgeBase").child(doc_id).set(document_data)
|
| 182 |
-
|
| 183 |
# Add content to the knowledge base
|
| 184 |
if "knowledge_base" not in st.session_state:
|
| 185 |
st.session_state["knowledge_base"] = []
|
|
@@ -191,16 +196,15 @@ def upload_to_firebase(user_id, file):
|
|
| 191 |
st.sidebar.success(f"Document '{file.name}' uploaded successfully and added to the knowledge base!")
|
| 192 |
return content, None
|
| 193 |
|
| 194 |
-
|
| 195 |
def index_document_content(doc_content, doc_id):
|
| 196 |
"""
|
| 197 |
Indexes the document content by splitting it into chunks and creating embeddings.
|
| 198 |
"""
|
| 199 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=
|
| 200 |
texts = text_splitter.split_text(doc_content)
|
| 201 |
|
| 202 |
# Create embeddings for each chunk
|
| 203 |
-
embeddings = OpenAIEmbeddings(
|
| 204 |
doc_metadata = [{"doc_id": doc_id, "chunk_id": i} for i in range(len(texts))]
|
| 205 |
vector_store = FAISS.from_texts(texts, embeddings, metadatas=doc_metadata)
|
| 206 |
|
|
@@ -215,7 +219,6 @@ def index_document_content(doc_content, doc_id):
|
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
-
|
| 219 |
def fetch_trustbuilders(user_id):
|
| 220 |
"""
|
| 221 |
Retrieve TrustBuilders from Firebase for a specific user.
|
|
@@ -797,16 +800,7 @@ def side():
|
|
| 797 |
if uploaded_file:
|
| 798 |
try:
|
| 799 |
content, _= upload_to_firebase(st.session_state["wix_user_id"], uploaded_file)
|
| 800 |
-
|
| 801 |
-
doc_id = str(uuid.uuid4())
|
| 802 |
-
db.child("users").child(st.session_state["wix_user_id"]).child("KnowledgeBase").child(doc_id).set(
|
| 803 |
-
{"content": content, "name": uploaded_file.name}
|
| 804 |
-
)
|
| 805 |
-
st.session_state["documents"][doc_id] = {"content": content, "name": uploaded_file.name}
|
| 806 |
-
st.sidebar.success(f"Document '{uploaded_file.name}' uploaded successfully!")
|
| 807 |
-
st.rerun()
|
| 808 |
-
else:
|
| 809 |
-
st.sidebar.warning("File upload failed.")
|
| 810 |
except Exception as e:
|
| 811 |
st.sidebar.error(f"Error uploading document: {e}")
|
| 812 |
else:
|
|
|
|
| 173 |
content = convert_file_to_md(file) # Ensure this function extracts content correctly
|
| 174 |
if not content:
|
| 175 |
return None, "Failed to extract content from the file."
|
| 176 |
+
|
| 177 |
+
existing_files = st.session_state.get("documents", {})
|
| 178 |
+
for doc_id, doc_data in existing_files.items():
|
| 179 |
+
if doc_data["name"] == file.name and doc_data["content"] == content:
|
| 180 |
+
return None, f"File '{file.name}' already exists."
|
| 181 |
|
| 182 |
doc_id = str(uuid.uuid4())
|
| 183 |
document_data = {"content": content, "name": file.name}
|
| 184 |
|
| 185 |
# Save document to Firebase
|
| 186 |
db.child("users").child(user_id).child("KnowledgeBase").child(doc_id).set(document_data)
|
| 187 |
+
fetch_documents()
|
| 188 |
# Add content to the knowledge base
|
| 189 |
if "knowledge_base" not in st.session_state:
|
| 190 |
st.session_state["knowledge_base"] = []
|
|
|
|
| 196 |
st.sidebar.success(f"Document '{file.name}' uploaded successfully and added to the knowledge base!")
|
| 197 |
return content, None
|
| 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 |
|
|
|
|
| 219 |
|
| 220 |
|
| 221 |
|
|
|
|
| 222 |
def fetch_trustbuilders(user_id):
|
| 223 |
"""
|
| 224 |
Retrieve TrustBuilders from Firebase for a specific user.
|
|
|
|
| 800 |
if uploaded_file:
|
| 801 |
try:
|
| 802 |
content, _= upload_to_firebase(st.session_state["wix_user_id"], uploaded_file)
|
| 803 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
except Exception as e:
|
| 805 |
st.sidebar.error(f"Error uploading document: {e}")
|
| 806 |
else:
|