-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathapi.py
50 lines (43 loc) · 1.42 KB
/
api.py
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
import ast
import pdb
from PIL import Image, ImageDraw
from gradio_client import Client, handle_file
SHOWUI_HUGGINGFACE_SOURCE = "showlab/ShowUI"
SHOWUI_HUGGINGFACE_MODEL = "showlab/ShowUI-2B"
SHOWUI_HUGGINGFACE_API = "/on_submit"
class ShowUIProvider:
"""
The ShowUI provider is used to make calls to ShowUI.
"""
def __init__(self):
self.client = Client(SHOWUI_HUGGINGFACE_SOURCE)
def extract_norm_point(self, response, image_url):
if isinstance(image_url, str):
image = Image.open(image_url)
else:
image = Image.fromarray(np.uint8(image_url))
point = ast.literal_eval(response)
if len(point) == 2:
x, y = point[0] * image.width, point[1] * image.height
return x, y
else:
return None
def call(self, prompt, image_data):
result = self.client.predict(
image=handle_file(image_data),
query=prompt,
iterations=1,
is_example_image="False",
api_name=SHOWUI_HUGGINGFACE_API,
)
pred = result[1]
img_url = result[0][0]['image']
result = self.extract_norm_point(pred, img_url)
return result
if __name__ == "__main__":
showuiprovider = ShowUIProvider()
img_url = "examples/chrome.png"
query = "search box"
pdb.set_trace()
result = showuiprovider.call(query, img_url)
print(result)