Spaces:
Sleeping
Sleeping
''' | |
Copyright 2024 Infosys Ltd. | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
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. | |
''' | |
import json | |
from werkzeug.exceptions import HTTPException, UnprocessableEntity, InternalServerError | |
from flask import Flask | |
from router.router import app # Make sure this is a Flask Blueprint | |
from flask_swagger_ui import get_swaggerui_blueprint | |
from flask_cors import CORS | |
from dotenv import load_dotenv | |
import os | |
load_dotenv() | |
request_id_var.set("StartUp") | |
SWAGGER_URL = '/rai/v1/moderations/docs' | |
API_URL = '/static/metadata.json' | |
swaggerui_blueprint = get_swaggerui_blueprint( | |
SWAGGER_URL, API_URL, | |
config={'app_name': "Infosys Responsible AI - Moderation"} | |
) | |
# Create Flask app instance | |
app1 = Flask(__name__) | |
# Register blueprints | |
app1.register_blueprint(app) | |
app1.register_blueprint(swaggerui_blueprint) | |
CORS(app1) | |
# Error handlers | |
def handle_exception(e): | |
response = e.get_response() | |
response.data = json.dumps({ | |
"code": e.code, | |
"details": e.description, | |
}) | |
response.content_type = "application/json" | |
return response | |
def validation_error_handler(exc): | |
response = exc.get_response() | |
exc_code_desc = exc.description.split("-") | |
exc_code = int(exc_code_desc[0]) | |
exc_desc = exc_code_desc[1] | |
response.data = json.dumps({ | |
"code": exc_code, | |
"details": exc_desc, | |
}) | |
response.content_type = "application/json" | |
return response | |
def internal_server_error_handler(exc): | |
response = exc.get_response() | |
response.data = json.dumps({ | |
"code": 500, | |
"details": "Some Error Occurred, Please try later", | |
}) | |
response.content_type = "application/json" | |
return response | |
# Start Flask server | |
if __name__ == "__main__": | |
request_id_var.set("StartUp") | |
app1.run(host='0.0.0.0', port=int(os.getenv("PORT", 7860)), debug=True) | |