sohojoe commited on
Commit
cb92ee4
1 Parent(s): 44fc764

test the api

Browse files
Files changed (1) hide show
  1. api_test.py +82 -0
api_test.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gradio_client import Client
2
+ import time
3
+ import numpy as np
4
+
5
+ import torch
6
+
7
+ from api_helper import preprocess_image, encode_numpy_array
8
+ clip_image_size = 224
9
+
10
+ client = Client("http://127.0.0.1:7860/")
11
+
12
+ print("do we have cuda", torch.cuda.is_available())
13
+
14
+ def test_text():
15
+ result = client.predict(
16
+ "Howdy!", # str representing string value in 'Input' Textbox component
17
+ api_name="/text_to_embeddings"
18
+ )
19
+ return(result)
20
+
21
+ def test_image():
22
+ result = client.predict(
23
+ "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
24
+ api_name="/image_to_embeddings"
25
+ )
26
+ return(result)
27
+
28
+ def test_image_as_payload(payload):
29
+ result = client.predict(
30
+ payload, # image as string payload
31
+ api_name="/image_as_payload_to_embeddings"
32
+ )
33
+ return(result)
34
+
35
+ # performance test for text
36
+ start = time.time()
37
+ for i in range(100):
38
+ test_text()
39
+ end = time.time()
40
+ # print average time in seconds and in milliseconds and number of predictions per second
41
+ print("Average time for text: ", (end - start) / 100, "s")
42
+ print("Average time for text: ", (end - start) * 10, "ms")
43
+ print("Number of predictions per second for text: ", 1 / ((end - start) / 100))
44
+
45
+ # performance test for image
46
+ start = time.time()
47
+ for i in range(100):
48
+ test_image()
49
+ end = time.time()
50
+ # print average time in seconds and in milliseconds
51
+ print("Average time for image: ", (end - start) / 100, "s")
52
+ print("Average time for image: ", (end - start) * 10, "ms")
53
+ print("Number of predictions per second for image: ", 1 / ((end - start) / 100))
54
+
55
+
56
+
57
+ test_image_url = "https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png"
58
+ # download image from url
59
+ import requests
60
+ from PIL import Image
61
+ from io import BytesIO
62
+ response = requests.get(test_image_url)
63
+ input_image = Image.open(BytesIO(response.content))
64
+ input_image = input_image.convert('RGB')
65
+ # convert image to numpy array
66
+ input_image = np.array(input_image)
67
+
68
+
69
+ if input_image.shape[0] > clip_image_size or input_image.shape[1] > clip_image_size:
70
+ input_image = preprocess_image(input_image, clip_image_size)
71
+ payload = encode_numpy_array(input_image)
72
+
73
+ # performance test for image as payload
74
+ start = time.time()
75
+ for i in range(100):
76
+ test_image_as_payload(payload)
77
+ end = time.time()
78
+ # print average time in seconds and in milliseconds
79
+ print("Average time for image as payload: ", (end - start) / 100, "s")
80
+ print("Average time for image as payload: ", (end - start) * 10, "ms")
81
+ print("Number of predictions per second for image as payload: ", 1 / ((end - start) / 100))
82
+