import base64 import json import requests import socket import tempfile def generate_image(ip, port, prompt): 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": [ prompt, "", # 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, "", "", ], } response = requests.post(url, headers=headers, json=json_data) 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