Davit12 commited on
Commit
66c2be1
·
verified ·
1 Parent(s): 8b1c328

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -29
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import gradio as gr
2
  from random import randint
3
  from all_models import models
4
- from PIL import Image
5
- import io
6
 
7
  def load_fn(models):
8
  global models_load
@@ -28,27 +26,18 @@ def update_imgbox(choices, height, width):
28
  choices_plus = extend_choices(choices)
29
  return [gr.Image(None, label=m, visible=(m != 'NA'), height=height, width=width) for m in choices_plus]
30
 
31
- def resize_image(image, height, width):
32
- """Resize image to specified dimensions"""
33
- if image is None:
34
- return None
35
- try:
36
- # Convert Gradio image to PIL Image
37
- pil_image = Image.fromarray(image)
38
- # Resize while maintaining aspect ratio if one dimension is None
39
- resized_image = pil_image.resize((width, height), Image.Resampling.LANCZOS)
40
- return resized_image
41
- except Exception as e:
42
- return image # Return original if resize fails
43
-
44
- def gen_fn(model_str, prompt, height, width):
45
  if model_str == 'NA':
46
  return None
47
  noise = str(randint(0, 99999999999))
48
- # Generate image
49
- generated = models_load[model_str](f'{prompt} {noise}')
50
- # Resize to specified dimensions
51
- return resize_image(generated, height, width)
 
 
 
 
52
 
53
  with gr.Blocks() as demo:
54
  with gr.Tab('Multiple models'):
@@ -56,8 +45,21 @@ with gr.Blocks() as demo:
56
  model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} different models', value=default_models, multiselect=True, max_choices=num_models, interactive=True, filterable=False)
57
 
58
  with gr.Row():
59
- img_height = gr.Slider(100, 1200, value=1200, step=10, label='Image Height (px)')
60
- img_width = gr.Slider(100, 1920, value=1920, step=10, label='Image Width (px)')
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  txt_input = gr.Textbox(label='Prompt text')
63
  gen_button = gr.Button('Generate')
@@ -65,7 +67,7 @@ with gr.Blocks() as demo:
65
  gen_button.click(lambda s: gr.update(interactive=True), None, stop_button)
66
 
67
  with gr.Row():
68
- output = [gr.Image(label=m, height=300, width=300) for m in default_models]
69
  current_models = [gr.Textbox(m, visible=False) for m in default_models]
70
 
71
  model_choice.change(update_imgbox, [model_choice, img_height, img_width], output)
@@ -76,7 +78,7 @@ with gr.Blocks() as demo:
76
  for m, o in zip(current_models, output):
77
  gen_event = gen_button.click(
78
  gen_fn,
79
- [m, txt_input, img_height, img_width],
80
  o,
81
  queue=False
82
  )
@@ -86,8 +88,21 @@ with gr.Blocks() as demo:
86
  txt_input2 = gr.Textbox(label='Prompt text')
87
 
88
  with gr.Row():
89
- img_height2 = gr.Slider(100, 1200, value=1200, step=10, label='Image Height (px)')
90
- img_width2 = gr.Slider(100, 1920, value=1920, step=10, label='Image Width (px)')
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  max_images = 16
93
  num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
@@ -97,7 +112,7 @@ with gr.Blocks() as demo:
97
  gen_button2.click(lambda s: gr.update(interactive=True), None, stop_button2)
98
 
99
  with gr.Row():
100
- output2 = [gr.Image(label='', height=300, width=300) for _ in range(max_images)]
101
 
102
  for i, o in enumerate(output2):
103
  img_i = gr.Number(i, visible=False)
@@ -105,8 +120,8 @@ with gr.Blocks() as demo:
105
  img_height2.change(lambda h: gr.update(height=h), img_height2, o)
106
  img_width2.change(lambda w: gr.update(width=w), img_width2, o)
107
  gen_event2 = gen_button2.click(
108
- lambda i, n, m, t, h, w: gen_fn(m, t, h, w) if (i < n) else None,
109
- [img_i, num_images, model_choice2, txt_input2, img_height2, img_width2],
110
  o
111
  )
112
  stop_button2.click(lambda s: gr.update(interactive=False), None, stop_button2, cancels=[gen_event2])
 
1
  import gradio as gr
2
  from random import randint
3
  from all_models import models
 
 
4
 
