John6666 commited on
Commit
54c0632
β€’
1 Parent(s): 0d0ac30

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +33 -12
  2. externalmod.py +1 -1
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from random import randint
3
  from all_models import models
4
  from externalmod import gr_Interface_load
 
5
 
6
 
7
  def load_fn(models):
@@ -38,30 +39,50 @@ def update_imgbox_gallery(choices):
38
  choices_plus = extend_choices(choices[:num_models])
39
  return [gr.Gallery(None, label = m, visible = (m != 'NA')) for m in choices_plus]
40
 
41
-
42
- def gen_fn(model_str, prompt):
43
- if model_str == 'NA':
44
- return None
45
  noise = ""
46
  rand = randint(1, 500)
47
  for i in range(rand):
48
  noise += " "
49
- return models_load[model_str](f'{prompt} {noise}')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
 
52
  def gen_fn_gallery(model_str, prompt, gallery):
53
  if gallery is None: gallery = []
54
  if model_str == 'NA':
55
- return gallery
56
- noise = ""
57
- rand = randint(1, 500)
58
- for i in range(rand):
59
- noise += " "
60
  try:
61
- gallery.append(models_load[model_str](f'{prompt} {noise}'))
 
 
62
  except Exception as e:
63
  print(e)
64
- return gallery
 
 
65
 
66
 
67
  CSS="""
 
2
  from random import randint
3
  from all_models import models
4
  from externalmod import gr_Interface_load
5
+ import asyncio
6
 
7
 
8
  def load_fn(models):
 
39
  choices_plus = extend_choices(choices[:num_models])
40
  return [gr.Gallery(None, label = m, visible = (m != 'NA')) for m in choices_plus]
41
 
42
+
43
+ async def infer(model_str, prompt, timeout):
 
 
44
  noise = ""
45
  rand = randint(1, 500)
46
  for i in range(rand):
47
  noise += " "
48
+ task = asyncio.create_task(asyncio.to_thread(models_load[model_str], f'{prompt} {noise}'))
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
+
58
+
59
+ def gen_fn(model_str, prompt):
60
+ if model_str == 'NA':
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()
70
+ return result
71
 
72
 
73
  def gen_fn_gallery(model_str, prompt, gallery):
74
  if gallery is None: gallery = []
75
  if model_str == 'NA':
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
86
 
87
 
88
  CSS="""
externalmod.py CHANGED
@@ -115,7 +115,7 @@ def from_model(model_name: str, hf_token: str | None, alias: str | None, **kwarg
115
 
116
  headers["X-Wait-For-Model"] = "true"
117
  client = huggingface_hub.InferenceClient(
118
- model=model_name, headers=headers, token=hf_token, timeout=60,
119
  )
120
 
121
  # For tasks that are not yet supported by the InferenceClient
 
115
 
116
  headers["X-Wait-For-Model"] = "true"
117
  client = huggingface_hub.InferenceClient(
118
+ model=model_name, headers=headers, token=hf_token, timeout=120,
119
  )
120
 
121
  # For tasks that are not yet supported by the InferenceClient