AurelioAguirre commited on
Commit
baae755
·
1 Parent(s): c6b21e3

changed to uvicorn setup for HF v2

Browse files
Files changed (1) hide show
  1. main/main.py +34 -58
main/main.py CHANGED
@@ -6,7 +6,6 @@ import yaml
6
  import logging
7
  import asyncio
8
  from pathlib import Path
9
- from fastapi import FastAPI
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from .routes import router, init_router
12
  from .api import InferenceApi
@@ -25,68 +24,45 @@ def load_config():
25
  with open(config_path) as f:
26
  return yaml.safe_load(f)
27
 
28
- async def init_app() -> tuple[FastAPI, InferenceApi, dict]:
29
- """Initialize and configure the FastAPI application."""
30
- logger = setup_logging()
31
-
32
- try:
33
- # Load configuration
34
- config = load_config()
35
- server_config = config.get('server', {})
36
-
37
- # Initialize API with config
38
- api = InferenceApi(config)
39
-
40
- # Initialize router with the API instance
41
- await init_router(api)
42
-
43
- # Create LitServer instance with config
44
- server = ls.LitServer(
45
- api,
46
- timeout=server_config.get('timeout', 60),
47
- max_batch_size=server_config.get('max_batch_size', 1),
48
- track_requests=True
49
- )
50
-
51
- # Get the FastAPI app from the LitServer
52
- app = server.app
53
-
54
- # Add CORS middleware
55
- app.add_middleware(
56
- CORSMiddleware,
57
- allow_origins=["*"],
58
- allow_credentials=True,
59
- allow_methods=["*"],
60
- allow_headers=["*"],
61
- )
62
-
63
- # Add routes with configured prefix
64
- api_prefix = config.get('llm_server', {}).get('api_prefix', '/api/v1')
65
- app.include_router(router, prefix=api_prefix)
66
-
67
- return app, api, config
68
-
69
- except Exception as e:
70
- logger.error(f"Application initialization failed: {str(e)}")
71
- raise
72
-
73
- # Create the FastAPI app instance for uvicorn
74
- app, api_instance, config_dict = asyncio.get_event_loop().run_until_complete(init_app())
75
 
76
  async def run_server():
77
  """Run the server directly (not through uvicorn)"""
78
- server_config = config_dict.get('server', {})
79
  port = server_config.get('port', 8001)
80
  host = server_config.get('host', '0.0.0.0')
81
-
82
- # Create LitServer instance with all required parameters
83
- server = ls.LitServer(
84
- api_instance,
85
- timeout=server_config.get('timeout', 60),
86
- max_batch_size=server_config.get('max_batch_size', 1),
87
- track_requests=True
88
- )
89
-
90
  server.run(host=host, port=port)
91
 
92
  def main():
 
6
  import logging
7
  import asyncio
8
  from pathlib import Path
 
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from .routes import router, init_router
11
  from .api import InferenceApi
 
24
  with open(config_path) as f:
25
  return yaml.safe_load(f)
26
 
27
+ # Initialize everything synchronously
28
+ logger = setup_logging()
29
+ config = load_config()
30
+ server_config = config.get('server', {})
31
+ api = InferenceApi(config)
32
+
33
+ # Create LitServer instance
34
+ server = ls.LitServer(
35
+ api,
36
+ timeout=server_config.get('timeout', 60),
37
+ max_batch_size=server_config.get('max_batch_size', 1),
38
+ track_requests=True
39
+ )
40
+
41
+ # Get the FastAPI app from LitServer
42
+ app = server.app
43
+
44
+ # Add CORS middleware
45
+ app.add_middleware(
46
+ CORSMiddleware,
47
+ allow_origins=["*"],
48
+ allow_credentials=True,
49
+ allow_methods=["*"],
50
+ allow_headers=["*"],
51
+ )
52
+
53
+ # Add routes with configured prefix
54
+ api_prefix = config.get('llm_server', {}).get('api_prefix', '/api/v1')
55
+ app.include_router(router, prefix=api_prefix)
56
+
57
+ @app.on_event("startup")
58
+ async def startup_event():
59
+ """Initialize async components on startup."""
60
+ await init_router(api)
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  async def run_server():
63
  """Run the server directly (not through uvicorn)"""
 
64
  port = server_config.get('port', 8001)
65
  host = server_config.get('host', '0.0.0.0')
 
 
 
 
 
 
 
 
 
66
  server.run(host=host, port=port)
67
 
68
  def main():