ehristoforu commited on
Commit
7fb7561
1 Parent(s): 47461ee

Upload 3 files

Browse files
Files changed (3) hide show
  1. README (7).md +11 -0
  2. app (3).py +266 -0
  3. requirements (3).txt +5 -0
README (7).md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: DreamDrop
3
+ emoji: 🥏
4
+ colorFrom: purple
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 3.47.1
8
+ app_file: app.py
9
+ pinned: true
10
+ ---
11
+
app (3).py ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import gradio as gr
3
+ import requests
4
+ import time
5
+ import json
6
+ import base64
7
+ import os
8
+ from io import BytesIO
9
+ import PIL
10
+ from PIL.ExifTags import TAGS
11
+ import html
12
+ import re
13
+
14
+ batch_count = 1
15
+ batch_size = 1
16
+
17
+ i2i_batch_count = 1
18
+ i2i_batch_size = 1
19
+
20
+ class Prodia:
21
+ def __init__(self, api_key, base=None):
22
+ self.base = base or "https://api.prodia.com/v1"
23
+ self.headers = {
24
+ "X-Prodia-Key": api_key
25
+ }
26
+
27
+ def generate(self, params):
28
+ response = self._post(f"{self.base}/sd/generate", params)
29
+ return response.json()
30
+
31
+ def transform(self, params):
32
+ response = self._post(f"{self.base}/sd/transform", params)
33
+ return response.json()
34
+
35
+ def controlnet(self, params):
36
+ response = self._post(f"{self.base}/sd/controlnet", params)
37
+ return response.json()
38
+
39
+ def get_job(self, job_id):
40
+ response = self._get(f"{self.base}/job/{job_id}")
41
+ return response.json()
42
+
43
+ def wait(self, job):
44
+ job_result = job
45
+
46
+ while job_result['status'] not in ['succeeded', 'failed']:
47
+ time.sleep(0.25)
48
+ job_result = self.get_job(job['job'])
49
+
50
+ return job_result
51
+
52
+ def list_models(self):
53
+ response = self._get(f"{self.base}/sd/models")
54
+ return response.json()
55
+
56
+ def list_samplers(self):
57
+ response = self._get(f"{self.base}/sd/samplers")
58
+ return response.json()
59
+
60
+ def _post(self, url, params):
61
+ headers = {
62
+ **self.headers,
63
+ "Content-Type": "application/json"
64
+ }
65
+ response = requests.post(url, headers=headers, data=json.dumps(params))
66
+
67
+ if response.status_code != 200:
68
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
69
+
70
+ return response
71
+
72
+ def _get(self, url):
73
+ response = requests.get(url, headers=self.headers)
74
+
75
+ if response.status_code != 200:
76
+ raise Exception(f"Bad Prodia Response: {response.status_code}")
77
+
78
+ return response
79
+
80
+
81
+ def image_to_base64(image):
82
+ # Convert the image to bytes
83
+ buffered = BytesIO()
84
+ image.save(buffered, format="PNG") # You can change format to PNG if needed
85
+
86
+ # Encode the bytes to base64
87
+ img_str = base64.b64encode(buffered.getvalue())
88
+
89
+ return img_str.decode('utf-8') # Convert bytes to string
90
+
91
+ def remove_id_and_ext(text):
92
+ text = re.sub(r'\[.*\]$', '', text)
93
+ extension = text[-12:].strip()
94
+ if extension == "safetensors":
95
+ text = text[:-13]
96
+ elif extension == "ckpt":
97
+ text = text[:-4]
98
+ return text
99
+
100
+ def get_data(text):
101
+ results = {}
102
+ patterns = {
103
+ 'prompt': r'(.*)',
104
+ 'negative_prompt': r'Negative prompt: (.*)',
105
+ 'steps': r'Steps: (\d+),',
106
+ 'seed': r'Seed: (\d+),',
107
+ 'sampler': r'Sampler:\s*([^\s,]+(?:\s+[^\s,]+)*)',
108
+ 'model': r'Model:\s*([^\s,]+)',
109
+ 'cfg_scale': r'CFG scale:\s*([\d\.]+)',
110
+ 'size': r'Size:\s*([0-9]+x[0-9]+)'
111
+ }
112
+ for key in ['prompt', 'negative_prompt', 'steps', 'seed', 'sampler', 'model', 'cfg_scale', 'size']:
113
+ match = re.search(patterns[key], text)
114
+ if match:
115
+ results[key] = match.group(1)
116
+ else:
117
+ results[key] = None
118
+ if results['size'] is not None:
119
+ w, h = results['size'].split("x")
120
+ results['w'] = w
121
+ results['h'] = h
122
+ else:
123
+ results['w'] = None
124
+ results['h'] = None
125
+ return results
126
+
127
+ def send_to_txt2img(image):
128
+
129
+ result = {tabs: gr.Tabs.update(selected="t2i")}
130
+
131
+ try:
132
+ text = image.info['parameters']
133
+ data = get_data(text)
134
+ result[prompt] = gr.update(value=data['prompt'])
135
+ result[negative_prompt] = gr.update(value=data['negative_prompt']) if data['negative_prompt'] is not None else gr.update()
136
+ result[steps] = gr.update(value=int(data['steps'])) if data['steps'] is not None else gr.update()
137
+ result[seed] = gr.update(value=int(data['seed'])) if data['seed'] is not None else gr.update()
138
+ result[cfg_scale] = gr.update(value=float(data['cfg_scale'])) if data['cfg_scale'] is not None else gr.update()
139
+ result[width] = gr.update(value=int(data['w'])) if data['w'] is not None else gr.update()
140
+ result[height] = gr.update(value=int(data['h'])) if data['h'] is not None else gr.update()
141
+ result[sampler] = gr.update(value=data['sampler']) if data['sampler'] is not None else gr.update()
142
+ if model in model_names:
143
+ result[model] = gr.update(value=model_names[model])
144
+ else:
145
+ result[model] = gr.update()
146
+ return result
147
+
148
+ except Exception as e:
149
+ print(e)
150
+ result[prompt] = gr.update()
151
+ result[negative_prompt] = gr.update()
152
+ result[steps] = gr.update()
153
+ result[seed] = gr.update()
154
+ result[cfg_scale] = gr.update()
155
+ result[width] = gr.update()
156
+ result[height] = gr.update()
157
+ result[sampler] = gr.update()
158
+ result[model] = gr.update()
159
+
160
+ return result
161
+
162
+
163
+ prodia_client = Prodia(api_key=os.getenv("super_api_key"))
164
+ model_list = prodia_client.list_models()
165
+ model_names = {}
166
+
167
+ for model_name in model_list:
168
+ name_without_ext = remove_id_and_ext(model_name)
169
+ model_names[name_without_ext] = model_name
170
+
171
+ def txt2img(prompt, negative_prompt, model, width, height):
172
+ result = prodia_client.generate({
173
+ "prompt": prompt,
174
+ "negative_prompt": negative_prompt,
175
+ "model": model,
176
+ "steps": 30,
177
+ "sampler": "DPM++ SDE",
178
+ "cfg_scale": 7,
179
+ "width": width,
180
+ "height": height,
181
+ "seed": -1
182
+ })
183
+
184
+ job = prodia_client.wait(result)
185
+
186
+ return job["imageUrl"]
187
+
188
+ def img2img(input_image, prompt, negative_prompt, model, width, height):
189
+ result = prodia_client.transform({
190
+ "imageData": image_to_base64(input_image),
191
+ "denoising_strength": 0.7,
192
+ "prompt": prompt,
193
+ "negative_prompt": negative_prompt,
194
+ "model": i2i_model,
195
+ "steps": 30,
196
+ "sampler": "DPM++ SDE",
197
+ "cfg_scale": 7,
198
+ "width": width,
199
+ "height": height,
200
+ "seed": -1
201
+ })
202
+
203
+ job = prodia_client.wait(result)
204
+
205
+ return job["imageUrl"]
206
+
207
+
208
+ css = """
209
+ #generate {
210
+ height: 100%;
211
+ }
212
+ """
213
+
214
+ with gr.Blocks(css=css, theme="Base") as demo:
215
+ gr.HTML(value="<h1><center>🥏 DreamDrop</center></h1>")
216
+ with gr.Tabs() as tabs:
217
+ with gr.Tab("Text to Image", id='t2i'):
218
+ with gr.Row():
219
+ with gr.Column(scale=6, min_width=600):
220
+ prompt = gr.Textbox(label="Prompt", placeholder="a cute cat, 8k", lines=2)
221
+ negative_prompt = gr.Textbox(label="Negative Prompt", value="text, blurry, fuzziness", lines=1)
222
+ with gr.Column():
223
+ text_button = gr.Button("Generate", variant='primary', elem_id="generate")
224
+
225
+ with gr.Row():
226
+ with gr.Column(scale=2):
227
+ image_output = gr.Image(label="Result Image")
228
+ with gr.Row():
229
+ with gr.Accordion("⚙️ Settings", open=False):
230
+ with gr.Column(scale=1):
231
+ model = gr.Dropdown(interactive=True, value="absolutereality_v181.safetensors [3d9d4d2b]",
232
+ show_label=True, label="Model",
233
+ choices=prodia_client.list_models())
234
+ width = gr.Slider(label="↔️ Width", maximum=1024, value=768, step=8)
235
+ height = gr.Slider(label="↕️ Height", maximum=1024, value=768, step=8)
236
+ text_button.click(txt2img, inputs=[prompt, negative_prompt, model, width, height], outputs=image_output)
237
+
238
+ with gr.Tab("Image to Image", id='i2i'):
239
+ with gr.Row():
240
+ with gr.Column(scale=6, min_width=600):
241
+ i2i_prompt = gr.Textbox(label="Prompt", placeholder="a cute cat, 8k", lines=3)
242
+ i2i_negative_prompt = gr.Textbox(label="Negative Prompt", lines=2, value="text, blurry, fuzziness")
243
+ with gr.Column():
244
+ i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")
245
+
246
+ with gr.Row():
247
+ with gr.Column(scale=3):
248
+ i2i_image_input = gr.Image(label="Input Image", type="pil")
249
+
250
+ with gr.Column(scale=2):
251
+ i2i_image_output = gr.Image(label="Result Image")
252
+ with gr.Row():
253
+ with gr.Accordion("⚙️ Settings", open=False):
254
+ with gr.Column(scale=1):
255
+ i2i_model = gr.Dropdown(interactive=True,
256
+ value="absolutereality_v181.safetensors [3d9d4d2b]",
257
+ show_label=True, label="Model",
258
+ choices=prodia_client.list_models())
259
+ with gr.Column(scale=1):
260
+ i2i_width = gr.Slider(label="↔️ Width", maximum=1024, value=768, step=8)
261
+ i2i_height = gr.Slider(label="↕️ Height", maximum=1024, value=768, step=8)
262
+
263
+ i2i_text_button.click(img2img, inputs=[i2i_image_input, i2i_prompt, i2i_negative_prompt, model, i2i_width, i2i_height], outputs=i2i_image_output)
264
+
265
+
266
+ demo.queue(concurrency_count=64, max_size=30, api_open=False).launch(max_threads=256, show_api=False)
requirements (3).txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ numpy
2
+ gradio
3
+ requests
4
+ pillow
5
+ pyexif