Spaces:
Runtime error
Runtime error
Combine interfaces
Browse files
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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|