-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_server.py
More file actions
136 lines (115 loc) · 3.84 KB
/
start_server.py
File metadata and controls
136 lines (115 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
"""
Startup script for DynamicGroundingDINO API
Supports both development and production modes with automatic worker scaling
"""
import os
import sys
import multiprocessing
import subprocess
import argparse
from pathlib import Path
def get_optimal_workers():
"""Calculate optimal number of workers based on system resources"""
cpu_count = multiprocessing.cpu_count()
# For AI workloads, we typically use fewer workers than CPU cores
# because each worker will be CPU and memory intensive
if cpu_count <= 2:
return 1
elif cpu_count <= 4:
return 2
elif cpu_count <= 8:
return min(4, cpu_count - 1)
else:
return min(6, cpu_count // 2)
def run_development():
"""Run in development mode with auto-reload"""
print("🚀 Starting DynamicGroundingDINO API in DEVELOPMENT mode...")
print("📝 Features: Auto-reload, Debug logging, Single worker")
cmd = [
"uvicorn",
"server:app",
"--host", "0.0.0.0",
"--port", "8000",
"--reload",
"--log-level", "debug",
"--access-log"
]
subprocess.run(cmd)
def run_production(workers=None, max_requests=None):
"""Run in production mode with Gunicorn"""
workers = workers or get_optimal_workers()
max_requests = max_requests or 1000
print(f"🚀 Starting DynamicGroundingDINO API in PRODUCTION mode...")
print(f"👥 Workers: {workers}")
print(f"🔄 Max requests per worker: {max_requests}")
print(f"⚡ Worker class: uvicorn.workers.UvicornWorker")
# Set environment variables for Gunicorn config
os.environ["WORKERS"] = str(workers)
os.environ["MAX_REQUESTS"] = str(max_requests)
cmd = [
"gunicorn",
"server:app",
"-c", "gunicorn.conf.py"
]
subprocess.run(cmd)
def run_with_custom_config(config_file):
"""Run with custom Gunicorn configuration"""
print(f"🚀 Starting with custom config: {config_file}")
cmd = [
"gunicorn",
"server:app",
"-c", config_file
]
subprocess.run(cmd)
def main():
parser = argparse.ArgumentParser(description="DynamicGroundingDINO API Server")
parser.add_argument(
"--mode",
choices=["dev", "prod", "custom"],
default="dev",
help="Server mode (default: dev)"
)
parser.add_argument(
"--workers",
type=int,
help="Number of worker processes (production mode only)"
)
parser.add_argument(
"--max-requests",
type=int,
default=1000,
help="Maximum requests per worker before restart (production mode only)"
)
parser.add_argument(
"--config",
help="Custom Gunicorn configuration file (custom mode only)"
)
args = parser.parse_args()
# Check if required files exist
if not Path("server.py").exists():
print("❌ Error: server.py not found!")
sys.exit(1)
if args.mode == "prod" and not Path("gunicorn.conf.py").exists():
print("❌ Error: gunicorn.conf.py not found!")
sys.exit(1)
# Print environment info
print("=" * 60)
print("🔍 DynamicGroundingDINO API Server")
print("=" * 60)
print(f"🖥️ CPU cores: {multiprocessing.cpu_count()}")
print(f"🎯 Recommended workers: {get_optimal_workers()}")
print(f"🐍 Python: {sys.version.split()[0]}")
print(f"📁 Working directory: {os.getcwd()}")
print("=" * 60)
if args.mode == "dev":
run_development()
elif args.mode == "prod":
run_production(args.workers, args.max_requests)
elif args.mode == "custom":
if not args.config:
print("❌ Error: --config required for custom mode!")
sys.exit(1)
run_with_custom_config(args.config)
if __name__ == "__main__":
main()