Skip to content

Commit

Permalink
Toy example request and local server response working
Browse files Browse the repository at this point in the history
  • Loading branch information
JanRiedelsheimer committed Nov 12, 2024
1 parent 7d83e09 commit 43d8543
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
2 changes: 1 addition & 1 deletion http_submission/model_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def predict():


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


if __name__ == "__main__":
Expand Down
40 changes: 37 additions & 3 deletions http_submission/sample_evaluation.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import numpy as np
import pickle
import pysaliency
import requests
import sys
from sample_submission import SampleScanpathModel
sys.path.insert(0, '..')
import pysaliency


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

Expand All @@ -24,6 +26,38 @@ def conditional_log_density(
# print(f"Received: {response.json()}")
return np.array(response.json())

# 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):
# # build request
# pil_image = Image.fromarray(stimulus)
# image_bytes = BytesIO()
# pil_image.save(image_bytes, format='png')

# def _convert_attribute(attribute):
# if isinstance(attribute, np.ndarray):
# return attribute.tolist()
# return attribute

# json_data = {
# "x_hist": list(x_hist),
# "y_hist": list(y_hist),
# "t_hist": list(t_hist),
# "attributes": {key: _convert_attribute(value) for key, value in (attributes or {}).items()}
# }

# # send request

# response = requests.post(f"{self.url}/conditional_log_density", json=json_data, files={'stimulus': image_bytes.getvalue()})

# # parse response

# if response.status_code != 200:
# raise ValueError(f"Server returned status code {response.status_code}")

# return np.array(response.json()['log_density'])

if __name__ == "__main__":
http_model = HTTPScanpathModel("http://localhost:4000/predict")
Expand Down
43 changes: 42 additions & 1 deletion http_submission/sample_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,45 @@ 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)
return np.log(stimulus)

# from io import BytesIO

# import pysaliency
# import requests
# from PIL import Image
# import numpy as np


# 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):
# # build request
# pil_image = Image.fromarray(stimulus)
# image_bytes = BytesIO()
# pil_image.save(image_bytes, format='png')

# def _convert_attribute(attribute):
# if isinstance(attribute, np.ndarray):
# return attribute.tolist()
# return attribute

# json_data = {
# "x_hist": list(x_hist),
# "y_hist": list(y_hist),
# "t_hist": list(t_hist),
# "attributes": {key: _convert_attribute(value) for key, value in (attributes or {}).items()}
# }

# # send request

# response = requests.post(f"{self.url}/conditional_log_density", json=json_data, files={'stimulus': image_bytes.getvalue()})

# # parse response

# if response.status_code != 200:
# raise ValueError(f"Server returned status code {response.status_code}")

# return np.array(response.json()['log_density'])

0 comments on commit 43d8543

Please sign in to comment.