geethareddy commited on
Commit
16dbf0f
·
verified ·
1 Parent(s): e7c1a90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -1,12 +1,20 @@
1
- from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import json
5
  import logging
6
  import os
 
7
 
8
- # Set up logging
9
- logging.basicConfig(level=logging.INFO)
 
 
 
 
 
 
 
10
  logger = logging.getLogger(__name__)
11
 
12
  # Initialize FastAPI app
@@ -26,10 +34,10 @@ model_load_status = "not_loaded"
26
 
27
  # Define model path and fallback
28
  model_path = "/app/fine-tuned-construction-llm"
29
- fallback_model = "distilgpt2" # Smaller model for faster loading
30
 
31
- # Load model and tokenizer at startup
32
- def load_model():
33
  global model, tokenizer, model_load_status
34
  try:
35
  if os.path.isdir(model_path):
@@ -38,7 +46,7 @@ def load_model():
38
  tokenizer = AutoTokenizer.from_pretrained(model_path, local_files_only=True)
39
  model_load_status = "local_model_loaded"
40
  else:
41
- logger.warning(f"Model directory not found: {model_path}. Falling back to pre-trained model: {fallback_model}")
42
  model = AutoModelForCausalLM.from_pretrained(fallback_model)
43
  tokenizer = AutoTokenizer.from_pretrained(fallback_model)
44
  model_load_status = "fallback_model_loaded"
@@ -46,24 +54,25 @@ def load_model():
46
  except Exception as e:
47
  logger.error(f"Failed to load model or tokenizer: {str(e)}")
48
  model_load_status = f"failed: {str(e)}"
49
- # Do not raise an exception; allow the app to start
50
-
51
- # Load model on startup
52
- load_model()
53
 
 
54
  @app.on_event("startup")
55
- async def startup_event():
56
  logger.info("FastAPI application started")
 
57
 
58
  @app.get("/health")
59
  async def health_check():
60
- return {"status": "healthy", "model_load_status": model_load_status}
 
 
 
61
 
62
  @app.post("/generate_coaching")
63
  async def generate_coaching(data: CoachingInput):
64
  if model is None or tokenizer is None:
65
  logger.error("Model or tokenizer not loaded")
66
- raise HTTPException(status_code=503, detail="Model not loaded. Please check server logs.")
67
 
68
  try:
69
  # Prepare input text
 
1
+ from fastapi import FastAPI, HTTPException, BackgroundTasks
2
  from pydantic import BaseModel
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import json
5
  import logging
6
  import os
7
+ import asyncio
8
 
9
+ # Set up logging to both stdout and a file
10
+ logging.basicConfig(
11
+ level=logging.INFO,
12
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
13
+ handlers=[
14
+ logging.StreamHandler(), # Log to stdout
15
+ logging.FileHandler("/app/app.log") # Log to a file
16
+ ]
17
+ )
18
  logger = logging.getLogger(__name__)
19
 
20
  # Initialize FastAPI app
 
34
 
35
  # Define model path and fallback
36
  model_path = "/app/fine-tuned-construction-llm"
37
+ fallback_model = "distilgpt2"
38
 
39
+ # Asynchronous function to load model in the background
40
+ async def load_model_background():
41
  global model, tokenizer, model_load_status
42
  try:
43
  if os.path.isdir(model_path):
 
46
  tokenizer = AutoTokenizer.from_pretrained(model_path, local_files_only=True)
47
  model_load_status = "local_model_loaded"
48
  else:
49
+ logger.info(f"Model directory not found: {model_path}. Using pre-trained model: {fallback_model}")
50
  model = AutoModelForCausalLM.from_pretrained(fallback_model)
51
  tokenizer = AutoTokenizer.from_pretrained(fallback_model)
52
  model_load_status = "fallback_model_loaded"
 
54
  except Exception as e:
55
  logger.error(f"Failed to load model or tokenizer: {str(e)}")
56
  model_load_status = f"failed: {str(e)}"
 
 
 
 
57
 
58
+ # Startup event to initiate model loading in the background
59
  @app.on_event("startup")
60
+ async def startup_event(background_tasks: BackgroundTasks):
61
  logger.info("FastAPI application started")
62
+ background_tasks.add_task(load_model_background)
63
 
64
  @app.get("/health")
65
  async def health_check():
66
+ return {
67
+ "status": "healthy" if model_load_status in ["local_model_loaded", "fallback_model_loaded"] else "starting",
68
+ "model_load_status": model_load_status
69
+ }
70
 
71
  @app.post("/generate_coaching")
72
  async def generate_coaching(data: CoachingInput):
73
  if model is None or tokenizer is None:
74
  logger.error("Model or tokenizer not loaded")
75
+ raise HTTPException(status_code=503, detail="Model not loaded yet. Please try again later.")
76
 
77
  try:
78
  # Prepare input text