Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ml inference in OS #8

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ services:
networks:
- opensearch-net

inference:
container_name: inference-api
build:
context: ./inference_api
dockerfile: Dockerfile
ports:
- "8000:8000"
environment:
- OMP_NUM_THREADS=1
deploy:
resources:
limits:
memory: 1G
reservations:
memory: 512M
networks:
- opensearch-net

networks:
opensearch-net:
driver: bridge
15 changes: 15 additions & 0 deletions inference_api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.12.6

COPY ./requirements.txt /code/requirements.txt
COPY ./model/regression_model.pkl /code/model/regression_model.pkl
#
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app /code/app

# Run the API
WORKDIR /code/app

#RUN fastapi run opensearch_inference_service.py
CMD ["fastapi", "run", "opensearch_inference_service.py"]
#CMD ["gunicorn", "main:app", "-b", "0.0.0.0:8000", "--timeout", "600", "-w", "1", "-k", "uvicorn.workers.UvicornWorker"]
EXPOSE 8000
46 changes: 46 additions & 0 deletions inference_api/app/opensearch_inference_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
import pandas as pd

# Create a FastAPI application instance
app = FastAPI()

# Load the pre-trained machine learning model
with open('/code/model/regression_model.pkl', 'rb') as fid:
model = pickle.load(fid)

# Define a Pydantic model for input validation
class InputData(BaseModel):
data: str

# Define a POST endpoint for making predictions
@app.post("/predict/")
def predict(input_data: InputData):
"""
Accept a JSON object with a key "data" containing a comma-separated string of floats.
Convert the string to a list of floats and use the model to make predictions.
"""
try:
# Split the input string by commas and convert to a list of floats
data_list = [float(value.strip()) for value in input_data.data.split(",")]

# Define the column names for the input features
columns = model.feature_names_in_

# Create a pandas DataFrame from the input data
features = pd.DataFrame([data_list], columns=columns)

# Use the model to make a prediction
prediction = round(model.predict(features)[0], 2)
keywordness = 1 - prediction

# Return the prediction as a JSON object
return {
"neuralness": prediction,
"keywordness": keywordness
}

except Exception as e:
# Handle errors and provide feedback
return {"error": str(e)}
Binary file added inference_api/model/regression_model.pkl
Binary file not shown.
3 changes: 3 additions & 0 deletions inference_api/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fastapi[standard]==0.115.5
pandas==2.2.3
scikit-learn==1.5.2
Loading