|
from gradio_client import Client |
|
import time |
|
import numpy as np |
|
|
|
import torch |
|
|
|
from api_helper import preprocess_image, encode_numpy_array |
|
clip_image_size = 224 |
|
num_steps = 1000 |
|
test_image_url = "https://static.wixstatic.com/media/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg/v1/fill/w_454,h_333,fp_0.50_0.50,q_90/4d6b49_42b9435ce1104008b1b5f7a3c9bfcd69~mv2.jpg" |
|
|
|
|
|
client = Client("http://127.0.0.1:7860/") |
|
|
|
print("do we have cuda", torch.cuda.is_available()) |
|
|
|
def test_text(): |
|
result = client.predict( |
|
"Howdy!", |
|
api_name="/text_to_embeddings" |
|
) |
|
return(result) |
|
|
|
def test_image(): |
|
result = client.predict( |
|
test_image_url, |
|
api_name="/image_to_embeddings" |
|
) |
|
return(result) |
|
|
|
def test_image_as_payload(payload): |
|
result = client.predict( |
|
payload, |
|
api_name="/image_as_payload_to_embeddings" |
|
) |
|
return(result) |
|
|
|
|
|
start = time.time() |
|
for i in range(num_steps): |
|
test_text() |
|
end = time.time() |
|
average_time_seconds = (end - start) / num_steps |
|
print("Average time for text: ", average_time_seconds, "s") |
|
print("Average time for text: ", average_time_seconds * 1000, "ms") |
|
print("Number of predictions per second for text: ", 1 / average_time_seconds) |
|
|
|
|
|
start = time.time() |
|
for i in range(num_steps): |
|
test_image() |
|
end = time.time() |
|
average_time_seconds = (end - start) / num_steps |
|
print("Average time for image: ", average_time_seconds, "s") |
|
print("Average time for image: ", average_time_seconds * 1000, "ms") |
|
print("Number of predictions per second for image: ", 1 / average_time_seconds) |
|
|
|
|
|
|
|
|
|
import requests |
|
from PIL import Image |
|
from io import BytesIO |
|
response = requests.get(test_image_url) |
|
input_image = Image.open(BytesIO(response.content)) |
|
input_image = input_image.convert('RGB') |
|
|
|
input_image = np.array(input_image) |
|
|
|
if input_image.shape[0] > clip_image_size or input_image.shape[1] > clip_image_size: |
|
input_image = preprocess_image(input_image, clip_image_size) |
|
payload = encode_numpy_array(input_image) |
|
|
|
|
|
start = time.time() |
|
for i in range(num_steps): |
|
test_image_as_payload(payload) |
|
end = time.time() |
|
average_time_seconds = (end - start) / num_steps |
|
print("Average time for image as payload: ", average_time_seconds, "s") |
|
print("Average time for image as payload: ", average_time_seconds * 1000, "ms") |
|
print("Number of predictions per second for image as payload: ", 1 / average_time_seconds) |
|
|
|
|