dragon-bot/app/stable_diffusion.py

69 lines
1.6 KiB
Python

import base64
import json
import httpx
import socket
import tempfile
async def generate_image(ip, port, positives, negatives):
url = "http://" + ip + ":" + port + "/api/predict/"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"DNT": "1",
"Connection": "keep-alive",
}
json_data = {
"fn_index": 11,
"data": [
positives,
negatives, # Negative Prompt
"None",
"None",
20, # Sampling Steps
"Euler a",
True, # enable face fix
False,
1,
1,
7,
-1,
-1,
0,
0,
0,
False,
512,
512,
True, # Enable highres fix
False,
0.7, # Denoising Strength
"None",
False,
False,
None,
"",
"Seed",
"", # What seed to use
"Steps",
"",
True,
False,
None,
"",
"",
],
}
client = httpx.AsyncClient()
response = await client.post(url, headers=headers, json=json_data, timeout=300)
imageb64 = response.json()["data"][0][0].split("data:image/png;base64,")[1]
imgdata = base64.b64decode(imageb64)
file, file_path = tempfile.mkstemp()
with open(file_path, "wb") as f:
f.write(imgdata)
return file_path