5
  def load_fn(models):
6
  global models_load
 
26
  choices_plus = extend_choices(choices)
27
  return [gr.Image(None, label=m, visible=(m != 'NA'), height=height, width=width) for m in choices_plus]
28
 
29
+ def gen_fn(model_str, prompt, height, width, use_ratio):
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  if model_str == 'NA':
31
  return None
32
  noise = str(randint(0, 99999999999))
33
+
34
+ # Wenn 16:10 Verhältnis gewünscht, Höhe anpassen
35
+ if use_ratio:
36
+ height = int(width * 10 / 16) # 16:10 Verhältnis
37
+
38
+ # Größe in den Prompt einfügen
39
+ size_prompt = f"{prompt}, {width}x{height} resolution {noise}"
40
+ return models_load[model_str](size_prompt)
41
 
42
  with gr.Blocks() as demo:
43
  with gr.Tab('Multiple models'):
 
45
  model_choice = gr.CheckboxGroup(models, label=f'Choose up to {num_models} different models', value=default_models, multiselect=True, max_choices=num_models, interactive=True, filterable=False)
46
 
47
  with gr.Row():
48
+ img_width = gr.Slider(100, 1200, value=1024, step=2, label='Image Width (px)')
49
+ img_height = gr.Slider(100, 1200, value=640, step=2, label='Image Height (px)')
50
+ use_ratio = gr.Checkbox(label='Use 16:10 ratio', value=True)
51
+
52
+ # Höhe aktualisieren wenn Ratio aktiviert
53
+ img_width.change(
54
+ lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
55
+ [img_width, use_ratio],
56
+ img_height
57
+ )
58
+ use_ratio.change(
59
+ lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
60
+ [img_width, use_ratio],
61
+ img_height
62
+ )
63
 
64
  txt_input = gr.Textbox(label='Prompt text')
65
  gen_button = gr.Button('Generate')
 
67
  gen_button.click(lambda s: gr.update(interactive=True), None, stop_button)
68
 
69
  with gr.Row():
70
+ output = [gr.Image(label=m, height=640, width=1024) for m in default_models] # Default 16:10
71
  current_models = [gr.Textbox(m, visible=False) for m in default_models]
72
 
73
  model_choice.change(update_imgbox, [model_choice, img_height, img_width], output)
 
78
  for m, o in zip(current_models, output):
79
  gen_event = gen_button.click(
80
  gen_fn,
81
+ [m, txt_input, img_height, img_width, use_ratio],
82
  o,
83
  queue=False
84
  )
 
88
  txt_input2 = gr.Textbox(label='Prompt text')
89
 
90
  with gr.Row():
91
+ img_width2 = gr.Slider(100, 1200, value=1024, step=2, label='Image Width (px)')
92
+ img_height2 = gr.Slider(100, 1200, value=640, step=2, label='Image Height (px)')
93
+ use_ratio2 = gr.Checkbox(label='Use 16:10 ratio', value=True)
94
+
95
+ # Höhe aktualisieren wenn Ratio aktiviert
96
+ img_width2.change(
97
+ lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
98
+ [img_width2, use_ratio2],
99
+ img_height2
100
+ )
101
+ use_ratio2.change(
102
+ lambda w, r: gr.update(value=int(w * 10 / 16)) if r else gr.update(),
103
+ [img_width2, use_ratio2],
104
+ img_height2
105
+ )
106
 
107
  max_images = 16
108
  num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
 
112
  gen_button2.click(lambda s: gr.update(interactive=True), None, stop_button2)
113
 
114
  with gr.Row():
115
+ output2 = [gr.Image(label='', height=640, width=1024) for _ in range(max_images)]
116
 
117
  for i, o in enumerate(output2):
118
  img_i = gr.Number(i, visible=False)
 
120
  img_height2.change(lambda h: gr.update(height=h), img_height2, o)
121
  img_width2.change(lambda w: gr.update(width=w), img_width2, o)
122
  gen_event2 = gen_button2.click(
123
+ lambda i, n, m, t, h, w, r: gen_fn(m, t, h, w, r) if (i < n) else None,
124
+ [img_i, num_images, model_choice2, txt_input2, img_height2, img_width2, use_ratio2],
125
  o
126
  )
127
  stop_button2.click(lambda s: gr.update(interactive=False), None, stop_button2, cancels=[gen_event2])