Haoming02 commited on
Commit
0552e65
·
verified ·
1 Parent(s): 6bd9d57
Files changed (1) hide show
  1. api.py +40 -0
api.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =========================== #
2
+ # Example API Usage in Python #
3
+ # =========================== #
4
+
5
+ # ======================================== #
6
+ # Official Doc for Unity #
7
+ # https://github.com/huggingface/unity-api #
8
+ # ======================================== #
9
+
10
+ from gradio_client import Client
11
+ from PIL import Image
12
+ import base64
13
+ import io
14
+
15
+
16
+ def main(path: str, width: int = 512, height: int = 512):
17
+ client = Client("Haoming02/hello-world")
18
+
19
+ img = Image.open(path)
20
+
21
+ with io.BytesIO() as buffer:
22
+ img.save(buffer, format="PNG")
23
+ base64_image = base64.b64encode(buffer.getvalue()).decode()
24
+
25
+ log, image = client.predict(
26
+ image=base64_image,
27
+ width=int(width),
28
+ height=int(height),
29
+ api_name="/resizeImage",
30
+ )
31
+
32
+ print(log)
33
+
34
+ image = Image.open(io.BytesIO(base64.b64decode(image)))
35
+ image.save("./output.png", optimize=True)
36
+
37
+
38
+ if __name__ == "__main__":
39
+ import sys
40
+ main(*sys.argv[1:])