dragon-bot/app/stable_diffusion.py

37 lines
1.0 KiB
Python
Executable File

from bs4 import BeautifulSoup
import base64
import httpx
import tempfile
async def generate_image(
ip, port, positives, negatives, steps, enable_upscale, model_name
):
url = "http://" + ip + ":" + port + "/sdapi/v1/txt2img"
json_data = {
"prompt": positives,
"negative_prompt": negatives,
"sampler_name": "DPM++ 2M SDE Karras",
"steps": steps,
"enable_hr": enable_upscale,
"hr_upscaler": "Latent",
"hr_second_pass_steps": 20,
"denoising_strength": 0.5,
"hr_resize_x": 1024,
"hr_resize_y": 1024,
"alwayson_scripts": {"ADetailer": {"args": [{"ad_model": "face_yolov8n.pt"}]}},
"override_settings": {"sd_model_checkpoint": model_name},
}
client = httpx.AsyncClient()
response = await client.post(url, json=json_data, timeout=1200)
b64_image = response.json()["images"][0]
file, file_path = tempfile.mkstemp()
with open(file_path, "wb") as f:
f.write(base64.b64decode(b64_image))
return file_path