rag-assistant / google_storage.py
carpez's picture
.
1557d7f
Raw History Blame Contribute Delete
2.23 kB
from google.cloud import storage
from google.oauth2 import service_account
from dotenv import load_dotenv
import os
import base64
credentials_path = os.environ.get("GOOGLE_CREDENTIALS")
project_id = os.environ.get("PROJECT_ID")
bucket_name = os.environ.get("BUCKET_NAME")
credentials = service_account.Credentials.from_service_account_file(credentials_path)
client = storage.Client(credentials=credentials, project=project_id)
bucket = client.bucket(bucket_name)
vector_db = "./data/chroma_db/chroma.sqlite3"
if os.path.exists(vector_db):
print("Vector Database already in PATH")
else:
blob = bucket.blob("chroma.sqlite3")
blob.download_to_filename("./data/chroma_db/chroma.sqlite3")
def upload_images_and_tables(full_file_path, image_bytes):
blob = bucket.blob(full_file_path)
image = base64.b64decode(image_bytes)
blob.upload_from_string(image, content_type='image/jpeg')
return True
def get_pdf():
blob = bucket.blob("ingegneria/ingegneria_delle_prestazioni_degli_edifici.pdf")
blob.download_to_filename("./data/ingegneria_delle_prestazioni_degli_edifici.pdf")
return True
def upload_chromadb_to_google_storage():
blob = bucket.blob("chroma.sqlite3")
blob.upload_from_filename("./data/chroma_db/chroma.sqlite3")
return True
def get_images_and_tables(page_number, collection_name):
image_blobs = bucket.list_blobs(
prefix=f"{collection_name}/images/{page_number}"
)
table_blobs = bucket.list_blobs(
prefix=f"{collection_name}/tables/{page_number}"
)
images = []
tables = []
for image in image_blobs:
if image.content_type.startswith("image/"):
image_data = image.download_as_string()
image_url = f"data:image/{image.content_type.split('/')[-1]};base64,{base64.b64encode(image_data).decode()}"
images.append((image.name, image_url))
for table in table_blobs:
if table.content_type.startswith("image/"):
table_data = table.download_as_string()
table_url = f"data:image/{table.content_type.split('/')[-1]};base64,{base64.b64encode(table_data).decode()}"
tables.append((table.name, table_url))
return images, tables