likunchang commited on
Commit
717846a
·
1 Parent(s): cdfd9c4
Files changed (1) hide show
  1. app.py +34 -52
app.py CHANGED
@@ -26,12 +26,13 @@ from modeling.qwen2 import Qwen2Tokenizer
26
 
27
  from huggingface_hub import snapshot_download
28
 
29
- model_path = "./model"
30
  repo_id = "ByteDance-Seed/BAGEL-7B-MoT"
31
- cache_dir = model_path + "/cache"
32
 
33
- snapshot_download(cache_dir=cache_dir,
34
- local_dir=model_path,
 
35
  repo_id=repo_id,
36
  local_dir_use_symlinks=False,
37
  resume_download=True,
@@ -39,6 +40,7 @@ snapshot_download(cache_dir=cache_dir,
39
  )
40
 
41
  # Model Initialization
 
42
 
43
  llm_config = Qwen2Config.from_json_file(os.path.join(model_path, "llm_config.json"))
44
  llm_config.qk_norm = True
@@ -173,10 +175,17 @@ def text_to_image(prompt, show_thinking=False, cfg_text_scale=4.0, cfg_interval=
173
  cfg_renorm_type=cfg_renorm_type,
174
  image_shapes=image_shapes,
175
  )
176
-
 
177
  # Call inferencer with or without think parameter based on user choice
178
- result = inferencer(text=prompt, think=show_thinking, **inference_hyper)
179
- return result["image"], result.get("text", None)
 
 
 
 
 
 
180
 
181
 
182
  # Image Understanding function with thinking option and hyperparameters
@@ -198,10 +207,15 @@ def image_understanding(image: Image.Image, prompt: str, show_thinking=False,
198
  max_think_token_n=max_new_tokens, # Set max_length
199
  )
200
 
 
201
  # Use show_thinking parameter to control thinking process
202
- result = inferencer(image=image, text=prompt, think=show_thinking,
203
- understanding_output=True, **inference_hyper)
204
- return result["text"]
 
 
 
 
205
 
206
 
207
  # Image Editing function with thinking option and hyperparameters
@@ -237,9 +251,14 @@ def edit_image(image: Image.Image, prompt: str, show_thinking=False, cfg_text_sc
237
  )
238
 
239
  # Include thinking parameter based on user choice
240
- result = inferencer(image=image, text=prompt, think=show_thinking, **inference_hyper)
241
- return result["image"], result.get("text", "")
 
 
 
 
242
 
 
243
 
244
  # Helper function to load example images
245
  def load_example_image(image_path):
@@ -321,22 +340,9 @@ with gr.Blocks() as demo:
321
  outputs=[thinking_output, thinking_params]
322
  )
323
 
