Spaces:
Sleeping
Sleeping
adding files
Browse files
main.py
CHANGED
@@ -74,47 +74,40 @@ import os
|
|
74 |
import logging
|
75 |
from werkzeug.exceptions import HTTPException, UnprocessableEntity, InternalServerError
|
76 |
from flask import Flask
|
|
|
77 |
from flask_swagger_ui import get_swaggerui_blueprint
|
78 |
from flask_cors import CORS
|
79 |
from waitress import serve
|
80 |
-
from
|
81 |
-
import
|
82 |
-
from spacy.cli import download
|
83 |
|
84 |
# Load environment variables
|
85 |
load_dotenv()
|
86 |
|
87 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
app1 = Flask(__name__)
|
89 |
|
90 |
-
# Swagger UI
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
94 |
app1.register_blueprint(swaggerui_blueprint)
|
95 |
|
96 |
-
# CORS
|
97 |
CORS(app1)
|
98 |
|
99 |
-
# Ensure Spacy model is installed and loaded
|
100 |
-
def load_spacy_model():
|
101 |
-
try:
|
102 |
-
# Attempt to load the model
|
103 |
-
nlp = spacy.load('en_core_web_lg')
|
104 |
-
except OSError:
|
105 |
-
# If model is not found, download it
|
106 |
-
download('en_core_web_lg')
|
107 |
-
nlp = spacy.load('en_core_web_lg')
|
108 |
-
return nlp
|
109 |
-
|
110 |
-
# Attempt to load Spacy model
|
111 |
-
try:
|
112 |
-
nlp = load_spacy_model()
|
113 |
-
logging.info("Spacy model loaded successfully.")
|
114 |
-
except Exception as e:
|
115 |
-
logging.error(f"Failed to load Spacy model: {e}")
|
116 |
-
raise
|
117 |
-
|
118 |
# Error Handlers
|
119 |
@app1.errorhandler(HTTPException)
|
120 |
def handle_exception(e):
|
@@ -129,7 +122,7 @@ def handle_exception(e):
|
|
129 |
|
130 |
@app1.errorhandler(UnprocessableEntity)
|
131 |
def validation_error_handler(exc):
|
132 |
-
"""Return JSON instead of HTML for
|
133 |
response = exc.get_response()
|
134 |
exc_code_desc = exc.description.split("-")
|
135 |
exc_code = int(exc_code_desc[0])
|
@@ -143,7 +136,7 @@ def validation_error_handler(exc):
|
|
143 |
|
144 |
@app1.errorhandler(InternalServerError)
|
145 |
def internal_server_error_handler(exc):
|
146 |
-
"""Return JSON instead of HTML for
|
147 |
response = exc.get_response()
|
148 |
response.data = json.dumps({
|
149 |
"code": 500,
|
@@ -152,35 +145,29 @@ def internal_server_error_handler(exc):
|
|
152 |
response.content_type = "application/json"
|
153 |
return response
|
154 |
|
155 |
-
# Ensure
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
logging.info(f"Created log directory at: {log_dir}")
|
160 |
-
else:
|
161 |
-
logging.info(f"Log directory already exists: {log_dir}")
|
162 |
-
|
163 |
-
# Configure basic logging
|
164 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
165 |
|
166 |
-
#
|
167 |
-
logging.info(f"
|
168 |
-
logging.info(f"
|
169 |
-
logging.info(f"
|
170 |
-
logging.info(f"CHANNEL_TIMEOUT: {os.getenv('CHANNEL_TIMEOUT', 'Not Set')}")
|
171 |
|
172 |
-
# Use Waitress for production server, or Flask for development
|
173 |
-
if __name__ == "__main__":
|
174 |
try:
|
175 |
-
|
176 |
-
port = int(os.getenv("PORT", 7860))
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
180 |
channel_timeout=int(os.getenv('CHANNEL_TIMEOUT', 120)))
|
181 |
except Exception as e:
|
182 |
-
logging.error(f"Error starting
|
183 |
-
|
184 |
|
185 |
|
186 |
|
|
|
74 |
import logging
|
75 |
from werkzeug.exceptions import HTTPException, UnprocessableEntity, InternalServerError
|
76 |
from flask import Flask
|
77 |
+
from dotenv import load_dotenv
|
78 |
from flask_swagger_ui import get_swaggerui_blueprint
|
79 |
from flask_cors import CORS
|
80 |
from waitress import serve
|
81 |
+
from router.router import app # Assuming router is correctly defined elsewhere
|
82 |
+
from config.logger import CustomLogger, request_id_var # Ensure logger is set up properly
|
|
|
83 |
|
84 |
# Load environment variables
|
85 |
load_dotenv()
|
86 |
|
87 |
+
# Initialize the request ID
|
88 |
+
request_id_var.set("StartUp")
|
89 |
+
|
90 |
+
# Set up Swagger UI and API URL
|
91 |
+
SWAGGER_URL = '/rai/v1/moderations/docs' # URL for exposing Swagger UI (without trailing '/')
|
92 |
+
API_URL = '/static/metadata.json' # Swagger API URL for the documentation
|
93 |
+
|
94 |
+
# Initialize the Flask app
|
95 |
app1 = Flask(__name__)
|
96 |
|
97 |
+
# Set up Swagger UI Blueprint
|
98 |
+
swaggerui_blueprint = get_swaggerui_blueprint(
|
99 |
+
SWAGGER_URL,
|
100 |
+
API_URL,
|
101 |
+
config={'app_name': "Infosys Responsible AI - Moderation"}
|
102 |
+
)
|
103 |
+
|
104 |
+
# Register Blueprints
|
105 |
+
app1.register_blueprint(app) # Assuming 'app' comes from the router
|
106 |
app1.register_blueprint(swaggerui_blueprint)
|
107 |
|
108 |
+
# Enable CORS (Cross-Origin Resource Sharing)
|
109 |
CORS(app1)
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
# Error Handlers
|
112 |
@app1.errorhandler(HTTPException)
|
113 |
def handle_exception(e):
|
|
|
122 |
|
123 |
@app1.errorhandler(UnprocessableEntity)
|
124 |
def validation_error_handler(exc):
|
125 |
+
"""Return JSON instead of HTML for UnprocessableEntity errors."""
|
126 |
response = exc.get_response()
|
127 |
exc_code_desc = exc.description.split("-")
|
128 |
exc_code = int(exc_code_desc[0])
|
|
|
136 |
|
137 |
@app1.errorhandler(InternalServerError)
|
138 |
def internal_server_error_handler(exc):
|
139 |
+
"""Return JSON instead of HTML for InternalServerError."""
|
140 |
response = exc.get_response()
|
141 |
response.data = json.dumps({
|
142 |
"code": 500,
|
|
|
145 |
response.content_type = "application/json"
|
146 |
return response
|
147 |
|
148 |
+
# Ensure application starts with waitress in production
|
149 |
+
if __name__ == "__main__":
|
150 |
+
# Ensure environment variables are set and log them
|
151 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
+
logging.info(f"PORT: {os.getenv('PORT', 7860)}") # Default to 7860 if not set
|
154 |
+
logging.info(f"THREADS: {os.getenv('THREADS', 6)}")
|
155 |
+
logging.info(f"CONNECTION_LIMIT: {os.getenv('CONNECTION_LIMIT', 500)}")
|
156 |
+
logging.info(f"CHANNEL_TIMEOUT: {os.getenv('CHANNEL_TIMEOUT', 120)}")
|
|
|
157 |
|
|
|
|
|
158 |
try:
|
159 |
+
# Get port from environment or default to 7860
|
160 |
+
port = int(os.getenv("PORT", 7860))
|
161 |
+
|
162 |
+
logging.info(f"Starting the application with Waitress on port {port}...")
|
163 |
+
# Start the server using waitress (production WSGI server)
|
164 |
+
serve(app1, host='0.0.0.0', port=port,
|
165 |
+
threads=int(os.getenv('THREADS', 6)),
|
166 |
+
connection_limit=int(os.getenv('CONNECTION_LIMIT', 500)),
|
167 |
channel_timeout=int(os.getenv('CHANNEL_TIMEOUT', 120)))
|
168 |
except Exception as e:
|
169 |
+
logging.error(f"Error starting the application: {e}")
|
170 |
+
|
171 |
|
172 |
|
173 |
|