shweaung commited on
Commit
844af19
·
verified ·
1 Parent(s): 4a89318

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -103
app.py CHANGED
@@ -8,7 +8,8 @@ from io import BytesIO
8
  import html
9
  import re
10
 
11
-
 
12
 
13
  class Prodia:
14
  def __init__(self, api_key, base=None):
@@ -70,20 +71,21 @@ class Prodia:
70
 
71
  return response
72
 
 
 
 
 
 
 
73
 
74
  def image_to_base64(image):
75
- # Convert the image to bytes
76
  buffered = BytesIO()
77
- image.save(buffered, format="PNG") # You can change format to PNG if needed
78
-
79
- # Encode the bytes to base64
80
  img_str = base64.b64encode(buffered.getvalue())
81
-
82
- return img_str.decode('utf-8') # Convert bytes to string
83
-
84
 
85
  def remove_id_and_ext(text):
86
- text = re.sub(r'\[.*\]$', '', text)
87
  extension = text[-12:].strip()
88
  if extension == "safetensors":
89
  text = text[:-13]
@@ -91,7 +93,6 @@ def remove_id_and_ext(text):
91
  text = text[:-4]
92
  return text
93
 
94
-
95
  def get_data(text):
96
  results = {}
97
  patterns = {
@@ -119,11 +120,8 @@ def get_data(text):
119
  results['h'] = None
120
  return results
121
 
122
-
123
  def send_to_txt2img(image):
124
-
125
  result = {tabs: gr.update(selected="t2i")}
126
-
127
  try:
128
  text = image.info['parameters']
129
  data = get_data(text)
@@ -140,13 +138,10 @@ def send_to_txt2img(image):
140
  else:
141
  result[model] = gr.update()
142
  return result
143
-
144
  except Exception as e:
145
  print(e)
146
-
147
  return result
148
 
149
-
150
  prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
151
  model_list = prodia_client.list_models()
152
  model_names = {}
@@ -155,10 +150,10 @@ for model_name in model_list:
155
  name_without_ext = remove_id_and_ext(model_name)
156
  model_names[name_without_ext] = model_name
157
 
158
-
159
  def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
 
160
  result = prodia_client.generate({
161
- "prompt": prompt,
162
  "negative_prompt": negative_prompt,
163
  "model": model,
164
  "steps": steps,
@@ -168,17 +163,15 @@ def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, he
168
  "height": height,
169
  "seed": seed
170
  })
171
-
172
  job = prodia_client.wait(result)
173
-
174
  return job["imageUrl"]
175
 
176
-
177
  def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
 
178
  result = prodia_client.transform({
179
  "imageData": image_to_base64(input_image),
180
  "denoising_strength": denoising,
181
- "prompt": prompt,
182
  "negative_prompt": negative_prompt,
183
  "model": model,
184
  "steps": steps,
@@ -188,12 +181,9 @@ def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampl
188
  "height": height,
189
  "seed": seed
190
  })
191
-
192
  job = prodia_client.wait(result)
193
-
194
  return job["imageUrl"]
195
 
196
-
197
  css = """
198
  #generate {
199
  height: 100%;
@@ -229,99 +219,125 @@ with gr.Blocks(css=css) as demo:
229
 
230
  with gr.Row():
231
  with gr.Column(scale=1):
232
- width = gr.Slider(label="Width", maximum=1024, value=512, step=8)
233
- height = gr.Slider(label="Height", maximum=1024, value=512, step=8)
234
-
235
- with gr.Column(scale=1):
236
- batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
237
- batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
238
-
239
- cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
240
- seed = gr.Number(label="Seed", value=-1)
 
 
 
241
 
242
- with gr.Column(scale=2):
243
- image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
 
245
- text_button.click(txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height,
246
- seed], outputs=image_output, concurrency_limit=64)
247
-
248
- with gr.Tab("img2img", id='i2i'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  with gr.Row():
250
  with gr.Column(scale=6, min_width=600):
251
- i2i_prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt", show_label=False, lines=3)
252
- i2i_negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3, value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
 
 
 
253
  with gr.Column():
254
- i2i_text_button = gr.Button("Generate", variant='primary', elem_id="generate")
255
-
256
  with gr.Row():
257
  with gr.Column(scale=3):
258
  with gr.Tab("Generation"):
259
- i2i_image_input = gr.Image(type="pil")
260
-
261
  with gr.Row():
262
  with gr.Column(scale=1):
263
- i2i_sampler = gr.Dropdown(value="Euler a", show_label=True, label="Sampling Method", choices=prodia_client.list_samplers())
264
-
 
265
  with gr.Column(scale=1):
266
- i2i_steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=25, value=20, step=1)
267
 
268
  with gr.Row():
269
  with gr.Column(scale=1):
270
- i2i_width = gr.Slider(label="Width", maximum=1024, value=512, step=8)
271
- i2i_height = gr.Slider(label="Height", maximum=1024, value=512, step=8)
272
-
273
  with gr.Column(scale=1):
274
- i2i_batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
275
- i2i_batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
276
-
277
- i2i_cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
278
- i2i_denoising = gr.Slider(label="Denoising Strength", minimum=0, maximum=1, value=0.7, step=0.1)
279
- i2i_seed = gr.Number(label="Seed", value=-1)
280
 
281
  with gr.Column(scale=2):
282
- i2i_image_output = gr.Image(value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
283
-
284
- i2i_text_button.click(img2img, inputs=[i2i_image_input, i2i_denoising, i2i_prompt, i2i_negative_prompt,
285
- model, i2i_steps, i2i_sampler, i2i_cfg_scale, i2i_width, i2i_height,
286
- i2i_seed], outputs=i2i_image_output, concurrency_limit=64)
287
-
288
- with gr.Tab("PNG Info"):
289
- def plaintext_to_html(text, classname=None):
290
- content = "<br>\n".join(html.escape(x) for x in text.split('\n'))
291
-
292
- return f"<p class='{classname}'>{content}</p>" if classname else f"<p>{content}</p>"
293
-
294
-
295
- def get_exif_data(image):
296
- items = image.info
297
-
298
- info = ''
299
- for key, text in items.items():
300
- info += f"""
301
- <div>
302
- <p><b>{plaintext_to_html(str(key))}</b></p>
303
- <p>{plaintext_to_html(str(text))}</p>
304
- </div>
305
- """.strip()+"\n"
306
-
307
- if len(info) == 0:
308
- message = "Nothing found in the image."
309
- info = f"<div><p>{message}<p></div>"
310
-
311
- return info
312
-
313
- with gr.Row():
314
- with gr.Column():
315
- image_input = gr.Image(type="pil")
316
-
317
- with gr.Column():
318
- exif_output = gr.HTML(label="EXIF Data")
319
- send_to_txt2img_btn = gr.Button("Send to txt2img")
320
-
321
- image_input.upload(get_exif_data, inputs=[image_input], outputs=exif_output)
322
- send_to_txt2img_btn.click(send_to_txt2img, inputs=[image_input], outputs=[tabs, prompt, negative_prompt,
323
- steps, seed, model, sampler,
324
- width, height, cfg_scale],
325
- concurrency_limit=32)
326
 
327
- demo.queue(max_size=40, api_open=False).launch(max_threads=128, show_api=False)
 
 
 
8
  import html
9
  import re
10
 
11
+ # Free Google Translate API endpoint (without API key)
12
+ GOOGLE_TRANSLATE_API_URL = "https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=en&dt=t&q="
13
 
14
  class Prodia:
15
  def __init__(self, api_key, base=None):
 
71
 
72
  return response
73
 
74
+ # Function to translate text using Google Translate API
75
+ def translate_prompt(prompt):
76
+ url = GOOGLE_TRANSLATE_API_URL + requests.utils.quote(prompt)
77
+ response = requests.get(url)
78
+ translated_text = json.loads(response.text)[0][0][0]
79
+ return translated_text
80
 
81
  def image_to_base64(image):
 
82
  buffered = BytesIO()
83
+ image.save(buffered, format="PNG")
 
 
84
  img_str = base64.b64encode(buffered.getvalue())
85
+ return img_str.decode('utf-8')
 
 
86
 
87
  def remove_id_and_ext(text):
88
+ text = re.sub(r'.*$', '', text)
89
  extension = text[-12:].strip()
90
  if extension == "safetensors":
91
  text = text[:-13]
 
93
  text = text[:-4]
94
  return text
95
 
 
96
  def get_data(text):
97
  results = {}
98
  patterns = {
 
120
  results['h'] = None
121
  return results
122
 
 
123
  def send_to_txt2img(image):
 
124
  result = {tabs: gr.update(selected="t2i")}
 
125
  try:
126
  text = image.info['parameters']
127
  data = get_data(text)
 
138
  else:
139
  result[model] = gr.update()
140
  return result
 
141
  except Exception as e:
142
  print(e)
 
143
  return result
144
 
 
145
  prodia_client = Prodia(api_key=os.getenv("PRODIA_API_KEY"))
146
  model_list = prodia_client.list_models()
147
  model_names = {}
 
150
  name_without_ext = remove_id_and_ext(model_name)
151
  model_names[name_without_ext] = model_name
152
 
 
153
  def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
154
+ translated_prompt = translate_prompt(prompt) # Translate the prompt to English
155
  result = prodia_client.generate({
156
+ "prompt": translated_prompt,
157
  "negative_prompt": negative_prompt,
158
  "model": model,
159
  "steps": steps,
 
163
  "height": height,
164
  "seed": seed
165
  })
 
166
  job = prodia_client.wait(result)
 
167
  return job["imageUrl"]
168
 
 
169
  def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
170
+ translated_prompt = translate_prompt(prompt) # Translate the prompt to English
171
  result = prodia_client.transform({
172
  "imageData": image_to_base64(input_image),
173
  "denoising_strength": denoising,
174
+ "prompt": translated_prompt,
175
  "negative_prompt": negative_prompt,
176
  "model": model,
177
  "steps": steps,
 
181
  "height": height,
182
  "seed": seed
183
  })
 
184
  job = prodia_client.wait(result)
 
185
  return job["imageUrl"]
186
 
 
187
  css = """
188
  #generate {
189
  height: 100%;
 
219
 
220
  with gr.Row():
221
  with gr.Column(scale=1):
222
+ # Function for translating the prompt using the free Google Translate API
223
+ def translate_prompt(prompt, target_language="en"):
224
+ try:
225
+ # Set up the translation endpoint and parameters
226
+ url = "https://translate.googleapis.com/translate_a/single"
227
+ params = {
228
+ "client": "gtx",
229
+ "sl": "auto", # Source language is automatically detected
230
+ "tl": target_language, # Target language
231
+ "dt": "t", # Request translation only
232
+ "q": prompt, # The text to translate
233
+ }
234
 
235
+ # Make the GET request to the translation API
236
+ response = requests.get(url, params=params)
237
+
238
+ # Extract the translated text from the response
239
+ translated_text = response.json()[0][0][0]
240
+ return translated_text
241
+
242
+ except Exception as e:
243
+ print(f"Error translating prompt: {e}")
244
+ return prompt # In case of error, return the original prompt
245
+
246
+
247
+ # Modify txt2img function to include prompt translation
248
+ def txt2img(prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
249
+ translated_prompt = translate_prompt(prompt)
250
+ translated_negative_prompt = translate_prompt(negative_prompt)
251
 
252
+ result = prodia_client.generate({
253
+ "prompt": translated_prompt,
254
+ "negative_prompt": translated_negative_prompt,
255
+ "model": model,
256
+ "steps": steps,
257
+ "sampler": sampler,
258
+ "cfg_scale": cfg_scale,
259
+ "width": width,
260
+ "height": height,
261
+ "seed": seed
262
+ })
263
+
264
+ job = prodia_client.wait(result)
265
+
266
+ return job["imageUrl"]
267
+
268
+
269
+ # Modify img2img function to include prompt translation
270
+ def img2img(input_image, denoising, prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed):
271
+ translated_prompt = translate_prompt(prompt)
272
+ translated_negative_prompt = translate_prompt(negative_prompt)
273
+
274
+ result = prodia_client.transform({
275
+ "imageData": image_to_base64(input_image),
276
+ "denoising_strength": denoising,
277
+ "prompt": translated_prompt,
278
+ "negative_prompt": translated_negative_prompt,
279
+ "model": model,
280
+ "steps": steps,
281
+ "sampler": sampler,
282
+ "cfg_scale": cfg_scale,
283
+ "width": width,
284
+ "height": height,
285
+ "seed": seed
286
+ })
287
+
288
+ job = prodia_client.wait(result)
289
+
290
+ return job["imageUrl"]
291
+
292
+ # Add a translation option in the UI
293
+ with gr.Blocks(css=css) as demo:
294
+ with gr.Row():
295
+ with gr.Column(scale=6):
296
+ model = gr.Dropdown(interactive=True, value="absolutereality_v181.safetensors [3d9d4d2b]", show_label=True,
297
+ label="Stable Diffusion Checkpoint", choices=prodia_client.list_models())
298
+ with gr.Column(scale=1):
299
+ gr.Markdown(elem_id="powered-by-prodia",
300
+ value="AUTOMATIC1111 Stable Diffusion Web UI.<br>Powered by [Prodia](https://prodia.com).<br>For more features and faster generation times check out our [API Docs](https://docs.prodia.com/reference/getting-started-guide).")
301
+
302
+ with gr.Tabs() as tabs:
303
+ with gr.Tab("txt2img", id='t2i'):
304
  with gr.Row():
305
  with gr.Column(scale=6, min_width=600):
306
+ prompt = gr.Textbox("space warrior, beautiful, female, ultrarealistic, soft lighting, 8k", placeholder="Prompt",
307
+ show_label=False, lines=3)
308
+ negative_prompt = gr.Textbox(placeholder="Negative Prompt", show_label=False, lines=3,
309
+ value="3d, cartoon, anime, (deformed eyes, nose, ears, nose), bad anatomy, ugly")
310
+ translate_option = gr.Checkbox(label="Translate Prompt to English", value=True)
311
  with gr.Column():
312
+ text_button = gr.Button("Generate", variant='primary', elem_id="generate")
313
+
314
  with gr.Row():
315
  with gr.Column(scale=3):
316
  with gr.Tab("Generation"):
 
 
317
  with gr.Row():
318
  with gr.Column(scale=1):
319
+ sampler = gr.Dropdown(value="DPM++ 2M Karras", show_label=True, label="Sampling Method",
320
+ choices=prodia_client.list_samplers())
321
+
322
  with gr.Column(scale=1):
323
+ steps = gr.Slider(label="Sampling Steps", minimum=1, maximum=25, value=20, step=1)
324
 
325
  with gr.Row():
326
  with gr.Column(scale=1):
327
+ width = gr.Slider(label="Width", maximum=1024, value=512, step=8)
328
+ height = gr.Slider(label="Height", maximum=1024, value=512, step=8)
329
+
330
  with gr.Column(scale=1):
331
+ batch_size = gr.Slider(label="Batch Size", maximum=1, value=1)
332
+ batch_count = gr.Slider(label="Batch Count", maximum=1, value=1)
333
+
334
+ cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, value=7, step=1)
335
+ seed = gr.Number(label="Seed", value=-1)
 
336
 
337
  with gr.Column(scale=2):
338
+ image_output = gr.Image(
339
+ value="https://images.prodia.xyz/8ede1a7c-c0ee-4ded-987d-6ffed35fc477.png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
 
341
+ text_button.click(fn=txt2img, inputs=[prompt, negative_prompt, model, steps, sampler, cfg_scale, width, height, seed],
342
+ outputs=image_output, concurrency_limit=64)
343
+ demo.queue(max_size=40, api_open=False).launch(max_threads=128, show_api=False)