324
- # Process function based on thinking option and hyperparameters
325
- def process_text_to_image(prompt, show_thinking, cfg_text_scale,
326
- cfg_interval, timestep_shift,
327
- num_timesteps, cfg_renorm_min, cfg_renorm_type,
328
- max_think_token_n, do_sample, text_temperature, seed, image_ratio):
329
- image, thinking = text_to_image(
330
- prompt, show_thinking, cfg_text_scale, cfg_interval,
331
- timestep_shift, num_timesteps,
332
- cfg_renorm_min, cfg_renorm_type,
333
- max_think_token_n, do_sample, text_temperature, seed, image_ratio
334
- )
335
- return image, thinking if thinking else ""
336
-
337
  gr.on(
338
  triggers=[gen_btn.click, txt_input.submit],
339
- fn=process_text_to_image,
340
  inputs=[
341
  txt_input, show_thinking, cfg_text_scale,
342
  cfg_interval, timestep_shift,
@@ -413,24 +419,9 @@ with gr.Blocks() as demo:
413
  outputs=[edit_thinking_output, edit_thinking_params]
414
  )
415
 
416
- # Process editing with thinking option and hyperparameters
417
- def process_edit_image(image, prompt, show_thinking, cfg_text_scale,
418
- cfg_img_scale, cfg_interval,
419
- timestep_shift, num_timesteps, cfg_renorm_min,
420
- cfg_renorm_type, max_think_token_n, do_sample,
421
- text_temperature, seed):
422
- edited_image, thinking = edit_image(
423
- image, prompt, show_thinking, cfg_text_scale, cfg_img_scale,
424
- cfg_interval, timestep_shift,
425
- num_timesteps, cfg_renorm_min, cfg_renorm_type,
426
- max_think_token_n, do_sample, text_temperature, seed
427
- )
428
-
429
- return edited_image, thinking if thinking else ""
430
-
431
  gr.on(
432
  triggers=[edit_btn.click, edit_prompt.submit],
433
- fn=process_edit_image,
434
  inputs=[
435
  edit_image_input, edit_prompt, edit_show_thinking,
436
  edit_cfg_text_scale, edit_cfg_img_scale, edit_cfg_interval,
@@ -467,18 +458,9 @@ with gr.Blocks() as demo:
467
 
468
  img_understand_btn = gr.Button("Submit", variant="primary")
469
 
470
- # Process understanding with thinking option and hyperparameters
471
- def process_understanding(image, prompt, show_thinking, do_sample,
472
- text_temperature, max_new_tokens):
473
- result = image_understanding(
474
- image, prompt, show_thinking, do_sample,
475
- text_temperature, max_new_tokens
476
- )
477
- return result
478
-
479
  gr.on(
480
  triggers=[img_understand_btn.click, understand_prompt.submit],
481
- fn=process_understanding,
482
  inputs=[
483
  img_input, understand_prompt, understand_show_thinking,
484
  understand_do_sample, understand_text_temperature, understand_max_new_tokens
 
26
 
27
  from huggingface_hub import snapshot_download
28
 
29
+ save_dir = "./model_weights"
30
  repo_id = "ByteDance-Seed/BAGEL-7B-MoT"
31
+ cache_dir = save_dir + "/cache"
32
 
33
+ snapshot_download(
34
+ cache_dir=cache_dir,
35
+ local_dir=save_dir,
36
  repo_id=repo_id,
37
  local_dir_use_symlinks=False,
38
  resume_download=True,
 
40
  )
41
 
42
  # Model Initialization
43
+ model_path = save_dir
44
 
45
  llm_config = Qwen2Config.from_json_file(os.path.join(model_path, "llm_config.json"))
46
  llm_config.qk_norm = True
 
175
  cfg_renorm_type=cfg_renorm_type,
176
  image_shapes=image_shapes,
177
  )
178
+
179
+ result = {"text": "", "image": None}
180
  # Call inferencer with or without think parameter based on user choice
181
+ for i in inferencer(text=prompt, think=show_thinking, understanding_output=False, **inference_hyper):
182
+ print(type(i))
183
+ if type(i) == str:
184
+ result["text"] += i
185
+ else:
186
+ result["image"] = i
187
+
188
+ yield result["image"], result.get("text", None)
189
 
190
 
191
  # Image Understanding function with thinking option and hyperparameters
 
207
  max_think_token_n=max_new_tokens, # Set max_length
208
  )
209
 
210
+ result = {"text": "", "image": None}
211
  # Use show_thinking parameter to control thinking process
212
+ for i in inferencer(image=image, text=prompt, think=show_thinking,
213
+ understanding_output=True, **inference_hyper):
214
+ if type(i) == str:
215
+ result["text"] += i
216
+ else:
217
+ result["image"] = i
218
+ yield result["text"]
219
 
220
 
221
  # Image Editing function with thinking option and hyperparameters
 
251
  )
252
 
253
  # Include thinking parameter based on user choice
254
+ result = {"text": "", "image": None}
255
+ for i in inferencer(image=image, text=prompt, think=show_thinking, understanding_output=False, **inference_hyper):
256
+ if type(i) == str:
257
+ result["text"] += i
258
+ else:
259
+ result["image"] = i
260
 
261
+ yield result["image"], result.get("text", "")
262
 
263
  # Helper function to load example images
264
  def load_example_image(image_path):
 
340
  outputs=[thinking_output, thinking_params]
341
  )
342
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
  gr.on(
344
  triggers=[gen_btn.click, txt_input.submit],
345
+ fn=text_to_image,
346
  inputs=[
347
  txt_input, show_thinking, cfg_text_scale,
348
  cfg_interval, timestep_shift,
 
419
  outputs=[edit_thinking_output, edit_thinking_params]
420
  )
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  gr.on(
423
  triggers=[edit_btn.click, edit_prompt.submit],
424
+ fn=edit_image,
425
  inputs=[
426
  edit_image_input, edit_prompt, edit_show_thinking,
427
  edit_cfg_text_scale, edit_cfg_img_scale, edit_cfg_interval,
 
458
 
459
  img_understand_btn = gr.Button("Submit", variant="primary")
460
 
 
 
 
 
 
 
 
 
 
461
  gr.on(
462
  triggers=[img_understand_btn.click, understand_prompt.submit],
463
+ fn=image_understanding,
464
  inputs=[
465
  img_input, understand_prompt, understand_show_thinking,
466
  understand_do_sample, understand_text_temperature, understand_max_new_tokens