Download sample_novelai.py from nyanko7/image-samples: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/datasets/nyanko7/image-samples/resolve/main/sample_novelai.py
- Command line
-
hf download hf://datasets/nyanko7/image-samples/sample_novelai.py
-
curl -L -o sample_novelai.py https://huggingface.co/datasets/nyanko7/image-samples/resolve/main/sample_novelai.py
3.75 kB
| 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 |