dragon-bot/app/stable_diffusion.py
2023-09-14 09:37:15 -07:00

33 lines
913 B
Python
Executable File

from bs4 import BeautifulSoup
import base64
import httpx
import tempfile
async def generate_image(ip, port, positives, negatives, steps, enable_upscale):
url = "http://" + ip + ":" + port + "/sdapi/v1/txt2img"
json_data = {
"prompt": positives,
"negative_prompt": negatives,
"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"}]}},
}
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