sohojoe's picture
use a better sample image
91e4bde
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!", # str representing string value in 'Input' Textbox component
api_name="/text_to_embeddings"
)
return(result)
def test_image():
result = client.predict(
test_image_url, # str representing filepath or URL to image in 'Image Prompt' Image component
api_name="/image_to_embeddings"
)
return(result)
def test_image_as_payload(payload):
result = client.predict(
payload, # image as string payload
api_name="/image_as_payload_to_embeddings"
)
return(result)
# performance test for text
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)
# performance test for image
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)
# download image from url
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')
# convert image to numpy array
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)
# performance test for image as payload
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)