|
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 |
|
|
|
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( |
|
"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", |
|
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) |
|
|
|
|
|
|
|
test_image_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png" |
|
|
|
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) |
|
|
|
|