Uthar commited on
Commit
0b45526
1 Parent(s): 2c1071a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -49
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from all_models import models
3
- # from externalmod import gr_Interface_load, save_image, randomize_seed
4
  import asyncio
5
  import os
6
  from threading import RLock
@@ -11,52 +11,7 @@ negPreSetPrompt = "[deformed | disfigured], poorly drawn, [bad : wrong] anatomy,
11
 
12
  lock = RLock()
13
 
14
- HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None
15
-
16
- num_models = 12
17
- max_images = 12
18
- inference_timeout = 400
19
- default_models = models[:num_models]
20
- MAX_SEED = 2**32-1
21
-
22
- def gr_Interface_load(
23
- name: str,
24
- src: str | None = None,
25
- hf_token: str | None = None,
26
- alias: str | None = None,
27
- **kwargs, # ignore
28
- ) -> Blocks:
29
- try:
30
- return load_blocks_from_repo(name, src, hf_token, alias)
31
- except Exception as e:
32
- print(e)
33
- return gradio.Interface(lambda: None, ['text'], ['image'])
34
-
35
-
36
- def save_image(image, savefile, modelname, prompt, nprompt, height=0, width=0, steps=0, cfg=0, seed=-1):
37
- from PIL import Image, PngImagePlugin
38
- import json
39
- try:
40
- metadata = {"prompt": prompt, "negative_prompt": nprompt, "Model": {"Model": modelname.split("/")[-1]}}
41
- if steps > 0: metadata["num_inference_steps"] = steps
42
- if cfg > 0: metadata["guidance_scale"] = cfg
43
- if seed != -1: metadata["seed"] = seed
44
- if width > 0 and height > 0: metadata["resolution"] = f"{width} x {height}"
45
- metadata_str = json.dumps(metadata)
46
- info = PngImagePlugin.PngInfo()
47
- info.add_text("metadata", metadata_str)
48
- image.save(savefile, "PNG", pnginfo=info)
49
- return str(Path(savefile).resolve())
50
- except Exception as e:
51
- print(f"Failed to save image file: {e}")
52
- raise Exception(f"Failed to save image file:") from e
53
-
54
- def randomize_seed():
55
- from random import seed, randint
56
- MAX_SEED = 2**32-1
57
- seed()
58
- rseed = randint(0, MAX_SEED)
59
- return rseed
60
 
61
  def get_current_time():
62
  now = datetime.now()
@@ -76,20 +31,31 @@ def load_fn(models):
76
  m = gr.Interface(lambda: None, ['text'], ['image'])
77
  models_load.update({model: m})
78
 
 
79
  load_fn(models)
80
 
 
 
 
 
 
 
 
81
  def extend_choices(choices):
82
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
83
 
 
84
  def update_imgbox(choices):
85
  choices_plus = extend_choices(choices[:num_models])
86
  return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
87
 
 
88
  def random_choices():
89
  import random
90
  random.seed()
91
  return random.choices(models, k=num_models)
92
 
 
93
  async def infer(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1, timeout=inference_timeout):
94
  kwargs = {}
95
  if height > 0: kwargs["height"] = height
@@ -139,6 +105,7 @@ def gen_fn(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, see
139
  loop.close()
140
  return result
141
 
 
142
  def add_gallery(image, model_str, gallery):
143
  if gallery is None: gallery = []
144
  with lock:
@@ -150,7 +117,6 @@ js="""
150
  """
151
 
152
  with gr.Blocks(fill_width=True, head=js) as demo:
153
- gr.HTML("")
154
  with gr.Tab(str(num_models) + ' Models'):
155
  with gr.Column(scale=2):
156
  with gr.Group():
@@ -213,7 +179,7 @@ with gr.Blocks(fill_width=True, head=js) as demo:
213
  seed2 = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
214
  seed_rand2 = gr.Button("Randomize Seed", size="sm", variant="secondary")
215
  seed_rand2.click(randomize_seed, None, [seed2], queue=False)
216
- num_images = gr.Slider(1, max_images, value=max_images/2, step=1, label='Number of images')
217
  with gr.Row():
218
  gen_button2 = gr.Button('Let the machine halucinate', variant='primary', scale=2)
219
 
 
1
  import gradio as gr
2
  from all_models import models
3
+ from externalmod import gr_Interface_load, save_image, randomize_seed
4
  import asyncio
5
  import os
6
  from threading import RLock
 
11
 
12
  lock = RLock()
13
 
14
+ HF_TOKEN = os.environ.get("HF_TOKEN") if os.environ.get("HF_TOKEN") else None # If private or gated models aren't used, ENV setting is unnecessary.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  def get_current_time():
17
  now = datetime.now()
 
31
  m = gr.Interface(lambda: None, ['text'], ['image'])
32
  models_load.update({model: m})
33
 
34
+
35
  load_fn(models)
36
 
37
+ num_models = 12
38
+ max_images = 12
39
+ inference_timeout = 400
40
+ default_models = models[:num_models]
41
+ MAX_SEED = 2**32-1
42
+
43
+
44
  def extend_choices(choices):
45
  return choices[:num_models] + (num_models - len(choices[:num_models])) * ['NA']
46
 
47
+
48
  def update_imgbox(choices):
49
  choices_plus = extend_choices(choices[:num_models])
50
  return [gr.Image(None, label=m, visible=(m!='NA')) for m in choices_plus]
51
 
52
+
53
  def random_choices():
54
  import random
55
  random.seed()
56
  return random.choices(models, k=num_models)
57
 
58
+
59
  async def infer(model_str, prompt, nprompt="", height=0, width=0, steps=0, cfg=0, seed=-1, timeout=inference_timeout):
60
  kwargs = {}
61
  if height > 0: kwargs["height"] = height
 
105
  loop.close()
106
  return result
107
 
108
+
109
  def add_gallery(image, model_str, gallery):
110
  if gallery is None: gallery = []
111
  with lock:
 
117
  """
118
 
119
  with gr.Blocks(fill_width=True, head=js) as demo:
 
120
  with gr.Tab(str(num_models) + ' Models'):
121
  with gr.Column(scale=2):
122
  with gr.Group():
 
179
  seed2 = gr.Slider(label="Seed", info="Randomize Seed if -1.", minimum=-1, maximum=MAX_SEED, step=1, value=-1)
180
  seed_rand2 = gr.Button("Randomize Seed", size="sm", variant="secondary")
181
  seed_rand2.click(randomize_seed, None, [seed2], queue=False)
182
+ num_images = gr.Slider(1, max_images, value=max_images, step=1, label='Number of images')
183
  with gr.Row():
184
  gen_button2 = gr.Button('Let the machine halucinate', variant='primary', scale=2)
185