Commit
·
d6a2392
1
Parent(s):
564f32e
feat(api): modify AI task to run in a separate thread to avoid blocking
Browse files
app/api/endpoints/analysis.py
CHANGED
|
@@ -366,7 +366,7 @@ async def process_on_demand_job(request: Request):
|
|
| 366 |
|
| 367 |
# 1. Initialization
|
| 368 |
job_data = await request.json()
|
| 369 |
-
print(job_data)
|
| 370 |
keyword = job_data.get("keyword")
|
| 371 |
job_id = job_data.get("job_id")
|
| 372 |
|
|
@@ -394,7 +394,9 @@ async def process_on_demand_job(request: Request):
|
|
| 394 |
# Note: For on-demand, I might use a smaller fetching strategy
|
| 395 |
videos = yt_service.search_videos(query_string=keyword)
|
| 396 |
if not videos:
|
| 397 |
-
error_msg: str =
|
|
|
|
|
|
|
| 398 |
print(error_msg)
|
| 399 |
|
| 400 |
# Update job status to failed and raise an exception
|
|
@@ -438,7 +440,9 @@ async def process_on_demand_job(request: Request):
|
|
| 438 |
|
| 439 |
final_comments = comments_for_entity[: settings.ON_DEMAND_TOTAL_COMMENTS]
|
| 440 |
if not final_comments:
|
| 441 |
-
error_msg =
|
|
|
|
|
|
|
| 442 |
print(error_msg)
|
| 443 |
|
| 444 |
# Update job status to failed and raise an exception
|
|
@@ -455,10 +459,16 @@ async def process_on_demand_job(request: Request):
|
|
| 455 |
raise HTTPException(status_code=404, detail=error_msg)
|
| 456 |
|
| 457 |
# 3. Perform Sentiment Analysis
|
| 458 |
-
print(
|
|
|
|
|
|
|
| 459 |
texts_to_predict = [comment.get("text", "") for comment in final_comments]
|
| 460 |
-
predictions =
|
| 461 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 462 |
|
| 463 |
# 4. Save raw data and aggregate counts in memory to Database (similar to a mini-consumer)
|
| 464 |
|
|
|
|
| 366 |
|
| 367 |
# 1. Initialization
|
| 368 |
job_data = await request.json()
|
| 369 |
+
# print(job_data)
|
| 370 |
keyword = job_data.get("keyword")
|
| 371 |
job_id = job_data.get("job_id")
|
| 372 |
|
|
|
|
| 394 |
# Note: For on-demand, I might use a smaller fetching strategy
|
| 395 |
videos = yt_service.search_videos(query_string=keyword)
|
| 396 |
if not videos:
|
| 397 |
+
error_msg: str = (
|
| 398 |
+
f"No videos found for on-demand keyword '{keyword}' of job {job_id}."
|
| 399 |
+
)
|
| 400 |
print(error_msg)
|
| 401 |
|
| 402 |
# Update job status to failed and raise an exception
|
|
|
|
| 440 |
|
| 441 |
final_comments = comments_for_entity[: settings.ON_DEMAND_TOTAL_COMMENTS]
|
| 442 |
if not final_comments:
|
| 443 |
+
error_msg = (
|
| 444 |
+
f"No comments found for on-demand keyword '{keyword}' of job {job_id}."
|
| 445 |
+
)
|
| 446 |
print(error_msg)
|
| 447 |
|
| 448 |
# Update job status to failed and raise an exception
|
|
|
|
| 459 |
raise HTTPException(status_code=404, detail=error_msg)
|
| 460 |
|
| 461 |
# 3. Perform Sentiment Analysis
|
| 462 |
+
print(
|
| 463 |
+
f"Analyzing {len(final_comments)} comments in batches for job {job_id} to background thread..."
|
| 464 |
+
)
|
| 465 |
texts_to_predict = [comment.get("text", "") for comment in final_comments]
|
| 466 |
+
predictions = await asyncio.to_thread(
|
| 467 |
+
sentiment_service.predict, texts_to_predict
|
| 468 |
+
)
|
| 469 |
+
print(
|
| 470 |
+
f"Successfully analyzed {len(final_comments)} comments for job {job_id}!!!"
|
| 471 |
+
)
|
| 472 |
|
| 473 |
# 4. Save raw data and aggregate counts in memory to Database (similar to a mini-consumer)
|
| 474 |
|