Spaces:
Sleeping
Sleeping
Add application file
Browse files
main.py
CHANGED
@@ -8,25 +8,24 @@ The above copyright notice and this permission notice shall be included in all c
|
|
8 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
9 |
'''
|
10 |
import json
|
11 |
-
from werkzeug.exceptions import HTTPException,BadRequest,UnprocessableEntity,InternalServerError
|
12 |
from flask import Flask
|
13 |
-
from router.router import app
|
14 |
from waitress import serve
|
15 |
import os
|
16 |
from flask_swagger_ui import get_swaggerui_blueprint
|
17 |
from flask_cors import CORS
|
18 |
-
from config.logger import CustomLogger, request_id_var
|
19 |
-
# import os
|
20 |
from dotenv import load_dotenv
|
|
|
21 |
load_dotenv()
|
|
|
22 |
request_id_var.set("StartUp")
|
|
|
23 |
SWAGGER_URL = '/rai/v1/moderations/docs' # URL for exposing Swagger UI (without trailing '/')
|
24 |
-
# API_URL = '/static/metadata.json' # Our API url (can of course be a local resource)
|
25 |
-
# API_URL = 'src/config/swagger/metadata.json' # Our API url (can of course be a local resource)
|
26 |
API_URL = '/static/metadata.json' # Our API url (can of course be a local resource)
|
27 |
|
28 |
-
|
29 |
-
# Call factory function to create our blueprint
|
30 |
swaggerui_blueprint = get_swaggerui_blueprint(
|
31 |
SWAGGER_URL, # Swagger UI static files will be mapped to '{SWAGGER_URL}/dist/'
|
32 |
API_URL,
|
@@ -34,26 +33,22 @@ swaggerui_blueprint = get_swaggerui_blueprint(
|
|
34 |
'app_name': "Infosys Responsible AI - Moderation"
|
35 |
},
|
36 |
)
|
37 |
-
|
|
|
38 |
app1 = Flask(__name__)
|
39 |
-
# app1.register_blueprint(app)
|
40 |
|
41 |
-
#
|
|
|
|
|
42 |
|
|
|
43 |
CORS(app1)
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
app1.register_blueprint(swaggerui_blueprint)
|
49 |
-
|
50 |
-
|
51 |
@app1.errorhandler(HTTPException)
|
52 |
def handle_exception(e):
|
53 |
"""Return JSON instead of HTML for HTTP errors."""
|
54 |
-
# start with the correct headers and status code from the error
|
55 |
response = e.get_response()
|
56 |
-
# replace the body with JSON
|
57 |
response.data = json.dumps({
|
58 |
"code": e.code,
|
59 |
"details": e.description,
|
@@ -63,38 +58,35 @@ def handle_exception(e):
|
|
63 |
|
64 |
@app1.errorhandler(UnprocessableEntity)
|
65 |
def validation_error_handler(exc):
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
exc_code
|
73 |
-
exc_desc
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
})
|
78 |
-
response.content_type = "application/json"
|
79 |
-
return response
|
80 |
|
81 |
@app1.errorhandler(InternalServerError)
|
82 |
-
def
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
return response
|
94 |
-
|
95 |
-
|
96 |
if __name__ == "__main__":
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
99 |
|
100 |
|
|
|
8 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
9 |
'''
|
10 |
import json
|
11 |
+
from werkzeug.exceptions import HTTPException, BadRequest, UnprocessableEntity, InternalServerError
|
12 |
from flask import Flask
|
13 |
+
from router.router import app # Assuming 'app' is a Flask blueprint
|
14 |
from waitress import serve
|
15 |
import os
|
16 |
from flask_swagger_ui import get_swaggerui_blueprint
|
17 |
from flask_cors import CORS
|
18 |
+
from config.logger import CustomLogger, request_id_var
|
|
|
19 |
from dotenv import load_dotenv
|
20 |
+
|
21 |
load_dotenv()
|
22 |
+
|
23 |
request_id_var.set("StartUp")
|
24 |
+
|
25 |
SWAGGER_URL = '/rai/v1/moderations/docs' # URL for exposing Swagger UI (without trailing '/')
|
|
|
|
|
26 |
API_URL = '/static/metadata.json' # Our API url (can of course be a local resource)
|
27 |
|
28 |
+
# Swagger UI Blueprint setup
|
|
|
29 |
swaggerui_blueprint = get_swaggerui_blueprint(
|
30 |
SWAGGER_URL, # Swagger UI static files will be mapped to '{SWAGGER_URL}/dist/'
|
31 |
API_URL,
|
|
|
33 |
'app_name': "Infosys Responsible AI - Moderation"
|
34 |
},
|
35 |
)
|
36 |
+
|
37 |
+
# Create Flask app instance
|
38 |
app1 = Flask(__name__)
|
|
|
39 |
|
40 |
+
# Register Blueprints (Flask app and Swagger UI)
|
41 |
+
app1.register_blueprint(app) # app is the Flask Blueprint
|
42 |
+
app1.register_blueprint(swaggerui_blueprint)
|
43 |
|
44 |
+
# CORS configuration
|
45 |
CORS(app1)
|
46 |
|
47 |
+
# Error Handlers
|
|
|
|
|
|
|
|
|
|
|
48 |
@app1.errorhandler(HTTPException)
|
49 |
def handle_exception(e):
|
50 |
"""Return JSON instead of HTML for HTTP errors."""
|
|
|
51 |
response = e.get_response()
|
|
|
52 |
response.data = json.dumps({
|
53 |
"code": e.code,
|
54 |
"details": e.description,
|
|
|
58 |
|
59 |
@app1.errorhandler(UnprocessableEntity)
|
60 |
def validation_error_handler(exc):
|
61 |
+
"""Return JSON instead of HTML for validation errors (422)."""
|
62 |
+
response = exc.get_response()
|
63 |
+
exc_code_desc = exc.description.split("-")
|
64 |
+
exc_code = int(exc_code_desc[0])
|
65 |
+
exc_desc = exc_code_desc[1]
|
66 |
+
response.data = json.dumps({
|
67 |
+
"code": exc_code,
|
68 |
+
"details": exc_desc,
|
69 |
+
})
|
70 |
+
response.content_type = "application/json"
|
71 |
+
return response
|
|
|
|
|
|
|
72 |
|
73 |
@app1.errorhandler(InternalServerError)
|
74 |
+
def internal_server_error_handler(exc):
|
75 |
+
"""Return JSON instead of HTML for internal server errors (500)."""
|
76 |
+
response = exc.get_response()
|
77 |
+
response.data = json.dumps({
|
78 |
+
"code": 500,
|
79 |
+
"details": "Some Error Occurred, Please try later",
|
80 |
+
})
|
81 |
+
response.content_type = "application/json"
|
82 |
+
return response
|
83 |
+
|
84 |
+
# Start the server using waitress
|
|
|
|
|
|
|
85 |
if __name__ == "__main__":
|
86 |
+
request_id_var.set("StartUp")
|
87 |
+
serve(app1, host='0.0.0.0', port=int(os.getenv("PORT", 7860)),
|
88 |
+
threads=int(os.getenv('THREADS', 6)),
|
89 |
+
connection_limit=int(os.getenv('CONNECTION_LIMIT', 500)),
|
90 |
+
channel_timeout=int(os.getenv('CHANNEL_TIMEOUT', 120)))
|
91 |
|
92 |
|