Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -79,16 +79,14 @@ def init_astra_db():
|
|
| 79 |
astra_db_region = os.getenv("ASTRA_DB_REGION")
|
| 80 |
astra_db_keyspace = os.getenv("ASTRA_DB_KEYSPACE")
|
| 81 |
astra_db_application_token = os.getenv("ASTRA_DB_APPLICATION_TOKEN")
|
|
|
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
'token', astra_db_application_token)
|
| 90 |
-
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
|
| 91 |
-
session = cluster.connect()
|
| 92 |
|
| 93 |
# Create keyspace if it doesn't exist
|
| 94 |
session.execute(f"""
|
|
@@ -107,35 +105,43 @@ def init_astra_db():
|
|
| 107 |
)
|
| 108 |
""")
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
query text,
|
| 115 |
-
product_type text,
|
| 116 |
-
timestamp timestamp,
|
| 117 |
-
response_time float
|
| 118 |
-
)
|
| 119 |
-
""")
|
| 120 |
-
|
| 121 |
-
# Create table for product images
|
| 122 |
-
session.execute(f"""
|
| 123 |
-
CREATE TABLE IF NOT EXISTS {astra_db_keyspace}.product_images (
|
| 124 |
-
id text PRIMARY KEY,
|
| 125 |
-
product_type text,
|
| 126 |
-
image_data blob,
|
| 127 |
-
page_number int,
|
| 128 |
-
image_index int,
|
| 129 |
-
metadata text
|
| 130 |
-
)
|
| 131 |
-
""")
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
except Exception as e:
|
| 136 |
print(f"Error connecting to Astra DB: {e}")
|
| 137 |
-
|
| 138 |
-
return None, None
|
| 139 |
|
| 140 |
# Initialize AWS S3 client for accessing product catalogs
|
| 141 |
def init_s3_client():
|
|
|
|
| 79 |
astra_db_region = os.getenv("ASTRA_DB_REGION")
|
| 80 |
astra_db_keyspace = os.getenv("ASTRA_DB_KEYSPACE")
|
| 81 |
astra_db_application_token = os.getenv("ASTRA_DB_APPLICATION_TOKEN")
|
| 82 |
+
astra_db_endpoint = os.getenv("ASTRA_DB_ENDPOINT")
|
| 83 |
|
| 84 |
+
# Initialize the client
|
| 85 |
+
client = DataAPIClient(astra_db_token)
|
| 86 |
+
db = client.get_database_by_api_endpoint(
|
| 87 |
+
astra_db_endpoint,
|
| 88 |
+
keyspace=astra_db_keyspace
|
| 89 |
+
)
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# Create keyspace if it doesn't exist
|
| 92 |
session.execute(f"""
|
|
|
|
| 105 |
)
|
| 106 |
""")
|
| 107 |
|
| 108 |
+
# Get or create collections
|
| 109 |
+
product_embeddings = db.get_collection("product_embeddings")
|
| 110 |
+
query_analytics = db.get_collection("query_analytics")
|
| 111 |
+
product_images = db.get_collection("product_images")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# Create vector index for product_embeddings if it doesn't exist
|
| 114 |
+
try:
|
| 115 |
+
# Check if vector search is already set up
|
| 116 |
+
vector_indexes = product_embeddings.get_vector_indexes()
|
| 117 |
+
has_vector_index = any(index.get("name") == "embedding_vector_index" for index in vector_indexes)
|
| 118 |
+
|
| 119 |
+
if not has_vector_index:
|
| 120 |
+
# Create vector index
|
| 121 |
+
product_embeddings.create_vector_index(
|
| 122 |
+
vector_field="$vector",
|
| 123 |
+
dimension=1536, # Adjust dimension based on your embedding model
|
| 124 |
+
index_name="embedding_vector_index"
|
| 125 |
+
)
|
| 126 |
+
print("Vector index created for product_embeddings collection")
|
| 127 |
+
except Exception as e:
|
| 128 |
+
print(f"Warning: Could not create vector index: {e}")
|
| 129 |
+
|
| 130 |
+
print(f"Connected to Astra DB: {db.list_collection_names()}")
|
| 131 |
+
|
| 132 |
+
# Return DB client and collections for use in application
|
| 133 |
+
return {
|
| 134 |
+
"db": db,
|
| 135 |
+
"keyspace": astra_db_keyspace,
|
| 136 |
+
"collections": {
|
| 137 |
+
"product_embeddings": product_embeddings,
|
| 138 |
+
"query_analytics": query_analytics,
|
| 139 |
+
"product_images": product_images
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
except Exception as e:
|
| 143 |
print(f"Error connecting to Astra DB: {e}")
|
| 144 |
+
return None
|
|
|
|
| 145 |
|
| 146 |
# Initialize AWS S3 client for accessing product catalogs
|
| 147 |
def init_s3_client():
|