Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Commit
·
136373a
1
Parent(s):
665f955
chore: Initialize collection on startup and refresh data
Browse files
main.py
CHANGED
|
@@ -8,7 +8,7 @@ from fastapi import FastAPI, HTTPException, Query
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from starlette.responses import RedirectResponse
|
| 10 |
|
| 11 |
-
from load_data import get_save_path, refresh_data
|
| 12 |
|
| 13 |
# Set up logging
|
| 14 |
logging.basicConfig(
|
|
@@ -22,7 +22,7 @@ cache.setup("mem://?check_interval=10&size=10000")
|
|
| 22 |
# Initialize Chroma client
|
| 23 |
SAVE_PATH = get_save_path()
|
| 24 |
client = chromadb.PersistentClient(path=SAVE_PATH)
|
| 25 |
-
collection =
|
| 26 |
|
| 27 |
|
| 28 |
class QueryResult(BaseModel):
|
|
@@ -36,13 +36,23 @@ class QueryResponse(BaseModel):
|
|
| 36 |
|
| 37 |
@asynccontextmanager
|
| 38 |
async def lifespan(app: FastAPI):
|
| 39 |
-
|
|
|
|
| 40 |
logger.info("Starting up the application")
|
| 41 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
refresh_data()
|
| 43 |
logger.info("Data refresh completed successfully")
|
| 44 |
except Exception as e:
|
| 45 |
-
logger.error(f"Error during
|
|
|
|
| 46 |
|
| 47 |
yield # Here the app is running and handling requests
|
| 48 |
|
|
|
|
| 8 |
from pydantic import BaseModel
|
| 9 |
from starlette.responses import RedirectResponse
|
| 10 |
|
| 11 |
+
from load_data import get_embedding_function, get_save_path, refresh_data
|
| 12 |
|
| 13 |
# Set up logging
|
| 14 |
logging.basicConfig(
|
|
|
|
| 22 |
# Initialize Chroma client
|
| 23 |
SAVE_PATH = get_save_path()
|
| 24 |
client = chromadb.PersistentClient(path=SAVE_PATH)
|
| 25 |
+
collection = None
|
| 26 |
|
| 27 |
|
| 28 |
class QueryResult(BaseModel):
|
|
|
|
| 36 |
|
| 37 |
@asynccontextmanager
|
| 38 |
async def lifespan(app: FastAPI):
|
| 39 |
+
global collection
|
| 40 |
+
# Startup: refresh data and initialize collection
|
| 41 |
logger.info("Starting up the application")
|
| 42 |
try:
|
| 43 |
+
# Create or get the collection
|
| 44 |
+
embedding_function = get_embedding_function()
|
| 45 |
+
collection = client.get_or_create_collection(
|
| 46 |
+
name="dataset_cards", embedding_function=embedding_function
|
| 47 |
+
)
|
| 48 |
+
logger.info("Collection initialized successfully")
|
| 49 |
+
|
| 50 |
+
# Refresh data
|
| 51 |
refresh_data()
|
| 52 |
logger.info("Data refresh completed successfully")
|
| 53 |
except Exception as e:
|
| 54 |
+
logger.error(f"Error during startup: {str(e)}")
|
| 55 |
+
raise
|
| 56 |
|
| 57 |
yield # Here the app is running and handling requests
|
| 58 |
|