Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,10 +25,6 @@ if "rag" not in st.session_state:
|
|
25 |
if "llm" not in st.session_state:
|
26 |
st.session_state.llm = None
|
27 |
|
28 |
-
if "num" not in st.session_state:
|
29 |
-
st.session_state.num = 1
|
30 |
-
|
31 |
-
|
32 |
@st.cache_resource
|
33 |
def get_chat_google_model(api_key):
|
34 |
os.environ["GOOGLE_API_KEY"] = api_key
|
@@ -74,6 +70,29 @@ def load_chromadb(collection_name):
|
|
74 |
)
|
75 |
return db
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
if "vector_store" not in st.session_state:
|
78 |
st.session_state.vector_store = load_chromadb(f"data{st.session_state.num}")
|
79 |
|
@@ -131,17 +150,6 @@ def rag_chain(_model, _vectorstore):
|
|
131 |
if st.session_state.model is not None and st.session_state.vector_store is not None:
|
132 |
st.session_state.rag = rag_chain(st.session_state.model, st.session_state.vector_store)
|
133 |
|
134 |
-
# if st.session_state.save_dir is None:
|
135 |
-
# save_dir = "./Documents"
|
136 |
-
# if not os.path.exists(save_dir):
|
137 |
-
# os.makedirs(save_dir)
|
138 |
-
# st.session_state.save_dir = save_dir
|
139 |
-
|
140 |
-
# def load_txt(file_path):
|
141 |
-
# loader_sv = TextLoader(file_path=file_path, encoding="utf-8")
|
142 |
-
# doc = loader_sv.load()
|
143 |
-
# return doc
|
144 |
-
|
145 |
if "new_docs" not in st.session_state:
|
146 |
st.session_state.new_docs = False
|
147 |
|
@@ -212,8 +220,7 @@ def reset_vectorstore(_model, _vectorstore):
|
|
212 |
|
213 |
if st.session_state.new_docs:
|
214 |
st.session_state.new_docs = False
|
215 |
-
st.session_state.
|
216 |
-
st.session_state.vector_store = load_chromadb(f"data{st.session_state.num}")
|
217 |
if st.session_state.uploaded_files:
|
218 |
update_vectorstore(st.session_state.model, st.session_state.embd, st.session_state.vector_store, documents)
|
219 |
else:
|
|
|
25 |
if "llm" not in st.session_state:
|
26 |
st.session_state.llm = None
|
27 |
|
|
|
|
|
|
|
|
|
28 |
@st.cache_resource
|
29 |
def get_chat_google_model(api_key):
|
30 |
os.environ["GOOGLE_API_KEY"] = api_key
|
|
|
70 |
)
|
71 |
return db
|
72 |
|
73 |
+
@st.cache_resource
|
74 |
+
def update_chromadb(collection_name):
|
75 |
+
client = QdrantClient(
|
76 |
+
url="https://da9fadd2-dc5a-4481-ac79-4e2677a2354b.europe-west3-0.gcp.cloud.qdrant.io",
|
77 |
+
api_key="X_-IVToBM07Mot4Mmzg5xNjYzc1DlIgl0VQDUNmGhI_Z-WA5FJ2ETA"
|
78 |
+
)
|
79 |
+
|
80 |
+
try:
|
81 |
+
client.delete_collection(collection_name=collection_name)
|
82 |
+
except Exception as e:
|
83 |
+
print(f"Warning: {e}")
|
84 |
+
|
85 |
+
client.recreate_collection(
|
86 |
+
collection_name=collection_name,
|
87 |
+
vectors_config=VectorParams(size=768, distance=Distance.COSINE)
|
88 |
+
)
|
89 |
+
db = QdrantVectorStore(
|
90 |
+
client=client,
|
91 |
+
collection_name=collection_name,
|
92 |
+
embedding=st.session_state.embd,
|
93 |
+
)
|
94 |
+
return db
|
95 |
+
|
96 |
if "vector_store" not in st.session_state:
|
97 |
st.session_state.vector_store = load_chromadb(f"data{st.session_state.num}")
|
98 |
|
|
|
150 |
if st.session_state.model is not None and st.session_state.vector_store is not None:
|
151 |
st.session_state.rag = rag_chain(st.session_state.model, st.session_state.vector_store)
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
if "new_docs" not in st.session_state:
|
154 |
st.session_state.new_docs = False
|
155 |
|
|
|
220 |
|
221 |
if st.session_state.new_docs:
|
222 |
st.session_state.new_docs = False
|
223 |
+
st.session_state.vector_store = update_chromadb("data")
|
|
|
224 |
if st.session_state.uploaded_files:
|
225 |
update_vectorstore(st.session_state.model, st.session_state.embd, st.session_state.vector_store, documents)
|
226 |
else:
|