File size: 3,748 Bytes
bc1ecc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
97
98
99
100
101
102
import random
import io
import zipfile
import numpy as np
from PIL.PngImagePlugin import PngInfo
from PIL import Image
from curl_cffi import requests
from tqdm import tqdm

jwt_token = ""
random_seed = random.randint(0, 2**32 - 1)

# Define the API URL
url = "https://image.novelai.net/ai/generate-image"

# Set the headers
headers = {
    "Authorization": f"Bearer {jwt_token}",
    "Accept": "application/json, text/plain, */*",
    "Content-Type": "application/json",
    "Origin": "https://novelai.net",
    "Referer": "https://novelai.net/"
}

QUALITY_TAGS = "best quality, amazing quality, very aesthetic, absurdres"

# Define the payload
def generate(prompt="1girl, best quality, amazing quality, very aesthetic, absurdres"):
    # neg_prompt = "nsfw, lowres, {bad}, error, fewer, extra, missing, worst quality, jpeg artifacts, bad quality, watermark, unfinished, displeasing, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract]"
    payload = {
        "action": "generate",
        "input": f'{prompt}, best quality, amazing quality, very aesthetic, absurdres',
        "model": "nai-diffusion-3",
        "parameters": {
            "width": 832,
            "height": 1216,
            "scale": 5,
            "sampler": "k_euler_ancestral",
            "steps": 28,
            "n_samples": 1,
            "ucPreset": 0,
            "qualityToggle": True,
            "add_original_image": False,
            "cfg_rescale": 0,
            "controlnet_strength": 1,
            "dynamic_thresholding": False,
            "legacy": False,
            "noise_schedule": "karras",
            "seed": 8888,
            "sm": False,
            "sm_dyn": False,
            "uncond_scale": 1,
            "negative_prompt":"nsfw, lowres, bad, error, fewer, extra, missing, worst quality, jpeg artifacts, bad quality, watermark, unfinished, displeasing, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract], lowres, bad, error, fewer, extra, missing, worst quality, jpeg artifacts, bad quality, unfinished, displeasing, chromatic aberration, signature, extra digits, artistic error, username, scan, [abstract], chibi,doll, +_+",
            "legacy_v3_extend": False,
        }
    }

    # Send the POST request
    response = requests.post(url, impersonate="safari15_5", json=payload, headers=headers, timeout=120)
    
    # Save the response content (assuming it's a zip file)
    # with open('images.zip', 'wb') as f:
    #     f.write(response.content)
    
    zipfile_in_memory = io.BytesIO(response.content)
    with zipfile.ZipFile(zipfile_in_memory, 'r') as zip_ref:
        # Extract the list of file names
        file_names = zip_ref.namelist()

        # Check if there are files in the zip
        if file_names:
            # Open the first file as an image
            with zip_ref.open(file_names[0]) as file:
                # Display the image
                return Image.open(io.BytesIO(file.read())), payload

def process_image_and_save(image, path):
    metadata = PngInfo()

    image = image.convert('RGBA')
    image = Image.fromarray(np.array(image)[:,:,:3])
    image.save(path, pnginfo=metadata, quality=95, format="WEBP")
    print(path)

# read prompts for testing
with open("prompts.csv") as f:
    prompts = f.readlines()
    
# warmup
generate("abcd")

# generate images
for i, prompt in tqdm(enumerate(prompts), total=len(prompts)):
    try:
        image, payload = generate(prompt.strip())
        image = image.convert('RGBA')
        image = Image.fromarray(np.array(image)[:,:,:3])
        fn = f"naiv3/{i+1}.webp"
        image.save(fn, quality=95, format="WEBP")
    except Exception as e:
        print(e)
        continue