Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -74,6 +74,13 @@ def init_mistral_api():
|
|
| 74 |
# Initialize Astra DB connection
|
| 75 |
def init_astra_db():
|
| 76 |
"""Initialize connection to Astra DB"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
try:
|
| 78 |
# Get credentials from environment variables
|
| 79 |
astra_db_id = os.getenv("ASTRA_DB_ID")
|
|
@@ -84,36 +91,32 @@ def init_astra_db():
|
|
| 84 |
|
| 85 |
# Initialize the client
|
| 86 |
db = DataAPIClient(api_endpoint=astra_db_endpoint, token=astra_db_application_token)
|
| 87 |
-
|
| 88 |
-
# Initialize collection variables
|
| 89 |
-
product_embeddings = None
|
| 90 |
-
query_analytics = None
|
| 91 |
-
product_images = None
|
| 92 |
|
| 93 |
# Try to create or access collections
|
| 94 |
try:
|
| 95 |
product_embeddings = db.collection("product_embeddings")
|
| 96 |
query_analytics = db.collection("query_analytics")
|
| 97 |
product_images = db.collection("product_images")
|
|
|
|
| 98 |
except Exception as collection_error:
|
| 99 |
print(f"Error creating collections: {collection_error}")
|
| 100 |
-
|
| 101 |
-
|
| 102 |
print(f"Connected to Astra DB")
|
| 103 |
-
|
| 104 |
-
# Return DB client and collections for use in application
|
| 105 |
-
return {
|
| 106 |
-
"db": db,
|
| 107 |
-
"keyspace": astra_db_keyspace,
|
| 108 |
-
"collections": {
|
| 109 |
-
"product_embeddings": product_embeddings,
|
| 110 |
-
"query_analytics": query_analytics,
|
| 111 |
-
"product_images": product_images
|
| 112 |
-
}
|
| 113 |
-
}
|
| 114 |
except Exception as e:
|
| 115 |
print(f"Error connecting to Astra DB: {e}")
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Initialize AWS S3 client for accessing product catalogs
|
| 119 |
def init_s3_client():
|
|
|
|
| 74 |
# Initialize Astra DB connection
|
| 75 |
def init_astra_db():
|
| 76 |
"""Initialize connection to Astra DB"""
|
| 77 |
+
# Initialize collection variables at the very beginning
|
| 78 |
+
db = None
|
| 79 |
+
product_embeddings = None
|
| 80 |
+
query_analytics = None
|
| 81 |
+
product_images = None
|
| 82 |
+
astra_db_keyspace = None
|
| 83 |
+
|
| 84 |
try:
|
| 85 |
# Get credentials from environment variables
|
| 86 |
astra_db_id = os.getenv("ASTRA_DB_ID")
|
|
|
|
| 91 |
|
| 92 |
# Initialize the client
|
| 93 |
db = DataAPIClient(api_endpoint=astra_db_endpoint, token=astra_db_application_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Try to create or access collections
|
| 96 |
try:
|
| 97 |
product_embeddings = db.collection("product_embeddings")
|
| 98 |
query_analytics = db.collection("query_analytics")
|
| 99 |
product_images = db.collection("product_images")
|
| 100 |
+
print("Successfully created/accessed collections")
|
| 101 |
except Exception as collection_error:
|
| 102 |
print(f"Error creating collections: {collection_error}")
|
| 103 |
+
|
|
|
|
| 104 |
print(f"Connected to Astra DB")
|
| 105 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
print(f"Error connecting to Astra DB: {e}")
|
| 108 |
+
db = None
|
| 109 |
+
|
| 110 |
+
# Always return a dictionary, even if there are errors
|
| 111 |
+
return {
|
| 112 |
+
"db": db,
|
| 113 |
+
"keyspace": astra_db_keyspace,
|
| 114 |
+
"collections": {
|
| 115 |
+
"product_embeddings": product_embeddings,
|
| 116 |
+
"query_analytics": query_analytics,
|
| 117 |
+
"product_images": product_images
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
|
| 121 |
# Initialize AWS S3 client for accessing product catalogs
|
| 122 |
def init_s3_client():
|