Spaces:
Sleeping
Sleeping
File size: 1,739 Bytes
0e86186 f8e356e a46266e 9bb7963 12b1972 f8e356e de82359 077c30a a46266e 12b1972 f8e356e 426c4b1 d940eca f8e356e 077c30a de82359 077c30a f8e356e dc7ceea de82359 dc7ceea 10701c7 dc7ceea 10701c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import os
import asyncio
from smart_search import SmartSearch
# from dotenv import load_dotenv
# load_dotenv()
app = FastAPI()
LOAD_BALANCER_URL = os.getenv("LOAD_BALANCER_URL")
search_system = SmartSearch(
films_url=f'{LOAD_BALANCER_URL}/api/get/movie/all',
tv_series_url=f'{LOAD_BALANCER_URL}/api/get/series/all'
)
search_system_lock = asyncio.Lock()
@app.on_event("startup")
async def startup_event():
# Start the background task for updating data and initializing
asyncio.create_task(update_data_periodically())
async def update_data_periodically():
while True:
async with search_system_lock:
await search_system.update_data()
await asyncio.sleep(120) # Sleep for 2 minutes (120 seconds)
class Query(BaseModel):
query: str
@app.get("/")
async def index():
return "Server Running ..."
@app.post("/api/search")
async def search(query: Query):
query_str = query.query
if not query_str:
raise HTTPException(status_code=400, detail="Missing 'query' field in JSON body")
async with search_system_lock:
if not search_system.is_initialized:
raise HTTPException(status_code=503, detail="Search system is not initialized yet.")
results = search_system.search(query_str)
return results
@app.get("/api/data")
async def get_data():
async with search_system_lock:
if not search_system.is_initialized:
raise HTTPException(status_code=503, detail="Search system is not initialized yet.")
films = search_system.films
tv_series = search_system.tv_series
return {"films": films, "tv_series": tv_series}
|