Spaces:
Sleeping
Sleeping
andykr1k commited on
Commit ·
0b3667d
1
Parent(s): 31d75b0
Needed to update recommender for season and episode props
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@ from zoneinfo import ZoneInfo
|
|
| 14 |
from sentence_transformers import SentenceTransformer
|
| 15 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 16 |
import math
|
|
|
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -22,17 +23,6 @@ logger = logging.getLogger(__name__)
|
|
| 22 |
# Load environment variables
|
| 23 |
load_dotenv()
|
| 24 |
|
| 25 |
-
# FastAPI app setup
|
| 26 |
-
app = FastAPI()
|
| 27 |
-
|
| 28 |
-
app.add_middleware(
|
| 29 |
-
CORSMiddleware,
|
| 30 |
-
allow_origins=["*"],
|
| 31 |
-
allow_credentials=True,
|
| 32 |
-
allow_methods=["*"],
|
| 33 |
-
allow_headers=["*"],
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
# Configuration
|
| 37 |
SEED = 42
|
| 38 |
random.seed(SEED)
|
|
@@ -120,7 +110,7 @@ class Recommender:
|
|
| 120 |
self.reply_weight = reply_weight
|
| 121 |
self.reply_like_weight = reply_like_weight
|
| 122 |
|
| 123 |
-
async def fetch_existing_post_ids() -> set:
|
| 124 |
supabase = get_supabase_client()
|
| 125 |
page_size = 1000
|
| 126 |
page = 0
|
|
@@ -345,6 +335,32 @@ def sync_background_update():
|
|
| 345 |
loop.run_until_complete(background_update())
|
| 346 |
loop.close()
|
| 347 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
@app.get("/recommend/feed")
|
| 349 |
async def get_recommendations_handler(user_id: str = Query(...)):
|
| 350 |
try:
|
|
@@ -365,17 +381,6 @@ async def get_recommendations_handler(user_id: str = Query(...)):
|
|
| 365 |
except Exception as e:
|
| 366 |
logger.error(f"Error generating recommendations: {str(e)}")
|
| 367 |
return {"status": "error", "message": str(e)}
|
| 368 |
-
|
| 369 |
-
@app.on_event("startup")
|
| 370 |
-
async def startup_event():
|
| 371 |
-
await recommender.update_data()
|
| 372 |
-
scheduler.add_job(sync_background_update, 'interval', seconds=UPDATE_INTERVAL)
|
| 373 |
-
scheduler.start()
|
| 374 |
-
|
| 375 |
-
@app.on_event("shutdown")
|
| 376 |
-
async def shutdown_event():
|
| 377 |
-
scheduler.shutdown()
|
| 378 |
-
logger.info("Scheduler shut down")
|
| 379 |
|
| 380 |
@app.get("/")
|
| 381 |
async def health_check():
|
|
|
|
| 14 |
from sentence_transformers import SentenceTransformer
|
| 15 |
from apscheduler.schedulers.background import BackgroundScheduler
|
| 16 |
import math
|
| 17 |
+
from contextlib import asynccontextmanager # Import this for lifespan
|
| 18 |
|
| 19 |
# Configure logging
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 23 |
# Load environment variables
|
| 24 |
load_dotenv()
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
# Configuration
|
| 27 |
SEED = 42
|
| 28 |
random.seed(SEED)
|
|
|
|
| 110 |
self.reply_weight = reply_weight
|
| 111 |
self.reply_like_weight = reply_like_weight
|
| 112 |
|
| 113 |
+
async def fetch_existing_post_ids(self) -> set:
|
| 114 |
supabase = get_supabase_client()
|
| 115 |
page_size = 1000
|
| 116 |
page = 0
|
|
|
|
| 335 |
loop.run_until_complete(background_update())
|
| 336 |
loop.close()
|
| 337 |
|
| 338 |
+
# Lifespan context manager
|
| 339 |
+
@asynccontextmanager
|
| 340 |
+
async def lifespan(app: FastAPI):
|
| 341 |
+
# Startup event
|
| 342 |
+
logger.info("Starting up application...")
|
| 343 |
+
await recommender.update_data()
|
| 344 |
+
scheduler.add_job(sync_background_update, 'interval', seconds=UPDATE_INTERVAL)
|
| 345 |
+
scheduler.start()
|
| 346 |
+
yield
|
| 347 |
+
# Shutdown event
|
| 348 |
+
logger.info("Shutting down application...")
|
| 349 |
+
scheduler.shutdown()
|
| 350 |
+
logger.info("Scheduler shut down")
|
| 351 |
+
|
| 352 |
+
# FastAPI app setup with lifespan
|
| 353 |
+
app = FastAPI(lifespan=lifespan)
|
| 354 |
+
|
| 355 |
+
app.add_middleware(
|
| 356 |
+
CORSMiddleware,
|
| 357 |
+
allow_origins=["*"],
|
| 358 |
+
allow_credentials=True,
|
| 359 |
+
allow_methods=["*"],
|
| 360 |
+
allow_headers=["*"],
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
|
| 364 |
@app.get("/recommend/feed")
|
| 365 |
async def get_recommendations_handler(user_id: str = Query(...)):
|
| 366 |
try:
|
|
|
|
| 381 |
except Exception as e:
|
| 382 |
logger.error(f"Error generating recommendations: {str(e)}")
|
| 383 |
return {"status": "error", "message": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
@app.get("/")
|
| 386 |
async def health_check():
|