make clip_app_client a bit more generic
Browse files- experimental/clip_app_client.py +25 -12
experimental/clip_app_client.py
CHANGED
@@ -43,29 +43,30 @@ def preprocess_image(image_url):
|
|
43 |
|
44 |
preprocessed_image = preprocess_image(test_image_url)
|
45 |
|
46 |
-
|
|
|
47 |
payload = {
|
48 |
-
"text": ('str',
|
49 |
}
|
50 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
51 |
response = requests.post(url, files=payload)
|
52 |
embeddings = response.text
|
53 |
-
return
|
54 |
|
55 |
-
def
|
56 |
payload = {
|
57 |
"image_url": ('str', test_image_url, 'application/octet-stream'),
|
58 |
}
|
59 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
60 |
response = requests.post(url, files=payload)
|
61 |
embeddings = response.text
|
62 |
-
return
|
63 |
|
64 |
-
def
|
65 |
key = "preprocessed_image"
|
66 |
-
data_bytes =
|
67 |
-
shape_bytes = np.array(
|
68 |
-
dtype_bytes = str(
|
69 |
payload = {
|
70 |
key: ('tensor', data_bytes, 'application/octet-stream'),
|
71 |
'shape': ('shape', shape_bytes, 'application/octet-stream'),
|
@@ -74,6 +75,18 @@ def send_preprocessed_image_request(number):
|
|
74 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
75 |
response = requests.post(url, files=payload)
|
76 |
embeddings = response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
return number, embeddings
|
78 |
|
79 |
def process(numbers, send_func, max_workers=10):
|
@@ -97,7 +110,7 @@ if __name__ == "__main__":
|
|
97 |
# n_calls = 1
|
98 |
numbers = list(range(n_calls))
|
99 |
start_time = time.monotonic()
|
100 |
-
process(numbers,
|
101 |
end_time = time.monotonic()
|
102 |
total_time = end_time - start_time
|
103 |
avg_time_ms = total_time / n_calls * 1000
|
@@ -110,7 +123,7 @@ if __name__ == "__main__":
|
|
110 |
# n_calls = 1
|
111 |
numbers = list(range(n_calls))
|
112 |
start_time = time.monotonic()
|
113 |
-
process(numbers,
|
114 |
end_time = time.monotonic()
|
115 |
total_time = end_time - start_time
|
116 |
avg_time_ms = total_time / n_calls * 1000
|
@@ -123,7 +136,7 @@ if __name__ == "__main__":
|
|
123 |
# n_calls = 1
|
124 |
numbers = list(range(n_calls))
|
125 |
start_time = time.monotonic()
|
126 |
-
process(numbers,
|
127 |
end_time = time.monotonic()
|
128 |
total_time = end_time - start_time
|
129 |
avg_time_ms = total_time / n_calls * 1000
|
|
|
43 |
|
44 |
preprocessed_image = preprocess_image(test_image_url)
|
45 |
|
46 |
+
|
47 |
+
def text_to_embedding(text):
|
48 |
payload = {
|
49 |
+
"text": ('str', text, 'application/octet-stream'),
|
50 |
}
|
51 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
52 |
response = requests.post(url, files=payload)
|
53 |
embeddings = response.text
|
54 |
+
return embeddings
|
55 |
|
56 |
+
def image_url_to_embedding(image_url):
|
57 |
payload = {
|
58 |
"image_url": ('str', test_image_url, 'application/octet-stream'),
|
59 |
}
|
60 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
61 |
response = requests.post(url, files=payload)
|
62 |
embeddings = response.text
|
63 |
+
return embeddings
|
64 |
|
65 |
+
def preprocessed_image_to_embedding(image):
|
66 |
key = "preprocessed_image"
|
67 |
+
data_bytes = image.numpy().tobytes()
|
68 |
+
shape_bytes = np.array(image.shape).tobytes()
|
69 |
+
dtype_bytes = str(image.dtype).encode()
|
70 |
payload = {
|
71 |
key: ('tensor', data_bytes, 'application/octet-stream'),
|
72 |
'shape': ('shape', shape_bytes, 'application/octet-stream'),
|
|
|
75 |
url = os.environ.get("HTTP_ADDRESS", "http://127.0.0.1:8000/")
|
76 |
response = requests.post(url, files=payload)
|
77 |
embeddings = response.text
|
78 |
+
return embeddings
|
79 |
+
|
80 |
+
def _send_text_request(number):
|
81 |
+
embeddings = text_to_embedding(english_text)
|
82 |
+
return number, embeddings
|
83 |
+
|
84 |
+
def _send_image_url_request(number):
|
85 |
+
embeddings = image_url_to_embedding(test_image_url)
|
86 |
+
return number, embeddings
|
87 |
+
|
88 |
+
def _send_preprocessed_image_request(number):
|
89 |
+
embeddings = preprocessed_image_to_embedding(preprocessed_image)
|
90 |
return number, embeddings
|
91 |
|
92 |
def process(numbers, send_func, max_workers=10):
|
|
|
110 |
# n_calls = 1
|
111 |
numbers = list(range(n_calls))
|
112 |
start_time = time.monotonic()
|
113 |
+
process(numbers, _send_text_request)
|
114 |
end_time = time.monotonic()
|
115 |
total_time = end_time - start_time
|
116 |
avg_time_ms = total_time / n_calls * 1000
|
|
|
123 |
# n_calls = 1
|
124 |
numbers = list(range(n_calls))
|
125 |
start_time = time.monotonic()
|
126 |
+
process(numbers, _send_image_url_request)
|
127 |
end_time = time.monotonic()
|
128 |
total_time = end_time - start_time
|
129 |
avg_time_ms = total_time / n_calls * 1000
|
|
|
136 |
# n_calls = 1
|
137 |
numbers = list(range(n_calls))
|
138 |
start_time = time.monotonic()
|
139 |
+
process(numbers, _send_preprocessed_image_request)
|
140 |
end_time = time.monotonic()
|
141 |
total_time = end_time - start_time
|
142 |
avg_time_ms = total_time / n_calls * 1000
|