abhisheksan commited on
Commit
abc61cb
1 Parent(s): f55cd01

Improve model preloading in PoetryGenerationService with meaningful return value and enhanced error handling

Browse files
Files changed (2) hide show
  1. app/services/poetry_generation.py +2 -1
  2. main.py +10 -8
app/services/poetry_generation.py CHANGED
@@ -65,9 +65,10 @@ class PoetryGenerationService:
65
  try:
66
  _ = ModelManager() # Ensure ModelManager singleton is initialized
67
  logger.info("Models preloaded successfully")
 
68
  except Exception as e:
69
  logger.error(f"Error preloading models: {str(e)}")
70
- raise
71
 
72
  def generate_poem(
73
  self,
 
65
  try:
66
  _ = ModelManager() # Ensure ModelManager singleton is initialized
67
  logger.info("Models preloaded successfully")
68
+ return True # Return a meaningful value
69
  except Exception as e:
70
  logger.error(f"Error preloading models: {str(e)}")
71
+ raise Exception("Failed to preload models") from e
72
 
73
  def generate_poem(
74
  self,
main.py CHANGED
@@ -40,17 +40,19 @@ def init_huggingface():
40
  async def lifespan(app: FastAPI):
41
  # Initialize Hugging Face authentication first
42
  init_huggingface()
43
-
44
  # Initialize poetry service and preload models
45
  poetry_service = PoetryGenerationService()
46
-
47
- # Only await if `preload_models` is asynchronous
48
- if callable(getattr(poetry_service, "preload_models", None)):
49
- result = poetry_service.preload_models()
50
- if asyncio.iscoroutine(result):
51
- await result
52
  else:
53
- result() # Call directly if synchronous
 
 
 
54
 
55
  yield # Continue to application startup
56
 
 
40
  async def lifespan(app: FastAPI):
41
  # Initialize Hugging Face authentication first
42
  init_huggingface()
43
+
44
  # Initialize poetry service and preload models
45
  poetry_service = PoetryGenerationService()
46
+
47
+ try:
48
+ preload_result = poetry_service.preload_models()
49
+ if asyncio.iscoroutine(preload_result):
50
+ await preload_result
 
51
  else:
52
+ preload_result # Call directly if synchronous
53
+ except Exception as e:
54
+ logger.error(f"Error preloading models: {str(e)}")
55
+ raise
56
 
57
  yield # Continue to application startup
58