|
import boto3 |
|
import os |
|
import zipfile |
|
from botocore.exceptions import ClientError |
|
import streamlit as st |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def download_vectorstore(): |
|
"""Download vector store from S3 and extract it""" |
|
try: |
|
|
|
url = "https://uk-building-regulations-vectorstore.s3.eu-west-2.amazonaws.com/main_chroma_data.zip" |
|
import requests |
|
|
|
st.info("Downloading vector store from S3...") |
|
zip_path = "main_chroma_data.zip" |
|
|
|
r = requests.get(url, stream=True) |
|
with open(zip_path, 'wb') as f: |
|
for chunk in r.iter_content(chunk_size=8192): |
|
f.write(chunk) |
|
|
|
|
|
with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
|
zip_ref.extractall("./") |
|
|
|
|
|
os.remove(zip_path) |
|
st.success("Vector store downloaded and extracted successfully!") |
|
|
|
except Exception as e: |
|
st.error(f"Detailed error: {str(e)}") |
|
raise e |
|
|
|
|
|
def upload_vectorstore(): |
|
""" |
|
Zip and upload vector store to S3 |
|
Note: This is for your local use, not needed in the app |
|
""" |
|
|
|
with zipfile.ZipFile('main_chroma_data.zip', 'w', zipfile.ZIP_DEFLATED) as zipf: |
|
for root, dirs, files in os.walk('main_chroma_data'): |
|
for file in files: |
|
zipf.write(os.path.join(root, file)) |
|
|
|
|
|
aws_access_key = os.getenv("AWS_ACCESS_KEY_ID") |
|
aws_secret_key = os.getenv("AWS_SECRET_ACCESS_KEY") |
|
bucket_name = os.getenv("AWS_S3_BUCKET") |
|
|
|
s3 = boto3.client( |
|
's3', |
|
aws_access_key_id=aws_access_key, |
|
aws_secret_access_key=aws_secret_key |
|
) |
|
|
|
s3.upload_file('main_chroma_data.zip', bucket_name, 'main_chroma_data.zip') |
|
print("Vector store uploaded successfully!") |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
import sys |
|
if len(sys.argv) > 1 and sys.argv[1] == "upload": |
|
upload_vectorstore() |
|
|
|
|
|
|