Skip to content

Commit

Permalink
chore: Update Dockerfile for Python API and add CORS middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
amirnd51 committed Jul 24, 2024
1 parent a7e1e17 commit 7509b1b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions docker/Dockerfile.python_api
Original file line number Diff line number Diff line change
@@ -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"]

20 changes: 20 additions & 0 deletions python_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
23 changes: 16 additions & 7 deletions python_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}")
Expand Down Expand Up @@ -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
Expand All @@ -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]}
Expand All @@ -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)
Expand Down Expand Up @@ -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'],
Expand Down
5 changes: 1 addition & 4 deletions python_api/mq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
File renamed without changes.

0 comments on commit 7509b1b

Please sign in to comment.