Spaces:
Sleeping
Sleeping
andykr1k commited on
Commit ·
e7a6849
1
Parent(s): d94a580
Changed the rebuild method
Browse files
app.py
CHANGED
|
@@ -45,14 +45,17 @@ if torch.cuda.is_available():
|
|
| 45 |
torch.cuda.manual_seed_all(SEED)
|
| 46 |
|
| 47 |
# Global Variables
|
| 48 |
-
global G, features, user_nodes, post_nodes, node2idx, pyg_data, trained_model, post_embeddings
|
| 49 |
-
G = features = user_nodes = post_nodes = node2idx = pyg_data = trained_model = post_embeddings = None
|
| 50 |
|
| 51 |
SUPABASE_URL = os.getenv('supabaseUrl')
|
| 52 |
SUPABASE_KEY = os.getenv('supabaseAnonKey')
|
| 53 |
|
| 54 |
def get_supabase_client():
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
async def get_record_count(table):
|
| 58 |
supabase = get_supabase_client()
|
|
@@ -214,8 +217,8 @@ def get_recommendations(user_id, model, data, G, user_nodes, post_nodes, node2id
|
|
| 214 |
return []
|
| 215 |
|
| 216 |
user_idx = node2idx[user_id]
|
| 217 |
-
user_interacted = set(G[user_id])
|
| 218 |
-
post_indices = [node2idx[p] for p in post_nodes if p not in user_interacted]
|
| 219 |
|
| 220 |
if not post_indices:
|
| 221 |
return []
|
|
@@ -227,7 +230,7 @@ def get_recommendations(user_id, model, data, G, user_nodes, post_nodes, node2id
|
|
| 227 |
|
| 228 |
idx2node = {idx: node for node, idx in node2idx.items()}
|
| 229 |
post_scores = [(idx2node[i], score.item()) for i, score in zip(post_indices, scores)]
|
| 230 |
-
post_scores = sorted(post_scores, key=lambda x: x[1], reverse=True)
|
| 231 |
|
| 232 |
recommendations = [{"post_id": post_id, "score": score} for post_id, score in post_scores]
|
| 233 |
logger.info(f"Generated {len(recommendations)} recommendations for user {user_id}")
|
|
@@ -298,18 +301,27 @@ async def health_check():
|
|
| 298 |
return {"status": "success", "message": "Service operational"}
|
| 299 |
|
| 300 |
scheduler = BackgroundScheduler(timezone="America/Los_Angeles")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
scheduler.add_job(
|
| 302 |
-
|
| 303 |
-
trigger=CronTrigger(hour=3, minute=
|
| 304 |
id='daily_model_rebuild',
|
| 305 |
-
replace_existing=True
|
|
|
|
| 306 |
)
|
| 307 |
|
| 308 |
@app.on_event("startup")
|
| 309 |
async def startup_event():
|
| 310 |
await rebuild_model()
|
| 311 |
scheduler.start()
|
| 312 |
-
logger.info("Scheduler started, model will rebuild daily at 3:
|
| 313 |
|
| 314 |
@app.on_event("shutdown")
|
| 315 |
async def shutdown_event():
|
|
|
|
| 45 |
torch.cuda.manual_seed_all(SEED)
|
| 46 |
|
| 47 |
# Global Variables
|
| 48 |
+
global G, features, user_nodes, post_nodes, node2idx, pyg_data, trained_model, post_embeddings, supabase_client
|
| 49 |
+
G = features = user_nodes = post_nodes = node2idx = pyg_data = trained_model = post_embeddings = supabase_client = None
|
| 50 |
|
| 51 |
SUPABASE_URL = os.getenv('supabaseUrl')
|
| 52 |
SUPABASE_KEY = os.getenv('supabaseAnonKey')
|
| 53 |
|
| 54 |
def get_supabase_client():
|
| 55 |
+
global supabase_client
|
| 56 |
+
if supabase_client is None:
|
| 57 |
+
supabase_client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 58 |
+
return supabase_client
|
| 59 |
|
| 60 |
async def get_record_count(table):
|
| 61 |
supabase = get_supabase_client()
|
|
|
|
| 217 |
return []
|
| 218 |
|
| 219 |
user_idx = node2idx[user_id]
|
| 220 |
+
user_interacted = set(G[user_id])
|
| 221 |
+
post_indices = [node2idx[p] for p in post_nodes if p not in user_interacted]
|
| 222 |
|
| 223 |
if not post_indices:
|
| 224 |
return []
|
|
|
|
| 230 |
|
| 231 |
idx2node = {idx: node for node, idx in node2idx.items()}
|
| 232 |
post_scores = [(idx2node[i], score.item()) for i, score in zip(post_indices, scores)]
|
| 233 |
+
post_scores = sorted(post_scores, key=lambda x: x[1], reverse=True)
|
| 234 |
|
| 235 |
recommendations = [{"post_id": post_id, "score": score} for post_id, score in post_scores]
|
| 236 |
logger.info(f"Generated {len(recommendations)} recommendations for user {user_id}")
|
|
|
|
| 301 |
return {"status": "success", "message": "Service operational"}
|
| 302 |
|
| 303 |
scheduler = BackgroundScheduler(timezone="America/Los_Angeles")
|
| 304 |
+
def sync_rebuild_model():
|
| 305 |
+
loop = asyncio.new_event_loop()
|
| 306 |
+
asyncio.set_event_loop(loop)
|
| 307 |
+
try:
|
| 308 |
+
loop.run_until_complete(rebuild_model())
|
| 309 |
+
finally:
|
| 310 |
+
loop.close()
|
| 311 |
+
|
| 312 |
scheduler.add_job(
|
| 313 |
+
sync_rebuild_model,
|
| 314 |
+
trigger=CronTrigger(hour=3, minute=00),
|
| 315 |
id='daily_model_rebuild',
|
| 316 |
+
replace_existing=True,
|
| 317 |
+
misfire_grace_time=None
|
| 318 |
)
|
| 319 |
|
| 320 |
@app.on_event("startup")
|
| 321 |
async def startup_event():
|
| 322 |
await rebuild_model()
|
| 323 |
scheduler.start()
|
| 324 |
+
logger.info("Scheduler started, model will rebuild daily at 3:00 AM Pacific Time")
|
| 325 |
|
| 326 |
@app.on_event("shutdown")
|
| 327 |
async def shutdown_event():
|