ChandimaPrabath commited on
Commit
12b1972
·
1 Parent(s): 1603f40

fix update

Browse files
Files changed (2) hide show
  1. main.py +10 -1
  2. smart_search.py +3 -7
main.py CHANGED
@@ -3,6 +3,9 @@ from pydantic import BaseModel
3
  import os
4
  import asyncio
5
  from smart_search import SmartSearch
 
 
 
6
 
7
  app = FastAPI()
8
  LOAD_BALANCER_URL = os.getenv("LOAD_BALANCER_URL")
@@ -16,7 +19,13 @@ search_system_lock = asyncio.Lock()
16
  @app.on_event("startup")
17
  async def startup_event():
18
  # Start the background task for updating data and initializing
19
- asyncio.create_task(search_system.update_data_and_initialize(search_system_lock))
 
 
 
 
 
 
20
 
21
  class Query(BaseModel):
22
  query: str
 
3
  import os
4
  import asyncio
5
  from smart_search import SmartSearch
6
+ # from dotenv import load_dotenv
7
+
8
+ # load_dotenv()
9
 
10
  app = FastAPI()
11
  LOAD_BALANCER_URL = os.getenv("LOAD_BALANCER_URL")
 
19
  @app.on_event("startup")
20
  async def startup_event():
21
  # Start the background task for updating data and initializing
22
+ asyncio.create_task(update_data_periodically())
23
+
24
+ async def update_data_periodically():
25
+ while True:
26
+ async with search_system_lock:
27
+ await search_system.update_data()
28
+ await asyncio.sleep(120) # Sleep for 2 minutes (120 seconds)
29
 
30
  class Query(BaseModel):
31
  query: str
smart_search.py CHANGED
@@ -49,12 +49,8 @@ class SmartSearch:
49
  self.index = self.create_index(self.films, self.tv_series)
50
  self.is_initialized = True
51
 
52
- async def update_data_and_initialize(self, lock: asyncio.Lock):
53
- while True:
54
- await self.initialize()
55
- async with lock:
56
- self.is_initialized = True
57
- await asyncio.sleep(120) # Sleep for 2 minutes (120 seconds)
58
 
59
  def create_index(self, films: Dict[str, str], tv_series: Dict[str, Dict[str, Union[str, List[Dict[str, str]]]]]) -> Dict[str, Union[Dict[str, str], Dict[str, Dict[str, Union[str, List[Dict[str, str]]]]]]]:
60
  return {
@@ -75,7 +71,7 @@ class SmartSearch:
75
  # Search TV series
76
  tv_series = self.index['tv_series']
77
  for series, data in tv_series.items():
78
- if query in series or fuzz.partial_ratio(query, series) >50: # Adjusted threshold
79
  results['tv_series'].append(data['original'])
80
  else:
81
  for episode in data['episodes']:
 
49
  self.index = self.create_index(self.films, self.tv_series)
50
  self.is_initialized = True
51
 
52
+ async def update_data(self):
53
+ await self.initialize()
 
 
 
 
54
 
55
  def create_index(self, films: Dict[str, str], tv_series: Dict[str, Dict[str, Union[str, List[Dict[str, str]]]]]) -> Dict[str, Union[Dict[str, str], Dict[str, Dict[str, Union[str, List[Dict[str, str]]]]]]]:
56
  return {
 
71
  # Search TV series
72
  tv_series = self.index['tv_series']
73
  for series, data in tv_series.items():
74
+ if query in series or fuzz.partial_ratio(query, series) > 50: # Adjusted threshold
75
  results['tv_series'].append(data['original'])
76
  else:
77
  for episode in data['episodes']: