-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_request.py
88 lines (66 loc) · 2.41 KB
/
client_request.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
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
85
86
87
88
import base64
import json
import os
import requests
from PIL import Image
# This is an example script to show how to send a request to the SDXL Turbo endpoint
def get_t2i_payload():
# Define your request payload
payload = {
"prompt": "a tree full of lemons in the Sicilian sun",
"num_inference_steps": 1 # Turbo needs only 1 step
}
# Convert payload to JSON
json_payload = json.dumps(payload)
return json_payload
def get_i2i_payload():
# Read the image file as binary
image_path = os.path.join(os.getcwd(), "cat.png")
with open(image_path, "rb") as image_file:
original_image = Image.open(image_file)
resized_image = original_image.resize((512, 512))
# Convert the binary data to a base64-encoded string
base64_encoded_image = base64.b64encode(resized_image.tobytes()).decode("utf-8")
# Define your request payload
payload = {
"image": base64_encoded_image,
"prompt": "cat wizard, adorable",
"strength": 0.7,
"guidance_scale": 0.0,
"num_inference_steps": 3,
}
# Convert payload to JSON
json_payload = json.dumps(payload)
return json_payload
def test():
# Define the URL (replace with your actual URL)
url = "http://0.0.0.0:8080/sdxl-turbo-t2i"
# test text2image
json_payload = get_t2i_payload()
# Send the POST request
response = requests.post(url, data=json_payload)
# Check the response
if response.status_code == 200:
# Process the response (e.g., save the image, print success message)
with open("output_image_t2i.png", "wb") as f:
f.write(response.content)
print(f"Image saved successfully as output_image_t2i.png.")
pass
else:
print("Error:", response.status_code, response.text)
i2i_url = "http://0.0.0.0:8080/sdxl-turbo-i2i"
# test image2image
json_payload = get_i2i_payload()
# Send the POST request
response = requests.post(i2i_url, data=json_payload)
# Check the response
if response.status_code == 200:
# Process the response (e.g., save the image, print success message)
with open("output_image_i2i.png", "wb") as f:
f.write(response.content)
print(f"Image saved successfully as output_image_i2i.png.")
pass
else:
print("Error:", response.status_code, response.text)
if __name__ == "__main__":
test()