From 7509b1bc1e52fd8fc8a0d3e6762af16410d34531 Mon Sep 17 00:00:00 2001 From: amirnd51 Date: Wed, 24 Jul 2024 01:00:14 +0000 Subject: [PATCH] chore: Update Dockerfile for Python API and add CORS middleware --- .github/workflows/build-and-push.yaml | 2 +- docker/Dockerfile.python_api | 21 +++++++++++++++++++++ python_api/Dockerfile | 20 ++++++++++++++++++++ python_api/api.py | 23 ++++++++++++++++------- python_api/mq.py | 5 +---- python_api/{req.txt => requirements.txt} | 0 6 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 docker/Dockerfile.python_api create mode 100644 python_api/Dockerfile rename python_api/{req.txt => requirements.txt} (100%) diff --git a/.github/workflows/build-and-push.yaml b/.github/workflows/build-and-push.yaml index 31f9a64..5389712 100644 --- a/.github/workflows/build-and-push.yaml +++ b/.github/workflows/build-and-push.yaml @@ -21,7 +21,7 @@ jobs: uses: actions/checkout@v3 - name: Build Image - run: ./scripts/build-container.sh api ${IMAGE_TAGS} ${IMAGE_REGISTRY} + run: ./scripts/build-container.sh python_api ${IMAGE_TAGS} ${IMAGE_REGISTRY} - name: Push to GHCR id: push-to-ghcr diff --git a/docker/Dockerfile.python_api b/docker/Dockerfile.python_api new file mode 100644 index 0000000..e8a4e33 --- /dev/null +++ b/docker/Dockerfile.python_api @@ -0,0 +1,21 @@ +# Use the official Python image from the Docker Hub +FROM python:3.11-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements.txt file into the container +COPY requirements.txt . + +# Install the dependencies specified in the requirements.txt file +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code into the container +COPY . . + +# Expose the port that the FastAPI app runs on +EXPOSE 8000 + +# Command to run the FastAPI application with --reload for development +CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"] + diff --git a/python_api/Dockerfile b/python_api/Dockerfile new file mode 100644 index 0000000..0175942 --- /dev/null +++ b/python_api/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Python image from the Docker Hub +FROM python:3.11-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements.txt file into the container +COPY python_api/requirements.txt . + +# Install the dependencies specified in the requirements.txt file +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code into the container +COPY python_api/ . + +# Expose the port that the FastAPI app runs on +EXPOSE 8000 + +# Command to run the FastAPI application with --reload for development +CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/python_api/api.py b/python_api/api.py index 4d90df5..0067e3b 100644 --- a/python_api/api.py +++ b/python_api/api.py @@ -19,6 +19,7 @@ from typing import Optional from schema_mlmodelscope import * from datetime import datetime +from fastapi.middleware.cors import CORSMiddleware app = FastAPI() logging.basicConfig(level=logging.INFO) @@ -28,6 +29,14 @@ async def add_cors_header(request, call_next): response = await call_next(request) response.headers['Access-Control-Allow-Origin'] = '*' return response + + # enable cors + app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_methods=["*"], + allow_headers=["*"], + ) @app.exception_handler(Exception) async def generic_exception_handler(request: Request, exc: Exception): logging.error(f"An error occurred: {exc}") @@ -305,7 +314,7 @@ async def predict(request: PredictRequest): # data = request.get_json() # print(data) - print(request) + # print(request) architecture = request.architecture batch_size = request.batchSize @@ -322,16 +331,16 @@ async def predict(request: PredictRequest): if inputs and len(inputs)>1: has_multi_input=True config = request.config - print(inputs[0]) + # print(inputs[0]) first_input=inputs[0] # xxx trail= get_trial_by_model_and_input( model_id, first_input["src"]) - print(trail) - print("trial") + # print(trail) + # print("trial") if trail and trail[2] is not None: - print(trail[2]) + # print(trail[2]) experiment_id = trail[0] trial_id = trail[1] return {"experimentId": experiment_id, "trialId": trial_id, "model_id": model_id, "input_url": inputs[0]} @@ -344,7 +353,7 @@ async def predict(request: PredictRequest): trial_id=create_trial( model_id, experiment_id, cur, conn) create_trial_inputs(trial_id, inputs, cur, conn) - print(trial_id) + # print(trial_id) model=get_model_by_id(model_id,cur,conn) @@ -421,7 +430,7 @@ async def get_trial(trial_id: str): if not row: raise Exception(f"No trial found with ID {trial_id}") - print(row) + # print(row) # Prepare the response structure result = { "id": row['trial_id'], diff --git a/python_api/mq.py b/python_api/mq.py index 467bd3a..3b2b8f2 100644 --- a/python_api/mq.py +++ b/python_api/mq.py @@ -9,10 +9,7 @@ MQ_USER = os.environ.get('MQ_USER', 'user') MQ_PASS = os.environ.get('MQ_PASS', 'password') -print(MQ_HOST) -print(MQ_PORT) -print(MQ_USER) -print(MQ_PASS) + def connect(): parameters = pika.ConnectionParameters( diff --git a/python_api/req.txt b/python_api/requirements.txt similarity index 100% rename from python_api/req.txt rename to python_api/requirements.txt