sohojoe's picture
test the api
cb92ee4
raw
history blame
2.7 kB
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
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(
"https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png", # 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(100):
test_text()
end = time.time()
# print average time in seconds and in milliseconds and number of predictions per second
print("Average time for text: ", (end - start) / 100, "s")
print("Average time for text: ", (end - start) * 10, "ms")
print("Number of predictions per second for text: ", 1 / ((end - start) / 100))
# performance test for image
start = time.time()
for i in range(100):
test_image()
end = time.time()
# print average time in seconds and in milliseconds
print("Average time for image: ", (end - start) / 100, "s")
print("Average time for image: ", (end - start) * 10, "ms")
print("Number of predictions per second for image: ", 1 / ((end - start) / 100))
test_image_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
# 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(100):
test_image_as_payload(payload)
end = time.time()
# print average time in seconds and in milliseconds
print("Average time for image as payload: ", (end - start) / 100, "s")
print("Average time for image as payload: ", (end - start) * 10, "ms")
print("Number of predictions per second for image as payload: ", 1 / ((end - start) / 100))