Kiran5 commited on
Commit
4a14e46
·
1 Parent(s): 001c169

Add application file

Browse files
Files changed (1) hide show
  1. main.py +68 -1
main.py CHANGED
@@ -4,6 +4,68 @@ Copyright 2024 Infosys Ltd.
4
  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:
5
 
6
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
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
  '''
@@ -15,6 +77,7 @@ from flask_cors import CORS
15
  from waitress import serve
16
  import os
17
  from dotenv import load_dotenv
 
18
  load_dotenv()
19
 
20
  # Flask app setup
@@ -26,6 +89,9 @@ API_URL = '/static/metadata.json'
26
  swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL, config={'app_name': "Infosys Responsible AI - Moderation"})
27
  app1.register_blueprint(swaggerui_blueprint)
28
 
 
 
 
29
  # CORS and error handling setup
30
  CORS(app1)
31
 
@@ -64,7 +130,8 @@ def internal_server_error_handler(exc):
64
 
65
  # Use Waitress for production server
66
  if __name__ == "__main__":
67
- serve(app1, host="0.0.0.0", port=7860)
 
68
 
69
 
70
 
 
4
  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:
5
 
6
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+ import json
8
+ from werkzeug.exceptions import HTTPException, UnprocessableEntity, InternalServerError
9
+ from flask import Flask
10
+ from flask_swagger_ui import get_swaggerui_blueprint
11
+ from flask_cors import CORS
12
+ from waitress import serve
13
+ import os
14
+ from dotenv import load_dotenv
15
+ from router.router import app # Importing the original blueprint
16
+ load_dotenv()
17
+
18
+ # Flask app setup
19
+ app1 = Flask(__name__)
20
+
21
+ # Swagger UI setup
22
+ SWAGGER_URL = '/rai/v1/moderations/docs'
23
+ API_URL = '/static/metadata.json'
24
+ swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL, config={'app_name': "Infosys Responsible AI - Moderation"})
25
+ app1.register_blueprint(swaggerui_blueprint)
26
+
27
+ # Register the app blueprint for the '/rai/v1/moderations' route
28
+ app1.register_blueprint(app) # Registering the blueprint that contains the route
29
+
30
+ # CORS and error handling setup
31
+ CORS(app1)
32
+
33
+ @app1.errorhandler(HTTPException)
34
+ def handle_exception(e):
35
+ response = e.get_response()
36
+ response.data = json.dumps({
37
+ "code": e.code,
38
+ "details": e.description,
39
+ })
40
+ response.content_type = "application/json"
41
+ return response
42
+
43
+ @app1.errorhandler(UnprocessableEntity)
44
+ def validation_error_handler(exc):
45
+ response = exc.get_response()
46
+ exc_code_desc = exc.description.split("-")
47
+ exc_code = int(exc_code_desc[0])
48
+ exc_desc = exc_code_desc[1]
49
+ response.data = json.dumps({
50
+ "code": exc_code,
51
+ "details": exc_desc,
52
+ })
53
+ response.content_type = "application/json"
54
+ return response
55
+
56
+ @app1.errorhandler(InternalServerError)
57
+ def internal_server_error_handler(exc):
58
+ response = exc.get_response()
59
+ response.data = json.dumps({
60
+ "code": 500,
61
+ "details": "Some Error Occurred, Please try later",
62
+ })
63
+ response.content_type = "application/json"
64
+ return response
65
+
66
+ # Use Waitress for production server
67
+ if __name__ == "__main__":
68
+ serve(app1, host="0.0.0.0", port=int(os.getenv("PORT", 7860))) # Ensure correct port is used
69
 
70
  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.
71
  '''
 
77
  from waitress import serve
78
  import os
79
  from dotenv import load_dotenv
80
+ from router.router import app # Importing the original blueprint
81
  load_dotenv()
82
 
83
  # Flask app setup
 
89
  swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL, config={'app_name': "Infosys Responsible AI - Moderation"})
90
  app1.register_blueprint(swaggerui_blueprint)
91
 
92
+ # Register the app blueprint for the '/rai/v1/moderations' route
93
+ app1.register_blueprint(app) # Registering the blueprint that contains the route
94
+
95
  # CORS and error handling setup
96
  CORS(app1)
97
 
 
130
 
131
  # Use Waitress for production server
132
  if __name__ == "__main__":
133
+ serve(app1, host="0.0.0.0", port=int(os.getenv("PORT", 7860))) # Ensure correct port is used
134
+
135
 
136
 
137