Skip to content

Commit

Permalink
http_server first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JanRiedelsheimer committed Nov 11, 2024
1 parent b7e836e commit 7d83e09
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
23 changes: 23 additions & 0 deletions http_submission/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Specify a base image depending on the project.
FROM bitnami/python:3.8
# For more complex examples, might need to use a different base image.
# FROM pytorch/pytorch:1.9.1-cuda11.1-cudnn8-runtime

WORKDIR /app

ENV HTTP_PORT=4000

RUN apt-get update \
&& apt-get -y install gcc

COPY ./requirements.txt ./
RUN python -m pip install -U pip \
&& python -m pip install -r requirements.txt

COPY . ./

# This is needed for Singularity builds.
EXPOSE $HTTP_PORT

# The entrypoint for a container,
CMD ["gunicorn", "-w", "1", "-b", "0.0.0.0:4000", "--pythonpath", ".", "model_server:app"]
27 changes: 27 additions & 0 deletions http_submission/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Submission
TODO: Add a description of the submission process here.



## Launching the submission container
TODO: Create a docker-compose file
```bash
cd ./http_submission
docker build -t sample_pysaliency .
```

```bash
docker run --name sample_pysaliency -dp 4000:4000 sample_pysaliency
```
The above command will launch a container named `sample_pysaliency` and expose the port `4000` to the host machine. The container will be running in the background.

To test the model server, run the sample_evaluation script (Make sure to have the `pysaliency` package installed):
```bash
python ./http_evaluation/sample_evaluation.py
```


To delete the container, run the following command:
```bash
docker stop sample_pysaliency && docker rm sample_pysaliency
```
40 changes: 40 additions & 0 deletions http_submission/model_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from flask import Flask, request, jsonify
import pickle

# Import your model here
from sample_submission import SampleScanpathModel

app = Flask("saliency-model-server")
app.logger.setLevel("DEBUG")

# TODO - replace this with your model
model = SampleScanpathModel()


@app.route("/predict", methods=["POST"])
def predict():
payload = request.get_data()
inputs = pickle.loads(payload)
app.logger.info(f"Received: {inputs}")

# TODO - replace this with your model prediction function
result = model.conditional_log_density(
inputs["stimulus"],
inputs["x_hist"],
inputs["y_hist"],
inputs["t_hist"],
inputs["attributes"],
inputs["out"],
)
# resp = pickle.dumps(result)
app.logger.info(f"Result: {result}")
# The below assumes that the model returns a numpy array.
return result.tolist()


def main():
app.run(host="0.0.0.0", port="4000", debug="True", threaded=True)


if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions http_submission/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cython
flask
gunicorn
numpy

# Add additional dependencies here
38 changes: 38 additions & 0 deletions http_submission/sample_evaluation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import numpy as np
import pickle
import pysaliency
import requests


class HTTPScanpathModel(pysaliency.ScanpathModel):
def __init__(self, url):
self.url = url

def conditional_log_density(
self, stimulus, x_hist, y_hist, t_hist, attributes=None, out=None
):
inputs = {
"stimulus": stimulus,
"x_hist": x_hist,
"y_hist": y_hist,
"t_hist": t_hist,
"attributes": attributes,
"out": out,
}
payload = pickle.dumps(inputs)
response = requests.post(self.url, data=payload)
# print(f"Received: {response.json()}")
return np.array(response.json())


if __name__ == "__main__":
http_model = HTTPScanpathModel("http://localhost:4000/predict")

print(
http_model.conditional_log_density(
[1, 1.4, 10, 1],
[1, 1, 0.51, 1],
[1, 1, 2, 1],
[1, 3, 1, 1],
)
)
9 changes: 9 additions & 0 deletions http_submission/sample_submission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import numpy as np
# import pysaliency

class SampleScanpathModel():
def __init__(self):
super().__init__()

def conditional_log_density(self, stimulus, x_hist, y_hist, t_hist, attributes=None, out=None):
return np.log(stimulus)

0 comments on commit 7d83e09

Please sign in to comment.