Spaces:
Runtime error
Runtime error
updated get_csv and update_csv_from_local
Browse files- db_firestore.py +55 -1
db_firestore.py
CHANGED
@@ -3,6 +3,7 @@ from langchain_community.embeddings.huggingface import HuggingFaceBgeEmbeddings
|
|
3 |
import firebase_admin
|
4 |
from firebase_admin import credentials, storage
|
5 |
import json, os, dotenv
|
|
|
6 |
from dotenv import load_dotenv
|
7 |
load_dotenv()
|
8 |
try:
|
@@ -13,10 +14,39 @@ except TypeError:
|
|
13 |
import streamlit as st
|
14 |
os.environ["FIREBASE_CREDENTIAL"] = st.secrets["FIREBASE_CREDENTIAL"]
|
15 |
cred = credentials.Certificate(json.loads(str(os.environ.get("FIREBASE_CREDENTIAL")), strict=False))
|
16 |
-
|
17 |
if not firebase_admin._apps:
|
18 |
firebase_admin.initialize_app(cred, {'storageBucket': 'healthhack-store.appspot.com'})
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def get_store(index_name, embeddings = None):
|
22 |
while index_name[-1]=="/":
|
@@ -54,6 +84,30 @@ def get_store(index_name, embeddings = None):
|
|
54 |
store = FAISS.load_local(index_name, embeddings)
|
55 |
return store
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
def update_store_from_local(index_name):
|
58 |
while index_name[-1]=="/":
|
59 |
index_name = index_name[:-1]
|
|
|
3 |
import firebase_admin
|
4 |
from firebase_admin import credentials, storage
|
5 |
import json, os, dotenv
|
6 |
+
import pandas as pd
|
7 |
from dotenv import load_dotenv
|
8 |
load_dotenv()
|
9 |
try:
|
|
|
14 |
import streamlit as st
|
15 |
os.environ["FIREBASE_CREDENTIAL"] = st.secrets["FIREBASE_CREDENTIAL"]
|
16 |
cred = credentials.Certificate(json.loads(str(os.environ.get("FIREBASE_CREDENTIAL")), strict=False))
|
17 |
+
|
18 |
if not firebase_admin._apps:
|
19 |
firebase_admin.initialize_app(cred, {'storageBucket': 'healthhack-store.appspot.com'})
|
20 |
|
21 |
+
def get_csv(index_name, isDiseases = True):
|
22 |
+
while index_name[-1]=="/":
|
23 |
+
index_name = index_name[:-1]
|
24 |
+
dir = index_name.split("/")
|
25 |
+
|
26 |
+
## Check if path exists locally
|
27 |
+
for i in range(len(dir)):
|
28 |
+
path = '/'.join(dir[:i+1])
|
29 |
+
if not os.path.exists(path):
|
30 |
+
os.mkdir(path)
|
31 |
+
|
32 |
+
## Check if file exists locally, get from blob
|
33 |
+
if (not os.path.exists(index_name+"/diseases.csv") or
|
34 |
+
not os.path.exists(index_name+"/network.csv")
|
35 |
+
):
|
36 |
+
bucket = storage.bucket()
|
37 |
+
blob = bucket.blob(f"{index_name}/diseases.csv")
|
38 |
+
blob.download_to_filename(f"{index_name}/diseases.csv")
|
39 |
+
bucket = storage.bucket()
|
40 |
+
blob = bucket.blob(f"{index_name}/network.csv")
|
41 |
+
blob.download_to_filename(f"{index_name}/network.csv")
|
42 |
+
|
43 |
+
## load store from local
|
44 |
+
if isDiseases:
|
45 |
+
store = pd.read_csv(f"{index_name}/diseases.csv")
|
46 |
+
return store
|
47 |
+
else:
|
48 |
+
store = pd.read_csv(f"{index_name}/network.csv")
|
49 |
+
return store
|
50 |
|
51 |
def get_store(index_name, embeddings = None):
|
52 |
while index_name[-1]=="/":
|
|
|
84 |
store = FAISS.load_local(index_name, embeddings)
|
85 |
return store
|
86 |
|
87 |
+
def update_csv_from_local(index_name):
|
88 |
+
while index_name[-1]=="/":
|
89 |
+
index_name = index_name[:-1]
|
90 |
+
pathdir = index_name.split("/")
|
91 |
+
|
92 |
+
## Check if path exists locally
|
93 |
+
for i in range(len(pathdir)):
|
94 |
+
path = '/'.join(pathdir[:i+1])
|
95 |
+
if not os.path.exists(path):
|
96 |
+
raise Exception("Index name does not exist locally")
|
97 |
+
|
98 |
+
if (not os.path.exists(index_name+"/diseases.csv") or
|
99 |
+
not os.path.exists(index_name+"/network.csv")
|
100 |
+
):
|
101 |
+
raise("Index is missing some files (network.csv, diseases.csv)")
|
102 |
+
|
103 |
+
## Update store
|
104 |
+
bucket = storage.bucket()
|
105 |
+
blob = bucket.blob(index_name+"/diseases.csv")
|
106 |
+
blob.upload_from_filename(index_name+"/diseases.csv")
|
107 |
+
blob = bucket.blob(index_name+"/network.csv")
|
108 |
+
blob.upload_from_filename(index_name+"/network.csv")
|
109 |
+
return True
|
110 |
+
|
111 |
def update_store_from_local(index_name):
|
112 |
while index_name[-1]=="/":
|
113 |
index_name = index_name[:-1]
|