mikegarts commited on
Commit
a267f88
·
1 Parent(s): ee4e6f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -8
app.py CHANGED
@@ -69,17 +69,20 @@ def on_change_event(app_state):
69
  step = app_state['step']
70
  label = f'Reconstructed image from the latent state at step {step}. It will get better :)'
71
  print(f'Updating the image:! {app_state}')
72
- return gr.update(value=app_state['img_list'], label='intermediate steps')
 
 
73
  else:
74
- return gr.update(label='images list')
75
 
76
  with gr.Blocks() as demo:
77
 
78
  def generate_image(prompt, inference_steps, app_state):
79
  app_state['running'] = True
80
  app_state['img_list'] = []
 
81
  def callback(step, ts, latents):
82
- print (f'In Callback on {step} {ts} !')
83
  latents = 1 / 0.18215 * latents
84
  res = pipe.vae.decode(latents).sample
85
  res = (res / 2 + 0.5).clamp(0, 1)
@@ -88,18 +91,22 @@ with gr.Blocks() as demo:
88
  app_state['img'] = res
89
  app_state['step'] = step
90
  app_state['img_list'].append(res)
91
- print (f'In Callback on {app_state} Done!')
 
92
 
93
  prompt = prompt + ' masterpiece charcoal pencil art lord of the rings illustration'
94
- img = pipe(prompt, height=512, width=512, num_inference_steps=inference_steps, callback=callback, callback_steps=2)
95
  app_state['running'] = False
96
  app_state['img'] = None
 
97
  return gr.update(value=img.images[0], label='Generated image')
98
 
99
  app_state = gr.State({'img': None,
100
  'step':0,
101
  'running':False,
102
- 'img_list': []
 
 
103
  })
104
  title = gr.Markdown('## Lord of the rings app')
105
  description = gr.Markdown(f'#### A Lord of the rings inspired app that combines text and image generation.'
@@ -115,6 +122,8 @@ with gr.Blocks() as demo:
115
  img_description = gr.Markdown('Image generation takes some time'
116
  ' but here you can see what is generated from the latent state of the diffuser every few steps.'
117
  ' Usually there is a significant improvement around step 12 that yields a much better image')
 
 
118
  gallery = gr.Gallery()
119
  image = gr.Image(label='Illustration for your story', show_label=True)
120
 
@@ -131,8 +140,8 @@ with gr.Blocks() as demo:
131
  bt_make_image.click(fn=generate_image, inputs=[summary, inference_steps, app_state], outputs=image)
132
 
133
  eventslider = gr.Slider(visible=False)
134
- dep = demo.load(on_change_event, app_state, [gallery], every=10)
135
- eventslider.change(fn=on_change_event, inputs=[app_state], outputs=[gallery], every=10, cancels=[dep])
136
 
137
 
138
  if READ_TOKEN:
 
69
  step = app_state['step']
70
  label = f'Reconstructed image from the latent state at step {step}. It will get better :)'
71
  print(f'Updating the image:! {app_state}')
72
+ app_state['dots'] += 1
73
+ app_state['dots'] = app_state['dots'] % 10
74
+ return gr.update(value=app_state['img_list'], label='intermediate steps'), app_state['staus_msg'] + '.' * app_state['dots']
75
  else:
76
+ return gr.update(label='images list'), ''
77
 
78
  with gr.Blocks() as demo:
79
 
80
  def generate_image(prompt, inference_steps, app_state):
81
  app_state['running'] = True
82
  app_state['img_list'] = []
83
+ app_state['status_msg'] = 'Starting'
84
  def callback(step, ts, latents):
85
+ app_state['status_msg'] = f'Reconstructing an image from the latent state on step {step}'
86
  latents = 1 / 0.18215 * latents
87
  res = pipe.vae.decode(latents).sample
88
  res = (res / 2 + 0.5).clamp(0, 1)
 
91
  app_state['img'] = res
92
  app_state['step'] = step
93
  app_state['img_list'].append(res)
94
+ app_state['status_msg'] = f'Generating state {step}'
95
+ app_state['dots'] = 0
96
 
97
  prompt = prompt + ' masterpiece charcoal pencil art lord of the rings illustration'
98
+ img = pipe(prompt, height=512, width=512, num_inference_steps=inference_steps, callback=callback, callback_steps=1)
99
  app_state['running'] = False
100
  app_state['img'] = None
101
+ app_state['status_msg'] = ''
102
  return gr.update(value=img.images[0], label='Generated image')
103
 
104
  app_state = gr.State({'img': None,
105
  'step':0,
106
  'running':False,
107
+ 'status_msg': '',
108
+ 'img_list': [],
109
+ 'dots': 0
110
  })
111
  title = gr.Markdown('## Lord of the rings app')
112
  description = gr.Markdown(f'#### A Lord of the rings inspired app that combines text and image generation.'
 
122
  img_description = gr.Markdown('Image generation takes some time'
123
  ' but here you can see what is generated from the latent state of the diffuser every few steps.'
124
  ' Usually there is a significant improvement around step 12 that yields a much better image')
125
+ status_msg = gr.Markdown(label='')
126
+
127
  gallery = gr.Gallery()
128
  image = gr.Image(label='Illustration for your story', show_label=True)
129
 
 
140
  bt_make_image.click(fn=generate_image, inputs=[summary, inference_steps, app_state], outputs=image)
141
 
142
  eventslider = gr.Slider(visible=False)
143
+ dep = demo.load(on_change_event, app_state, [gallery, status_msg], every=10)
144
+ eventslider.change(fn=on_change_event, inputs=[app_state], outputs=[gallery, status_msg], every=10, cancels=[dep])
145
 
146
 
147
  if READ_TOKEN: