P01yH3dr0n commited on
Commit
a411e12
1 Parent(s): d696794

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +152 -0
utils.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import io
3
+ import zipfile
4
+ import os
5
+
6
+ from PIL import Image
7
+ from httpx import AsyncClient
8
+
9
+
10
+ jwt_token = os.environ.get('token')
11
+ url = "https://api.novelai.net/ai/generate-image"
12
+ global_client = AsyncClient(timeout=100)
13
+
14
+
15
+ def set_token(token):
16
+ global jwt_token, global_client
17
+ if jwt_token == token:
18
+ return
19
+ jwt_token = token
20
+ global_client = AsyncClient(
21
+ timeout=100,
22
+ headers = {
23
+ "Authorization": f"Bearer {jwt_token}",
24
+ "Content-Type": "application/json",
25
+ "Origin": "https://novelai.net",
26
+ "Referer": "https://novelai.net/"
27
+ }
28
+ )
29
+
30
+
31
+ async def remote_login(end_point, password):
32
+ payload = {"password": password}
33
+ response = await global_client.post(f'{end_point}/login', params=payload)
34
+ if response.status_code == 200:
35
+ return response.json()["status"]
36
+ else:
37
+ return None
38
+
39
+
40
+ async def remote_gen(
41
+ end_point="http://127.0.0.1:7000",
42
+ input_text="",
43
+ quality_tags="",
44
+ negative_prompt="",
45
+ seed=-1,
46
+ scale=5.0,
47
+ width=1024,
48
+ height=1024,
49
+ steps=28,
50
+ sampler="k_euler",
51
+ schedule='native',
52
+ smea=False,
53
+ dyn=False,
54
+ dyn_threshold=False,
55
+ chg_rescale=0,
56
+ ):
57
+ payload = {
58
+ "prompt": f'{input_text}, {quality_tags}',
59
+ "neg_prompt": negative_prompt,
60
+ "seed": seed,
61
+ "scale": scale,
62
+ "width": width,
63
+ "height": height,
64
+ "steps": steps,
65
+ "sampler": sampler,
66
+ "schedule": schedule,
67
+ "smea": smea,
68
+ "dyn": dyn,
69
+ "dyn_threshold": dyn_threshold,
70
+ "chg_rescale": chg_rescale,
71
+ }
72
+ response = await global_client.post(f'{end_point}/gen', json=payload)
73
+ if response.status_code == 200:
74
+ mem_file = io.BytesIO(response.content)
75
+ mem_file.seek(0)
76
+ return Image.open(mem_file)
77
+ else:
78
+ return None
79
+
80
+
81
+ async def generate_novelai_image(
82
+ input_text="",
83
+ negative_prompt="",
84
+ seed=-1,
85
+ scale=5.0,
86
+ width=1024,
87
+ height=1024,
88
+ steps=28,
89
+ sampler="k_euler",
90
+ schedule='native',
91
+ smea=False,
92
+ dyn=False,
93
+ dyn_threshold=False,
94
+ cfg_rescale=0,
95
+ ):
96
+ # Assign a random seed if seed is -1
97
+ if seed == -1:
98
+ seed = random.randint(0, 2**32 - 1)
99
+
100
+ # Define the payload
101
+ payload = {
102
+ "action": "generate",
103
+ "input": input_text,
104
+ "model": "nai-diffusion-3",
105
+ "parameters": {
106
+ "width": width,
107
+ "height": height,
108
+ "scale": scale,
109
+ "sampler": sampler,
110
+ "steps": steps,
111
+ "n_samples": 1,
112
+ "ucPreset": 0,
113
+ "add_original_image": False,
114
+ "cfg_rescale": cfg_rescale,
115
+ "controlnet_strength": 1,
116
+ "dynamic_thresholding": dyn_threshold,
117
+ "legacy": False,
118
+ "negative_prompt": negative_prompt,
119
+ "noise_schedule": schedule,
120
+ "qualityToggle": True,
121
+ "seed": seed,
122
+ "sm": smea,
123
+ "sm_dyn": dyn,
124
+ "uncond_scale": 1,
125
+ }
126
+ }
127
+
128
+ # Send the POST request
129
+ response = await global_client.post(url, json=payload)
130
+
131
+ # Process the response
132
+ if response.headers.get('Content-Type') == 'application/x-zip-compressed':
133
+ zipfile_in_memory = io.BytesIO(response.content)
134
+ with zipfile.ZipFile(zipfile_in_memory, 'r') as zip_ref:
135
+ file_names = zip_ref.namelist()
136
+ if file_names:
137
+ with zip_ref.open(file_names[0]) as file:
138
+ return file.read(), payload
139
+ else:
140
+ return "NAI doesn't return any images", response
141
+ else:
142
+ return "Generation failed", response
143
+
144
+
145
+ def free_check(width, height, steps):
146
+ return width * height <= 1024 * 1024 and steps<=28
147
+
148
+
149
+ def image_from_bytes(data):
150
+ img_file = io.BytesIO(data)
151
+ img_file.seek(0)
152
+ return Image.open(img_file)