| 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) |
|
|
| |
| url = "https://image.novelai.net/ai/generate-image" |
|
|
| |
| 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" |
|
|
| |
| def generate(prompt="1girl, best quality, amazing quality, very aesthetic, absurdres"): |
| |
| 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, |
| } |
| } |
|
|
| |
| response = requests.post(url, impersonate="safari15_5", json=payload, headers=headers, timeout=120) |
| |
| |
| |
| |
| |
| 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 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) |
|
|
| |
| with open("prompts.csv") as f: |
| prompts = f.readlines() |
| |
| |
| generate("abcd") |
|
|
| |
| 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 |