Repository navigation
Need help to replace Upscale function with an HiresFix upscale instead. PLEASE <3 #2
Description
Hello there! I've been thoroughly enjoying the base-template of your Bot which I've sort of completely re-shaped to my will. But there's just one thing that I'm really struggling to put into place and I was wondering if I could have your help..
The upscale function, I want it to do an hires-fix instead that I tune to my will:
"hr_scale": 1.35,
"hr_upscaler": "4x_NMKD-Superscale-SP_178000_G",
"denoising_strength": 0.35,```
Issue is, I do not know how to make it work with the current base of the bot.
async def imagegen(prompt, orientation, negative_prompt, variation=False):
print(orientation) # debug print statement
global total_requests
total_requests = total_requests + 1
global webui_url
global variation_strength
currentTime = datetime.now()
width, height = make_orientation(orientation)
with open('gen_settings.json') as json_file:
data = json.load(json_file)
default = data["default"]
enable_hr_value = data["enable_hr"]
hr_scale_value = data["hr_scale"]
hr_upscaler_value = data["hr_upscaler"]
hr_denoise_value = data["denoising_strength"]
add_prompt_txt = data["add_prompt"]
if variation:
variation_strength = variation_strength
else:
variation_strength = 0
config = load_config("gen_settings.json")
payload = {
"prompt": prompt + add_prompt_txt,
"negative_prompt" : negative_prompt,
"width": width,
"height": height,
"enable_hr": enable_hr_value,
"hr_scale": hr_scale_value,
"hr_upscaler": hr_upscaler_value,
"denoising_strength": hr_denoise_value,
"subseed_strength": variation_strength,
**config # Merge the loaded config with the payload
}
async with session.post(url=f'{webui_url}/sdapi/v1/txt2img', json=payload) as response:
r = await response.json()
for i in r['images']:
image = Image.open(io.BytesIO(base64.b64decode(i.split(",",1)[0])))
png_payload = {
"image": "data:image/png;base64," + i
}
async with session.post(url=f'{webui_url}/sdapi/v1/png-info', json=png_payload) as response2:
json_response = await response2.json()
pnginfo = PngImagePlugin.PngInfo()
pnginfo.add_text("parameters", json_response.get("info"))
global characters
image_id = ''.join(random.choice(characters) for i in range(24))
file_path = f"GeneratedImages/{image_id}.png"
image.save(file_path, pnginfo=pnginfo)
print ("Generated Image:", file_path)
print (total_requests)
with open('current_requests.txt', 'w') as file:
file.write(str(total_requests))
return file_path, image_id
Sends the upscale request to A1111
async def upscale(image):
global total_requests
total_requests = total_requests + 1
with open(image, 'rb') as image_file:
image_b64 = base64.b64encode(image_file.read()).decode()
upscale_payload = {
"upscaling_resize": 2,
"upscaling_crop": True,
"gfpgan_visibility": 0,
"codeformer_visibility": 0,
"codeformer_weight": 0,
"upscaler_1": "4x_NMKD-Siax_200k",
"image": image_b64
}
async with session.post(url=f'{webui_url}/sdapi/v1/extra-single-image', json=upscale_payload) as response_upscaled:
r_u = await response_upscaled.json()
image_bytes = base64.b64decode(r_u['image'])
image_upscaled = Image.open(io.BytesIO(image_bytes))
file_path = image
file_path = file_path.replace('.png', '')
file_path = f"{file_path}-upscaled.png"
image_upscaled.save(file_path)
print ("Upscaled Image:", file_path)
print (total_requests)
with open('current_requests.txt', 'w') as file:
file.write(str(total_requests))
return file_path
```
I'd just appreciate it to have the very bare minimum of help with this. Just the foundation, would help greatly. THanks in advance.
Also great work !
Activity
CyberTimon commentedon Jul 13, 2023
Hey there!
Thank you for using my discord bot code.
To change the upscale function with the hiresfix, it get's a bit complicated.
Here is a short overview what you should do:
As the upscale request goes to a different endpoint from A1111, you can really just delete the upscale function. Check how I pass the prompt, seed etc trough the functions for the variations button. Make the same for the upscale button, but instead of setting a variation strenght, select high-res fix to true and the corresponding variables. It kind of needs to generate the same image with the same seed, prompt etc but also use highres fix. Do you understand?
I'm here to help if you face errors or have different problems.
Feel free to ask me for help.
Kind regards,
Timon Käch
PurpleBlueAloeVera commentedon Jul 14, 2023
Thanks I'm gonna try that and let you know !
PurpleBlueAloeVera commentedon Jul 14, 2023
I managed it !! Thanks to your help. was a great shortcut ! <3
PurpleBlueAloeVera commentedon Jul 14, 2023
@CyberTimon One last thing. It seems like the script does batch count 2, instead of batch size 2. Which slows down the generation process. How would you edit that ?
CyberTimon commentedon Jul 16, 2023
Hello!
Sorry for the late reply - I'm on vacation right now.
Instead of triggering the image generation function twice, you have to trigger it once and give both images as a return value back.
You can set batch size 2 in the request and then you will get 2 images back. You can see how it processes the images at line 186.
Hope this helps!