gstaff commited on
Commit
c63db98
·
1 Parent(s): bba00f4

Combine interfaces

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -42,6 +42,7 @@ class DropOutput(Callback):
42
 
43
 
44
  # initialize only once
 
45
  model = MinDalle(
46
  models_root='./pretrained',
47
  dtype=float32,
@@ -52,7 +53,9 @@ model = MinDalle(
52
 
53
 
54
  def gen_image(prompt):
55
- torch.set_grad_enabled(False) # Hugging Space faces seems to run out of memory if grads are not disabled
 
 
56
  print(f'RUNNING gen_image with pronpt: {prompt}')
57
  images = model.generate_images(
58
  text=prompt,
@@ -70,24 +73,30 @@ def gen_image(prompt):
70
  return Image.fromarray(images[0])
71
 
72
 
73
- # gpu = False
74
- # # init only once
75
- # learner = load_learner('export.pkl',
76
- # cpu=not gpu) # cpu=False uses GPU; make sure installed torch is GPU e.g. `pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116`
77
- #
78
- #
79
- # def run_model(name):
80
- # prompt = f"Name: {name}\r\n"
81
- # prompt_ids = tokenizer.encode(prompt)
82
- # if gpu:
83
- # inp = tensor(prompt_ids)[None].cuda() # Use .cuda() for torch GPU
84
- # else:
85
- # inp = tensor(prompt_ids)[None]
86
- # preds = learner.model.generate(inp, max_length=1024, num_beams=5, temperature=1.5, do_sample=True)
87
- # result = tokenizer.decode(preds[0].cpu().numpy())
88
- # result = result.split('###')[0].replace(r'\r\n', '\n')
89
- # return result
90
-
91
-
92
- iface = gr.Interface(fn=gen_image, inputs="text", outputs="pil")
 
 
 
 
 
 
93
  iface.launch()
 
42
 
43
 
44
  # initialize only once
45
+ # Takes about 2 minutes (126 seconds) to generate an image in Huggingface spaces on CPU
46
  model = MinDalle(
47
  models_root='./pretrained',
48
  dtype=float32,
 
53
 
54
 
55
  def gen_image(prompt):
56
+ # See https://huggingface.co/spaces/pootow/min-dalle/blob/main/app.py
57
+ # Hugging Space faces seems to run out of memory if grads are not disabled
58
+ torch.set_grad_enabled(False)
59
  print(f'RUNNING gen_image with pronpt: {prompt}')
60
  images = model.generate_images(
61
  text=prompt,
 
73
  return Image.fromarray(images[0])
74
 
75
 
76
+ gpu = False
77
+ # init only once
78
+ learner = load_learner('export.pkl',
79
+ cpu=not gpu) # cpu=False uses GPU; make sure installed torch is GPU e.g. `pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116`
80
+
81
+
82
+ def run_model(name):
83
+ prompt = f"Name: {name}\r\n"
84
+ prompt_ids = tokenizer.encode(prompt)
85
+ if gpu:
86
+ inp = tensor(prompt_ids)[None].cuda() # Use .cuda() for torch GPU
87
+ else:
88
+ inp = tensor(prompt_ids)[None]
89
+ preds = learner.model.generate(inp, max_length=1024, num_beams=5, temperature=1.5, do_sample=True)
90
+ result = tokenizer.decode(preds[0].cpu().numpy())
91
+ result = result.split('###')[0].replace(r'\r\n', '\n')
92
+ return result
93
+
94
+
95
+ def run(name):
96
+ text = run_model(name)
97
+ pil = gen_image('smiling dog')
98
+ return text, pil
99
+
100
+
101
+ iface = gr.Interface(fn=run, inputs="text", outputs=["text", "pil"])
102
  iface.launch()