AurelioAguirre commited on
Commit
0af4a83
·
1 Parent(s): be8d239

changed to uvicorn setup for HF v4

Browse files
Files changed (1) hide show
  1. main/main.py +32 -51
main/main.py CHANGED
@@ -4,7 +4,6 @@ LLM Inference Server main application using LitServe framework.
4
  import litserve as ls
5
  import yaml
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
@@ -24,58 +23,40 @@ def load_config():
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
- # Initialize the router
61
- await init_router(api)
62
- # Launch the inference worker
63
- server.launch_inference_worker()
64
 
65
- @app.on_event("shutdown")
66
- async def shutdown_event():
67
- """Cleanup on shutdown."""
68
- server.stop_inference_worker()
 
 
 
 
69
 
70
- async def run_server():
71
- """Run the server directly (not through uvicorn)"""
72
- port = server_config.get('port', 8001)
73
- host = server_config.get('host', '0.0.0.0')
74
- server.run(host=host, port=port)
75
 
76
- def main():
77
- """Entry point that runs the server directly"""
78
- asyncio.run(run_server())
79
 
80
- if __name__ == "__main__":
81
- main()
 
4
  import litserve as ls
5
  import yaml
6
  import logging
 
7
  from pathlib import Path
8
  from fastapi.middleware.cors import CORSMiddleware
9
  from .routes import router, init_router
 
23
  with open(config_path) as f:
24
  return yaml.safe_load(f)
25
 
26
+ def create_app():
27
+ """Create and configure the application instance."""
28
+ logger = setup_logging()
29
+ config = load_config()
30
+ server_config = config.get('server', {})
31
+
32
+ # Initialize API with config
33
+ api = InferenceApi(config)
34
+
35
+ # Create LitServer instance
36
+ server = ls.LitServer(
37
+ api,
38
+ timeout=server_config.get('timeout', 60),
39
+ max_batch_size=server_config.get('max_batch_size', 1),
40
+ track_requests=True
41
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
+ # Get the FastAPI app
44
+ app = server.app
 
 
 
 
 
45
 
46
+ # Add CORS middleware
47
+ app.add_middleware(
48
+ CORSMiddleware,
49
+ allow_origins=["*"],
50
+ allow_credentials=True,
51
+ allow_methods=["*"],
52
+ allow_headers=["*"],
53
+ )
54
 
55
+ # Add routes with configured prefix
56
+ api_prefix = config.get('llm_server', {}).get('api_prefix', '/api/v1')
57
+ app.include_router(router, prefix=api_prefix)
 
 
58
 
59
+ return app
 
 
60
 
61
+ # Create the app instance for uvicorn
62
+ app = create_app()