Kiran5 commited on
Commit
4e1d594
·
1 Parent(s): 08c8442

adding files

Browse files
Files changed (1) hide show
  1. main.py +41 -54
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 dotenv import load_dotenv
81
- import spacy
82
- from spacy.cli import download
83
 
84
  # Load environment variables
85
  load_dotenv()
86
 
87
- # Flask app setup
 
 
 
 
 
 
 
88
  app1 = Flask(__name__)
89
 
90
- # Swagger UI setup
91
- SWAGGER_URL = '/rai/v1/moderations/docs'
92
- API_URL = '/static/metadata.json'
93
- swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL, config={'app_name': "Infosys Responsible AI - Moderation"})
 
 
 
 
 
94
  app1.register_blueprint(swaggerui_blueprint)
95
 
96
- # CORS setup
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 HTTP errors."""
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 HTTP errors."""
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 that log directories exist and are writable
156
- log_dir = '/home/user/logs' # Update to a directory inside user's home directory
157
- if not os.path.exists(log_dir):
158
- os.makedirs(log_dir, exist_ok=True)
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
- # Debugging: log environment variables
167
- logging.info(f"PORT: {os.getenv('PORT', 'Not Set')}")
168
- logging.info(f"THREADS: {os.getenv('THREADS', 'Not Set')}")
169
- logging.info(f"CONNECTION_LIMIT: {os.getenv('CONNECTION_LIMIT', 'Not Set')}")
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
- logging.info("Starting Flask application...")
176
- port = int(os.getenv("PORT", 7860)) # Default to 7860 if PORT is not set
177
- logging.info(f"Flask app is starting on port {port}...")
178
- serve(app1, host="0.0.0.0", port=port, threads=int(os.getenv('THREADS', 6)),
179
- connection_limit=int(os.getenv('CONNECTION_LIMIT', 500)),
 
 
 
180
  channel_timeout=int(os.getenv('CHANNEL_TIMEOUT', 120)))
181
  except Exception as e:
182
- logging.error(f"Error starting Flask application with Waitress: {e}")
183
- raise
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