John6666 commited on
Commit
e5fe549
1 Parent(s): 54c0632

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -15,7 +15,7 @@ def load_fn(models):
15
  m = gr_Interface_load(f'models/{model}')
16
  except Exception as error:
17
  print(error)
18
- m = gr.Interface(lambda txt: None, ['text'], ['image'])
19
  models_load.update({model: m})
20
 
21
 
@@ -24,7 +24,7 @@ load_fn(models)
24
 
25
  num_models = 6
26
  default_models = models[:num_models]
27
-
28
 
29
  def extend_choices(choices):
30
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
@@ -49,9 +49,10 @@ async def infer(model_str, prompt, timeout):
49
  await asyncio.sleep(0)
50
  try:
51
  result = await asyncio.wait_for(task, timeout=timeout)
52
- except Exception as e:
53
  print(e)
54
- task.cancel()
 
55
  result = None
56
  return result
57
 
@@ -61,9 +62,10 @@ def gen_fn(model_str, prompt):
61
  return None
62
  try:
63
  loop = asyncio.new_event_loop()
64
- result = loop.run_until_complete(infer(model_str, prompt, 60))
65
- except Exception as e:
66
  print(e)
 
67
  result = None
68
  finally:
69
  loop.close()
@@ -76,10 +78,11 @@ def gen_fn_gallery(model_str, prompt, gallery):
76
  yield gallery
77
  try:
78
  loop = asyncio.new_event_loop()
79
- result = loop.run_until_complete(infer(model_str, prompt, 60))
80
  if result: gallery.append(result)
81
- except Exception as e:
82
  print(e)
 
83
  finally:
84
  loop.close()
85
  yield gallery
@@ -94,7 +97,7 @@ with gr.Blocks(css=CSS) as demo:
94
  txt_input = gr.Textbox(label = 'Your prompt:', lines=4) #.style(container=False,min_width=1200)
95
  gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
96
  stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
97
- gen_button.click(lambda s: gr.update(interactive = True), None, stop_button)
98
  gr.HTML(
99
  """
100
  <div style="text-align: center; max-width: 1200px; margin: 0 auto;">
@@ -115,7 +118,7 @@ with gr.Blocks(css=CSS) as demo:
115
  for m, o in zip(current_models, output):
116
  #gen_event = gen_button.click(gen_fn, [m, txt_input], o)
117
  gen_event = gen_button.click(gen_fn_gallery, [m, txt_input, o], o)
118
- stop_button.click(lambda s: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
119
  with gr.Accordion('Model selection'):
120
  model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value = default_models, interactive = True)
121
  #model_choice.change(update_imgbox, model_choice, output)
 
15
  m = gr_Interface_load(f'models/{model}')
16
  except Exception as error:
17
  print(error)
18
+ m = gr.Interface(lambda: None, ['text'], ['image'])
19
  models_load.update({model: m})
20
 
21
 
 
24
 
25
  num_models = 6
26
  default_models = models[:num_models]
27
+ timeout = 60
28
 
29
  def extend_choices(choices):
30
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
 
49
  await asyncio.sleep(0)
50
  try:
51
  result = await asyncio.wait_for(task, timeout=timeout)
52
+ except (Exception, asyncio.TimeoutError) as e:
53
  print(e)
54
+ print(f"Task timed-out: {model_str}")
55
+ if not task.done(): task.cancel()
56
  result = None
57
  return result
58
 
 
62
  return None
63
  try:
64
  loop = asyncio.new_event_loop()
65
+ result = loop.run_until_complete(infer(model_str, prompt, timeout))
66
+ except (Exception, asyncio.CancelledError) as e:
67
  print(e)
68
+ print(f"Task aborted: {model_str}")
69
  result = None
70
  finally:
71
  loop.close()
 
78
  yield gallery
79
  try:
80
  loop = asyncio.new_event_loop()
81
+ result = loop.run_until_complete(infer(model_str, prompt, timeout))
82
  if result: gallery.append(result)
83
+ except (Exception, asyncio.CancelledError) as e:
84
  print(e)
85
+ print(f"Task aborted: {model_str}")
86
  finally:
87
  loop.close()
88
  yield gallery
 
97
  txt_input = gr.Textbox(label = 'Your prompt:', lines=4) #.style(container=False,min_width=1200)
98
  gen_button = gr.Button('Generate up to 6 images in up to 3 minutes total')
99
  stop_button = gr.Button('Stop', variant = 'secondary', interactive = False)
100
+ gen_button.click(lambda: gr.update(interactive = True), None, stop_button)
101
  gr.HTML(
102
  """
103
  <div style="text-align: center; max-width: 1200px; margin: 0 auto;">
 
118
  for m, o in zip(current_models, output):
119
  #gen_event = gen_button.click(gen_fn, [m, txt_input], o)
120
  gen_event = gen_button.click(gen_fn_gallery, [m, txt_input, o], o)
121
+ stop_button.click(lambda: gr.update(interactive = False), None, stop_button, cancels = [gen_event])
122
  with gr.Accordion('Model selection'):
123
  model_choice = gr.CheckboxGroup(models, label = f'Choose up to {num_models} different models from the 866 available!', value = default_models, interactive = True)
124
  #model_choice.change(update_imgbox, model_choice, output)