Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,9 @@ def generate(input_ids,
|
|
| 15 |
cfg_weight: float = 5,
|
| 16 |
image_token_num_per_image: int = 576,
|
| 17 |
patch_size: int = 16):
|
|
|
|
|
|
|
|
|
|
| 18 |
tokens = torch.zeros((parallel_size * 2, len(input_ids)), dtype=torch.int).to(cuda_device)
|
| 19 |
for i in range(parallel_size * 2):
|
| 20 |
tokens[i, :] = input_ids
|
|
@@ -55,32 +58,36 @@ def unpack(dec, width, height, parallel_size=1):
|
|
| 55 |
return visual_img
|
| 56 |
|
| 57 |
@torch.inference_mode()
|
| 58 |
-
@spaces.GPU #
|
| 59 |
def generate_image(prompt,
|
| 60 |
width,
|
| 61 |
height,
|
| 62 |
guidance,
|
| 63 |
seed):
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
|
| 85 |
with gr.Blocks() as demo:
|
| 86 |
with gr.Row():
|
|
|
|
| 15 |
cfg_weight: float = 5,
|
| 16 |
image_token_num_per_image: int = 576,
|
| 17 |
patch_size: int = 16):
|
| 18 |
+
# Clear CUDA cache before generating
|
| 19 |
+
torch.cuda.empty_cache()
|
| 20 |
+
|
| 21 |
tokens = torch.zeros((parallel_size * 2, len(input_ids)), dtype=torch.int).to(cuda_device)
|
| 22 |
for i in range(parallel_size * 2):
|
| 23 |
tokens[i, :] = input_ids
|
|
|
|
| 58 |
return visual_img
|
| 59 |
|
| 60 |
@torch.inference_mode()
|
| 61 |
+
@spaces.GPU(duration=120) # Specify a duration to avoid timeout
|
| 62 |
def generate_image(prompt,
|
| 63 |
width,
|
| 64 |
height,
|
| 65 |
guidance,
|
| 66 |
seed):
|
| 67 |
+
# Clear CUDA cache and avoid tracking gradients
|
| 68 |
+
torch.cuda.empty_cache()
|
| 69 |
+
|
| 70 |
+
with torch.no_grad():
|
| 71 |
+
if seed > -1:
|
| 72 |
+
generator = torch.Generator('cpu').manual_seed(seed)
|
| 73 |
+
else:
|
| 74 |
+
generator = None
|
| 75 |
+
messages = [{'role': 'User', 'content': prompt},
|
| 76 |
+
{'role': 'Assistant', 'content': ''}]
|
| 77 |
+
text = processor.apply_sft_template_for_multi_turn_prompts(conversations=messages,
|
| 78 |
+
sft_format=processor.sft_format,
|
| 79 |
+
system_prompt='')
|
| 80 |
+
text = text + processor.image_start_tag
|
| 81 |
+
input_ids = torch.LongTensor(processor.tokenizer.encode(text))
|
| 82 |
+
output, patches = generate(input_ids,
|
| 83 |
+
width // 16 * 16,
|
| 84 |
+
height // 16 * 16,
|
| 85 |
+
cfg_weight=guidance)
|
| 86 |
+
images = unpack(patches,
|
| 87 |
+
width // 16 * 16,
|
| 88 |
+
height // 16 * 16)
|
| 89 |
|
| 90 |
+
return Image.fromarray(images[0]), seed, ''
|
| 91 |
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
with gr.Row():
|