burhansyam commited on
Commit
058cd31
·
verified ·
1 Parent(s): ce820b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -27
app.py CHANGED
@@ -1,7 +1,10 @@
1
  import os
2
  import gradio as gr
 
3
  from random import randint
4
  from all_models import models
 
 
5
 
6
  css_code = os.getenv("DazDinGo_CSS")
7
 
@@ -18,52 +21,63 @@ def load_fn(models):
18
 
19
  load_fn(models)
20
 
21
- num_models = len(models)
22
- default_models = models[:num_models]
23
-
24
- def extend_choices(choices):
25
- return choices + (num_models - len(choices)) * ['NA']
26
-
27
- def update_imgbox(choices):
28
- choices_plus = extend_choices(choices)
29
- return [gr.Image(None, label=m, visible=(m != 'NA'), elem_id="custom_image") for m in choices_plus]
30
-
31
  def gen_fn(model_str, prompt):
32
  if model_str == 'NA':
33
- return None
34
  noise = str(randint(0, 4294967296))
35
  klir = '| ultra detail, ultra elaboration, ultra quality, perfect'
36
- return models_load[model_str](f'{prompt} {klir} {noise}')
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  def make_me():
39
  with gr.Row():
40
  with gr.Column(scale=4):
41
- txt_input = gr.Textbox(label='Your prompt:', lines=4, container=False, elem_id="custom_textbox", placeholder="Prompt", height=250)
 
42
 
43
  with gr.Column(scale=1):
44
- gen_button = gr.Button('Generate images', elem_id="custom_gen_button")
45
- stop_button = gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
 
46
 
47
  def on_generate_click():
48
- return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
49
 
50
  def on_stop_click():
51
- return gr.Button('Generate images', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
52
 
53
  gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
54
  stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
55
 
56
  with gr.Row():
57
- output = [gr.Image(label=m, width=512, max_height=768, elem_id="custom_image", show_label=True, interactive=False, show_share_button=False) for m in default_models]
58
- current_models = [gr.Textbox(m, visible=False) for m in default_models]
59
- for m, o in zip(current_models, output):
60
- gen_event = gen_button.click(gen_fn, [m, txt_input], o)
61
- stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button], cancels=[gen_event])
62
-
63
- with gr.Accordion('Model selection', elem_id="custom_accordion"):
64
- model_choice = gr.CheckboxGroup(models, label=f'{num_models} different models selected', value=default_models, interactive=True, elem_id="custom_checkbox_group")
65
- model_choice.change(update_imgbox, model_choice, output)
66
- model_choice.change(extend_choices, model_choice, current_models)
 
 
 
 
 
 
 
67
 
68
  with gr.Row():
69
  gr.HTML("")
 
1
  import os
2
  import gradio as gr
3
+ import base64
4
  from random import randint
5
  from all_models import models
6
+ from io import BytesIO
7
+ from PIL import Image
8
 
9
  css_code = os.getenv("DazDinGo_CSS")
10
 
 
21
 
22
  load_fn(models)
23
 
 
 
 
 
 
 
 
 
 
 
24
  def gen_fn(model_str, prompt):
25
  if model_str == 'NA':
26
+ return None, ""
27
  noise = str(randint(0, 4294967296))
28
  klir = '| ultra detail, ultra elaboration, ultra quality, perfect'
29
+ image = models_load[model_str](f'{prompt} {klir} {noise}')
30
+
31
+ # Convert to base64
32
+ buffered = BytesIO()
33
+ if isinstance(image, str): # if it's a file path
34
+ img = Image.open(image)
35
+ img.save(buffered, format="JPEG")
36
+ else: # if it's a PIL Image
37
+ image.save(buffered, format="JPEG")
38
+ img_str = base64.b64encode(buffered.getvalue()).decode()
39
+ base64_code = f"data:image/jpeg;base64,{img_str}"
40
+
41
+ return image, base64_code
42
 
43
  def make_me():
44
  with gr.Row():
45
  with gr.Column(scale=4):
46
+ txt_input = gr.Textbox(label='Your prompt:', lines=4, container=False,
47
+ elem_id="custom_textbox", placeholder="Prompt", height=250)
48
 
49
  with gr.Column(scale=1):
50
+ gen_button = gr.Button('Generate image', elem_id="custom_gen_button")
51
+ stop_button = gr.Button('Stop', variant='secondary', interactive=False,
52
+ elem_id="custom_stop_button")
53
 
54
  def on_generate_click():
55
+ return gr.Button('Generate image', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=True, elem_id="custom_stop_button")
56
 
57
  def on_stop_click():
58
+ return gr.Button('Generate image', elem_id="custom_gen_button"), gr.Button('Stop', variant='secondary', interactive=False, elem_id="custom_stop_button")
59
 
60
  gen_button.click(on_generate_click, inputs=None, outputs=[gen_button, stop_button])
61
  stop_button.click(on_stop_click, inputs=None, outputs=[gen_button, stop_button])
62
 
63
  with gr.Row():
64
+ with gr.Column():
65
+ model_dropdown = gr.Dropdown(models, label="Select Model",
66
+ value=models[0] if models else None)
67
+ output_image = gr.Image(label="Generated Image", width=512,
68
+ max_height=768, elem_id="custom_image",
69
+ show_label=True, interactive=False,
70
+ show_share_button=False)
71
+
72
+ # Base64 output
73
+ base64_output = gr.Textbox(label="Base64 Code", interactive=True,
74
+ placeholder="Base64 code will appear here after generation",
75
+ lines=4)
76
+
77
+ gen_event = gen_button.click(gen_fn, [model_dropdown, txt_input],
78
+ [output_image, base64_output])
79
+ stop_button.click(on_stop_click, inputs=None,
80
+ outputs=[gen_button, stop_button], cancels=[gen_event])
81
 
82
  with gr.Row():
83
  gr.HTML("")