File size: 2,739 Bytes
cb92ee4 b255c4c 91e4bde cb92ee4 91e4bde cb92ee4 b255c4c cb92ee4 b255c4c cb92ee4 b255c4c cb92ee4 b255c4c cb92ee4 b255c4c cb92ee4 b255c4c cb92ee4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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)
|