Kiran5 commited on
Commit
498708d
·
1 Parent(s): c8cceb1

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +9 -9
  2. main.py +13 -25
  3. requirements.txt +2 -0
Dockerfile CHANGED
@@ -1,27 +1,27 @@
1
- FROM python:3.9
2
 
 
3
  RUN useradd -m -u 1000 user
4
  USER user
5
  ENV PATH="/home/user/.local/bin:$PATH"
6
 
7
  WORKDIR /app
8
 
 
9
  COPY --chown=user ./requirements.txt requirements.txt
10
-
11
  COPY --chown=user ./lib/aicloudlibs-0.1.0-py3-none-any.whl /lib/
12
-
13
  COPY --chown=user ./lib/better_profanity-2.0.0-py3-none-any.whl /lib/
14
-
15
  COPY --chown=user ./lib/privacy-1.0.9-py3-none-any.whl /lib/
16
 
 
17
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
18
 
19
- COPY --chown=user . /app
20
-
21
- # Expose the port (default for Hugging Face is 7860)
22
  EXPOSE 7860
23
 
24
- # CMD to run the FastAPI app with Uvicorn
25
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
26
 
 
 
27
 
 
1
+ FROM python:3.9-slim
2
 
3
+ # Create and use a non-root user for security
4
  RUN useradd -m -u 1000 user
5
  USER user
6
  ENV PATH="/home/user/.local/bin:$PATH"
7
 
8
  WORKDIR /app
9
 
10
+ # Copy necessary files
11
  COPY --chown=user ./requirements.txt requirements.txt
 
12
  COPY --chown=user ./lib/aicloudlibs-0.1.0-py3-none-any.whl /lib/
 
13
  COPY --chown=user ./lib/better_profanity-2.0.0-py3-none-any.whl /lib/
 
14
  COPY --chown=user ./lib/privacy-1.0.9-py3-none-any.whl /lib/
15
 
16
+ # Install dependencies
17
  RUN pip install --no-cache-dir --upgrade -r requirements.txt
18
 
19
+ # Expose the port used by Hugging Face Spaces (7860)
 
 
20
  EXPOSE 7860
21
 
22
+ # Copy the rest of the app
23
+ COPY --chown=user . /app
24
 
25
+ # Command to run the app (avoid Waitress here)
26
+ CMD ["python", "main.py"]
27
 
main.py CHANGED
@@ -8,46 +8,38 @@ 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 # 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,
32
- config={ # Swagger UI config overrides
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,
@@ -58,7 +50,6 @@ def handle_exception(e):
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])
@@ -72,7 +63,6 @@ def validation_error_handler(exc):
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,
@@ -81,12 +71,10 @@ def internal_server_error_handler(exc):
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
 
 
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, UnprocessableEntity, InternalServerError
12
  from flask import Flask
13
+ from router.router import app # Make sure this is a Flask Blueprint
 
 
14
  from flask_swagger_ui import get_swaggerui_blueprint
15
  from flask_cors import CORS
 
16
  from dotenv import load_dotenv
17
+ import os
18
 
19
  load_dotenv()
20
 
21
  request_id_var.set("StartUp")
22
 
23
+ SWAGGER_URL = '/rai/v1/moderations/docs'
24
+ API_URL = '/static/metadata.json'
25
 
 
26
  swaggerui_blueprint = get_swaggerui_blueprint(
27
+ SWAGGER_URL, API_URL,
28
+ config={'app_name': "Infosys Responsible AI - Moderation"}
 
 
 
29
  )
30
 
31
  # Create Flask app instance
32
  app1 = Flask(__name__)
33
 
34
+ # Register blueprints
35
+ app1.register_blueprint(app)
36
  app1.register_blueprint(swaggerui_blueprint)
37
 
 
38
  CORS(app1)
39
 
40
+ # Error handlers
41
  @app1.errorhandler(HTTPException)
42
  def handle_exception(e):
 
43
  response = e.get_response()
44
  response.data = json.dumps({
45
  "code": e.code,
 
50
 
51
  @app1.errorhandler(UnprocessableEntity)
52
  def validation_error_handler(exc):
 
53
  response = exc.get_response()
54
  exc_code_desc = exc.description.split("-")
55
  exc_code = int(exc_code_desc[0])
 
63
 
64
  @app1.errorhandler(InternalServerError)
65
  def internal_server_error_handler(exc):
 
66
  response = exc.get_response()
67
  response.data = json.dumps({
68
  "code": 500,
 
71
  response.content_type = "application/json"
72
  return response
73
 
74
+ # Start Flask server
75
  if __name__ == "__main__":
76
  request_id_var.set("StartUp")
77
+ app1.run(host='0.0.0.0', port=int(os.getenv("PORT", 7860)), debug=True)
78
+
 
 
79
 
80
 
requirements.txt CHANGED
@@ -15,6 +15,8 @@ setuptools
15
  flask
16
  Gunicorn
17
  flask[async]
 
 
18
  flask-cors
19
  azure-ai-translation-text==1.0.0b1
20
  waitress
 
15
  flask
16
  Gunicorn
17
  flask[async]
18
+ werkzeug==2.2.3
19
+ python-dotenv==1.0.0
20
  flask-cors
21
  azure-ai-translation-text==1.0.0b1
22
  waitress