Skip to content

Commit c647895

Browse files
authored
Merge pull request #57 from benrugg/pullrequests/tijszwinkels/stablehorde-async
Pullrequests/tijszwinkels/stablehorde async
2 parents ef59fd1 + 947090d commit c647895

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ADDON_DOWNLOAD_URL = "https://airender.gumroad.com/l/ai-render"
88
STABILITY_API_URL = "https://api.stability.ai/v1alpha/generation/"
99
DREAM_STUDIO_URL = "https://beta.dreamstudio.ai/membership?tab=apiKeys"
10-
STABLE_HORDE_API_URL = "https://stablehorde.net/api/v2/generate/sync"
10+
STABLE_HORDE_API_URL_BASE = "https://stablehorde.net/api/v2"
1111
STABLE_HORDE_URL = "https://stablehorde.net/"
1212
VIDEO_TUTORIAL_URL = "https://www.youtube.com/watch?v=tmyln5bwnO8"
1313
HELP_WITH_TIMEOUTS_URL = "https://github.com/benrugg/AI-Render/wiki/FAQ#%EF%B8%8F-ai-render-keeps-timing-out"

sd_backends/stablehorde_api.py

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
utils,
1010
)
1111

12+
API_REQUEST_URL = config.STABLE_HORDE_API_URL_BASE + "/generate/async"
13+
API_CHECK_URL = config.STABLE_HORDE_API_URL_BASE + "/generate/check"
14+
API_GET_URL = config.STABLE_HORDE_API_URL_BASE + "/generate/status"
1215

1316
# CORE FUNCTIONS:
1417

@@ -36,24 +39,54 @@ def send_to_api(params, img_file, filename_prefix, sd_model):
3639

3740
# send the API request
3841
start_time = time.monotonic()
39-
print("Sending to Stable Horde")
4042
try:
41-
response = requests.post(config.STABLE_HORDE_API_URL, json=stablehorde_params, headers=headers, timeout=request_timeout())
43+
print(f"Sending request to Stable Horde API: {API_REQUEST_URL}")
44+
response = requests.post(API_REQUEST_URL, json=stablehorde_params, headers=headers, timeout=20)
45+
print(response.json())
46+
id = response.json()["id"]
4247
img_file.close()
4348
except requests.exceptions.ReadTimeout:
4449
img_file.close()
45-
return operators.handle_error(f"The server timed out. Try again in a moment, or get help. [Get help with timeouts]({config.HELP_WITH_TIMEOUTS_URL})", "timeout")
46-
print("The horde took " + str(time.monotonic() - start_time) + " seconds to imagine this frame.")
50+
return operators.handle_error(f"There was an error sending this request to Stable Horde. Please try again in a moment.", "timeout")
51+
except Exception as e:
52+
img_file.close()
53+
return operators.handle_error(f"Error with Stable Horde. Full error message: {e}", "unknown_error")
54+
55+
# Check the status of the request (For at most request_timeout seconds)
56+
for i in range(request_timeout()):
57+
try:
58+
time.sleep(1)
59+
URL=API_CHECK_URL + "/" + id
60+
print(f"Checking status of request at Stable Horde API: {URL}")
61+
response = requests.get(URL, headers=headers, timeout=20)
62+
print(f"Waiting for {str(time.monotonic() - start_time)}s. Response: {response.json()}")
63+
if response.json()["done"] == True:
64+
print("The horde took " + str(time.monotonic() - start_time) + "s to imagine this frame.")
65+
break
66+
except requests.exceptions.ReadTimeout:
67+
# Ignore timeouts
68+
print("WARN: Timeout while checking status")
69+
except Exception as e: # Catch all other errors
70+
return operators.handle_error(f"Error while checking status: {e}", "unknown_error")
71+
if (i == request_timeout() - 1):
72+
return operators.handle_error(f"Timeout generating image. Try again in a moment, or get help. [Get help with timeouts]({config.HELP_WITH_TIMEOUTS_URL})", "timeout")
73+
74+
# Get the image
75+
try:
76+
URL=API_GET_URL + "/" + id
77+
print(f"Retrieving image from Stable Horde API: {URL}")
78+
response = requests.get(URL, headers=headers, timeout=20)
79+
# handle the response
80+
if response.status_code == 200:
81+
return handle_api_success(response, filename_prefix)
82+
else:
83+
return handle_api_error(response)
4784

48-
# For debugging
49-
# print("Send to Stable Horde: " + str(stablehorde_params))
50-
# print("Received from Stable Horde: " + str(response.json()))
85+
except requests.exceptions.ReadTimeout:
86+
return operators.handle_error(f"Timeout getting image from Stable Horde. Try again in a moment, or get help. [Get help with timeouts]({config.HELP_WITH_TIMEOUTS_URL})", "timeout")
87+
except Exception as e:
88+
return operators.handle_error(f"Error with Stable Horde. Full error message: {e}", "unknown_error")
5189

52-
# handle the response
53-
if response.status_code == 200:
54-
return handle_api_success(response, filename_prefix)
55-
else:
56-
return handle_api_error(response)
5790

5891

5992
def handle_api_success(response, filename_prefix):
@@ -63,7 +96,6 @@ def handle_api_success(response, filename_prefix):
6396
response_obj = response.json()
6497
base64_img = response_obj["generations"][0]["img"]
6598
print(f"Worker: {response_obj['generations'][0]['worker_name']}, " +
66-
f"queue position: {response_obj['queue_position']}, wait time: {response_obj['wait_time']}, " +
6799
f"kudos: {response_obj['kudos']}")
68100
except:
69101
print("Stable Horde response content: ")
@@ -127,10 +159,6 @@ def get_samplers():
127159
('k_dpm_2', 'DPM2', '', 40),
128160
('k_dpm_2_a', 'DPM2 a', '', 50),
129161
('k_lms', 'LMS', '', 60),
130-
('k_dpm_fast', 'DPM fast', '', 70),
131-
('k_dpm_adaptive', 'DPM adaptive', '', 80),
132-
('k_dpmpp_2s_a', 'DPM++ 2S a', '', 110),
133-
('k_dpmpp_2m', 'DPM++ 2M', '', 120),
134162
# TODO: Stable horde does have karras support, but it's a separate boolean
135163
]
136164

@@ -140,7 +168,7 @@ def default_sampler():
140168

141169

142170
def request_timeout():
143-
return 600
171+
return 300
144172

145173

146174
def get_image_format():

0 commit comments

Comments
 (0)