-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_api.py
More file actions
67 lines (52 loc) · 2.01 KB
/
Copy pathrun_api.py
File metadata and controls
67 lines (52 loc) · 2.01 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import argparse
def run_development():
"""Run the API in development mode using Flask's built-in server"""
from api import app, initialize_models, model, vectorizer
from file_api import register_file_blueprint
# Initialize model
success = initialize_models()
if not success:
print("Failed to initialize models. API may not function correctly.")
return
# Register the file API blueprint
register_file_blueprint(app, model, vectorizer)
# Get port from environment or use default
port = int(os.environ.get("PORT", 5000))
# Run Flask app in debug mode
app.run(host='0.0.0.0', port=port, debug=True)
def run_production(workers=4):
"""Run the API in production mode using gunicorn"""
# Get port from environment or use default
port = int(os.environ.get("PORT", 5000))
# Create a wrapper module for gunicorn
with open("wsgi.py", "w") as f:
f.write("""
from api import app, initialize_models, model, vectorizer
from file_api import register_file_blueprint
# Initialize model
success = initialize_models()
if not success:
print("Failed to initialize models. API may not function correctly.")
# Register the file API blueprint
register_file_blueprint(app, model, vectorizer)
""")
# Build gunicorn command
cmd = f"gunicorn --bind 0.0.0.0:{port} --workers {workers} wsgi:app"
# Execute gunicorn
os.system(cmd)
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Run the SCAM Detection API')
parser.add_argument('--production', action='store_true', help='Run in production mode with gunicorn')
parser.add_argument('--workers', type=int, default=4, help='Number of gunicorn workers (only for production mode)')
args = parser.parse_args()
if args.production:
run_production(workers=args.workers)
else:
run_development()
if __name__ == "__main__":
main()