Deadmon commited on
Commit
e10f0a4
1 Parent(s): 75a7f7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
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 # Decorate the function for ZeroGPU compatibility
59
  def generate_image(prompt,
60
  width,
61
  height,
62
  guidance,
63
  seed):
64
- if seed > -1:
65
- generator = torch.Generator('cpu').manual_seed(seed)
66
- else:
67
- generator = None
68
- messages = [{'role': 'User', 'content': prompt},
69
- {'role': 'Assistant', 'content': ''}]
70
- text = processor.apply_sft_template_for_multi_turn_prompts(conversations=messages,
71
- sft_format=processor.sft_format,
72
- system_prompt='')
73
- text = text + processor.image_start_tag
74
- input_ids = torch.LongTensor(processor.tokenizer.encode(text))
75
- output, patches = generate(input_ids,
76
- width // 16 * 16,
77
- height // 16 * 16,
78
- cfg_weight=guidance)
79
- images = unpack(patches,
80
- width // 16 * 16,
81
- height // 16 * 16)
 
 
 
 
82
 
83
- return Image.fromarray(images[0]), seed, ''
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():