Spaces:
Paused
Paused
File size: 2,489 Bytes
a411e12 2af6387 39661d4 a411e12 76a000e a411e12 2af6387 a411e12 2af6387 a411e12 2af6387 a411e12 2af6387 a411e12 2af6387 a411e12 39661d4 a411e12 39661d4 a411e12 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import random
import io
import zipfile
import requests
import json
from PIL import Image
jwt_token = ''
url = "https://api.novelai.net/ai/generate-image"
headers = {}
def set_token(token):
global jwt_token, headers
if jwt_token == token:
return
jwt_token = token
headers = {
"Authorization": f"Bearer {jwt_token}",
"Content-Type": "application/json",
"Origin": "https://novelai.net",
"Referer": "https://novelai.net/"
}
def generate_novelai_image(
input_text="",
negative_prompt="",
seed=-1,
scale=5.0,
width=1024,
height=1024,
steps=28,
sampler="k_euler",
schedule='native',
smea=False,
dyn=False,
dyn_threshold=False,
cfg_rescale=0,
):
# Assign a random seed if seed is -1
if seed == -1:
seed = random.randint(0, 2**32 - 1)
# Define the payload
payload = {
"action": "generate",
"input": input_text,
"model": "nai-diffusion-3",
"parameters": {
"width": width,
"height": height,
"scale": scale,
"sampler": sampler,
"steps": steps,
"n_samples": 1,
"ucPreset": 0,
"add_original_image": False,
"cfg_rescale": cfg_rescale,
"controlnet_strength": 1,
"dynamic_thresholding": dyn_threshold,
"legacy": False,
"negative_prompt": negative_prompt,
"noise_schedule": schedule,
"qualityToggle": True,
"seed": seed,
"sm": smea,
"sm_dyn": dyn,
"uncond_scale": 1,
}
}
# Send the POST request
response = requests.post(url, json=payload, headers=headers)
# Process the response
if response.headers.get('Content-Type') == 'application/x-zip-compressed':
zipfile_in_memory = io.BytesIO(response.content)
with zipfile.ZipFile(zipfile_in_memory, 'r') as zip_ref:
file_names = zip_ref.namelist()
if file_names:
with zip_ref.open(file_names[0]) as file:
return file.read(), payload
else:
return "NAI doesn't return any images", json.loads(response.content)
else:
return "Generation failed", json.loads(response.content)
def image_from_bytes(data):
img_file = io.BytesIO(data)
img_file.seek(0)
return Image.open(img_file) |