VOIDER commited on
Commit
f5b3885
·
verified ·
1 Parent(s): dcbe972

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -212,9 +212,14 @@ class ModelManager:
212
  "anime_aesthetic": {"name": "Anime Score", "process": self._process_anime_aesthetic, "model": self.anime_aesthetic_model},
213
  }
214
  self.processing_queue: asyncio.Queue = asyncio.Queue()
215
- self.worker_task = asyncio.create_task(self._worker())
216
  self.temp_dir = tempfile.mkdtemp()
217
 
 
 
 
 
 
218
  async def _worker(self):
219
  """Background worker to process image evaluation requests from the queue."""
220
  while True:
@@ -494,13 +499,15 @@ class ModelManager:
494
  """Clean up temporary directories and shutdown worker."""
495
  if os.path.exists(self.temp_dir):
496
  shutil.rmtree(self.temp_dir)
497
- asyncio.run(self.shutdown()) # Shutdown worker gracefully
 
498
 
499
  async def shutdown(self):
500
  """Send shutdown signal to worker and wait for it to finish."""
501
- await self.processing_queue.put(None) # Send shutdown signal
502
- await self.worker_task # Wait for worker task to complete
503
- await self.processing_queue.join() # Wait for queue to be empty
 
504
 
505
 
506
  #####################################
@@ -683,6 +690,8 @@ def create_interface():
683
  outputs=[download_file_output] # Output is now the gr.File component
684
  )
685
  demo.load(lambda: update_table_sort("Final Score", model_options, []), inputs=None, outputs=[output_html, global_results_state]) # Initial sort and table render, pass empty initial results
 
 
686
  gr.Markdown("""
687
  ### Notes
688
  - Select models to use for evaluation using the checkboxes.
 
212
  "anime_aesthetic": {"name": "Anime Score", "process": self._process_anime_aesthetic, "model": self.anime_aesthetic_model},
213
  }
214
  self.processing_queue: asyncio.Queue = asyncio.Queue()
215
+ self.worker_task = None # Initialize worker_task to None
216
  self.temp_dir = tempfile.mkdtemp()
217
 
218
+ async def start_worker(self):
219
+ """Start the background worker task."""
220
+ if self.worker_task is None:
221
+ self.worker_task = asyncio.create_task(self._worker())
222
+
223
  async def _worker(self):
224
  """Background worker to process image evaluation requests from the queue."""
225
  while True:
 
499
  """Clean up temporary directories and shutdown worker."""
500
  if os.path.exists(self.temp_dir):
501
  shutil.rmtree(self.temp_dir)
502
+ if self.worker_task is not None: # Check if worker_task was started
503
+ asyncio.run(self.shutdown()) # Shutdown worker gracefully
504
 
505
  async def shutdown(self):
506
  """Send shutdown signal to worker and wait for it to finish."""
507
+ if self.worker_task is not None: # Check if worker_task was started
508
+ await self.processing_queue.put(None) # Send shutdown signal
509
+ await self.worker_task # Wait for worker task to complete
510
+ await self.processing_queue.join() # Wait for queue to be empty
511
 
512
 
513
  #####################################
 
690
  outputs=[download_file_output] # Output is now the gr.File component
691
  )
692
  demo.load(lambda: update_table_sort("Final Score", model_options, []), inputs=None, outputs=[output_html, global_results_state]) # Initial sort and table render, pass empty initial results
693
+ demo.load(model_manager.start_worker) # Start the worker task on demo load
694
+
695
  gr.Markdown("""
696
  ### Notes
697
  - Select models to use for evaluation using the checkboxes.