zenafey commited on
Commit
c3ec568
1 Parent(s): 19ab7b2

Upload 2 files

Browse files
Files changed (2) hide show
  1. const.py +17 -0
  2. engine.py +188 -0
const.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ SAMPLER_LIST = ["Euler", "Euler a", "LMS", "Heun", "DPM2", "DPM2 a", "DPM++ 2S a", "DPM++ 2M", "DPM++ SDE", "DPM fast",
2
+ "DPM adaptive", "LMS Karras", "DPM2 Karras", "DPM2 a Karras", "DPM++ 2S a Karras", "DPM++ 2M Karras",
3
+ "DPM++ SDE Karras", "DDIM", "PLMS"]
4
+
5
+ SDXL_MODEL_LIST = ["sd_xl_base_1.0.safetensors [be9edd61]", "dreamshaperXL10_alpha2.safetensors [c8afe2ef]",
6
+ "dynavisionXL_0411.safetensors [c39cc051]"]
7
+
8
+ CMODULES = ['none', 'canny', 'depth', 'depth_leres', 'depth_leres++', 'hed', 'hed_safe', 'mediapipe_face', 'mlsd',
9
+ 'normal_map', 'openpose', 'openpose_hand', 'openpose_face', 'openpose_faceonly', 'openpose_full',
10
+ 'clip_vision', 'color', 'pidinet', 'pidinet_safe', 'pidinet_sketch', 'pidinet_scribble', 'scribble_xdog',
11
+ 'scribble_hed', 'segmentation', 'threshold', 'depth_zoe', 'normal_bae', 'oneformer_coco', 'oneformer_ade20k',
12
+ 'lineart', 'lineart_coarse', 'lineart_anime', 'lineart_standard', 'shuffle', 'tile_resample', 'invert',
13
+ 'lineart_anime_denoise', 'reference_only', 'reference_adain', 'reference_adain+attn', 'inpaint',
14
+ 'inpaint_only', 'inpaint_only+lama', 'tile_colorfix', 'tile_colorfix+sharp']
15
+
16
+ CMODELS = ['control_v11p_sd15_canny [d14c016b]', 'control_v11p_sd15_openpose [cab727d4]',
17
+ 'control_v11p_sd15_softedge [a8575a2a]', 'control_v11p_sd15_scribble [d4ba51ff]']
engine.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import requests
4
+ import random
5
+ import json
6
+ import base64
7
+ from io import BytesIO
8
+ from PIL import Image
9
+ from dotenv import load_dotenv
10
+
11
+ load_dotenv()
12
+
13
+
14
+ class Prodia:
15
+ def __init__(self, api_key, base=None):
16
+ self.base = base or "https://api.prodia.com/v1"
17
+ self.headers = {
18
+ "X-Prodia-Key": api_key
19
+ }
20
+
21
+ def sd_controlnet(self, params):
22
+ response = self._post(f"{self.base}/sd/controlnet", params)
23
+ return response.json()
24
+
25
+ def sd_transform(self, params):
26
+ response = self._post(f"{self.base}/sd/transform", params)
27
+ return response.json()
28
+
29
+ def sd_generate(self, params):
30
+ response = self._post(f"{self.base}/sd/generate", params)
31
+ return response.json()
32
+
33
+ def sdxl_generate(self, params):
34
+ response = self._post(f"{self.base}/sdxl/generate", params)
35
+ return response.json()
36
+
37
+ def get_job(self, job_id):
38
+ response = self._get(f"{self.base}/job/{job_id}")
39
+ return response.json()
40
+
41
+ def wait(self, job):
42
+ job_result = job
43
+
44
+ while job_result['status'] not in ['succeeded', 'failed']:
45
+ time.sleep(0.25)
46
+ job_result = self.get_job(job['job'])
47
+
48
+ if job_result['status'] == 'failed':
49
+ raise Exception("Job failed")
50
+
51
+ return job_result
52
+
53
+ def upload(self, file):
54
+ files = {'file': open(file, 'rb')}
55
+ img_id = requests.post(os.getenv("IMAGES_1"), files=files).json()['id']
56
+
57
+ payload = {
58
+ "content": "",
59
+ "nonce": f"{random.randint(1, 10000000)}H9X42KSEJFNNH",
60
+ "replies": [],
61
+ "attachments":
62
+ [img_id]
63
+ }
64
+ resp = requests.post(os.getenv("IMAGES_2"), json=payload, headers={"x-session-token": os.getenv("session-token")})
65
+ return f"{os.getenv('IMAGES_1')}/{img_id}/{resp.json()['attachments'][0]['filename']}"
66
+
67
+ def list_models(self):
68
+ response = self._get(f"{self.base}/models/list")
69
+ return response.json()
70
+
71
+ def _post(self, url, params):
72
+ headers = {
73
+ **self.headers,
74
+ "Content-Type": "application/json"
75
+ }
76
+ response = requests.post(url, headers=headers, data=json.dumps(params))
77
+
78
+ if response.status_code != 200:
79
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
80
+
81
+ return response
82
+
83
+ def _get(self, url):
84
+ response = requests.get(url, headers=self.headers)
85
+
86
+ if response.status_code != 200:
87
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
88
+
89
+ return response
90
+
91
+
92
+ def image_to_base64(image_path):
93
+ # Open the image with PIL
94
+ with Image.open(image_path) as image:
95
+ # Convert the image to bytes
96
+ buffered = BytesIO()
97
+ image.save(buffered, format="PNG") # You can change format to PNG if needed
98
+
99
+ # Encode the bytes to base64
100
+ img_str = base64.b64encode(buffered.getvalue())
101
+
102
+ return img_str.decode('utf-8') # Convert bytes to string
103
+
104
+
105
+ prodia_client = Prodia(api_key=os.getenv("PRODIA_X_KEY"))
106
+
107
+
108
+ def generate_sdxl(prompt, negative_prompt, model, steps, sampler, cfg_scale, seed):
109
+ result = prodia_client.sdxl_generate({
110
+ "prompt": prompt,
111
+ "negative_prompt": negative_prompt,
112
+ "model": model,
113
+ "steps": steps,
114
+ "sampler": sampler,
115
+ "cfg_scale": cfg_scale,
116
+ "seed": seed
117
+ })
118
+
119
+ job = prodia_client.wait(result)
120
+
121
+ return job["imageUrl"]
122
+
123
+
124
+ def generate_sd(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed, upscale):
125
+ result = prodia_client.sd_generate({
126
+ "prompt": prompt,
127
+ "negative_prompt": negative_prompt,
128
+ "model": model,
129
+ "steps": steps,
130
+ "sampler": sampler,
131
+ "cfg_scale": cfg_scale,
132
+ "seed": seed,
133
+ "upscale": upscale,
134
+ "width": width,
135
+ "height": height
136
+ })
137
+
138
+ job = prodia_client.wait(result)
139
+
140
+ return job["imageUrl"]
141
+
142
+
143
+ def transform_sd(image, model, prompt, denoising_strength, negative_prompt, steps, cfg_scale, seed, upscale, sampler):
144
+ image_url = prodia_client.upload(image)
145
+ result = prodia_client.sd_transform({
146
+ "imageUrl": image_url,
147
+ 'model': model,
148
+ 'prompt': prompt,
149
+ 'denoising_strength': denoising_strength,
150
+ 'negative_prompt': negative_prompt,
151
+ 'steps': steps,
152
+ 'cfg_scale': cfg_scale,
153
+ 'seed': seed,
154
+ 'upscale': upscale,
155
+ 'sampler': sampler
156
+ })
157
+
158
+ job = prodia_client.wait(result)
159
+
160
+ return job["imageUrl"]
161
+
162
+
163
+ def controlnet_sd(image, controlnet_model, controlnet_module, threshold_a, threshold_b, resize_mode, prompt, negative_prompt, steps, cfg_scale, seed, sampler, width, height):
164
+ print(image)
165
+ image_url = prodia_client.upload(image)
166
+ result = prodia_client.sd_transform({
167
+ "imageUrl": image_url,
168
+ "controlnet_model": controlnet_model,
169
+ "controlnet_module": controlnet_module,
170
+ "threshold_a": threshold_a,
171
+ "threshold_b": threshold_b,
172
+ "resize_mode": int(resize_mode),
173
+ "prompt": prompt,
174
+ 'negative_prompt': negative_prompt,
175
+ 'steps': steps,
176
+ 'cfg_scale': cfg_scale,
177
+ 'seed': seed,
178
+ 'sampler': sampler,
179
+ "height": height,
180
+ "width": width
181
+ })
182
+
183
+ job = prodia_client.wait(result)
184
+
185
+ return job["imageUrl"]
186
+
187
+ def get_models():
188
+ return prodia_client.list_models()