yichenchenchen commited on
Commit
8b7ed75
·
verified ·
1 Parent(s): b6a4620

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -64
app.py CHANGED
@@ -23,7 +23,7 @@ ensure_flash_attn()
23
  from inferencer import UniPicV2Inferencer
24
 
25
 
26
- model_path = snapshot_download(repo_id="Skywork/UniPic2-Metaquery-GRPO-9B")
27
  qwen_vl_path = snapshot_download(repo_id="Qwen/Qwen2.5-VL-7B-Instruct-AWQ")
28
 
29
  inferencer = UniPicV2Inferencer(
@@ -76,79 +76,79 @@ def on_submit(history, user_msg, img_path, mode, infer_steps, cfg_scale, model_v
76
  # seed = int(seed)
77
  # set_seed(seed) # 设置随机种子以确保可重复性
78
 
79
- # try:
80
- if mode == "Understand Image":
81
- if img_path is None:
82
- updated_history.append([None, "⚠️ Please upload or generate an image first."])
83
- return updated_history, "", img_path
84
-
85
- raw_output = (
86
- inferencer.understand_image(Image.open(img_path), user_msg)
87
- if img_path
88
- else inferencer.query_text(user_msg)
89
- )
90
- clean_output = raw_output
91
- return (
92
- updated_history + [(None, clean_output)],
93
- "",
94
- img_path,
95
- ) # 保持 img_path 不变
96
-
97
- if mode == "Generate Image":
98
- if not user_msg:
99
  return (
100
- updated_history
101
- + [(None, "⚠️ Please enter your message for generating.")],
102
  "",
103
  img_path,
104
- )
105
- imgs = inferencer.generate_image(user_msg, num_inference_steps=infer_steps, seed=seed, guidance_scale=cfg_scale)
106
- path = save_temp_image(imgs[0])
107
- return (
108
- updated_history
109
- + [
110
- (None, (path,)),
111
- (
112
- None,
113
- "✅ You can continue editing this image by switching the mode to 'Edit Image' and entering your instruction. ",
114
- ),
115
- ],
116
- "",
117
- path,
118
- ) # 更新 img_state
119
-
120
- elif mode == "Edit Image":
121
- if img_path is None:
122
  return (
123
  updated_history
124
- + [(None, "⚠️ Please upload or generate an image first.")],
 
 
 
 
 
 
125
  "",
126
- img_path,
127
- )
128
- if not user_msg:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  return (
130
  updated_history
131
- + [(None, "⚠️ Please enter your message for editing.")],
 
 
 
 
 
 
132
  "",
133
- img_path,
134
- )
135
- edited_img = inferencer.edit_image(Image.open(img_path), user_msg, num_inference_steps=infer_steps, seed=seed, guidance_scale=cfg_scale)[0]
136
- path = save_temp_image(edited_img)
137
- return (
138
- updated_history
139
- + [
140
- (None, (path,)),
141
- (
142
- None,
143
- "✅ You can continue editing this image by entering your instruction. ",
144
- ),
145
- ],
146
- "",
147
- path,
148
- ) # 更新 img_state
149
 
150
- # except Exception as e:
151
- # return updated_history + [(None, f"⚠️ Failed to process: {e}")], "", img_path
152
 
153
 
154
  # 定义CSS样式(兼容旧版 Gradio)
 
23
  from inferencer import UniPicV2Inferencer
24
 
25
 
26
+ model_path = snapshot_download(repo_id="Skywork/UniPic2-Metaquery-9B")
27
  qwen_vl_path = snapshot_download(repo_id="Qwen/Qwen2.5-VL-7B-Instruct-AWQ")
28
 
29
  inferencer = UniPicV2Inferencer(
 
76
  # seed = int(seed)
77
  # set_seed(seed) # 设置随机种子以确保可重复性
78
 
79
+ try:
80
+ if mode == "Understand Image":
81
+ if img_path is None:
82
+ updated_history.append([None, "⚠️ Please upload or generate an image first."])
83
+ return updated_history, "", img_path
84
+
85
+ raw_output = (
86
+ inferencer.understand_image(Image.open(img_path), user_msg)
87
+ if img_path
88
+ else inferencer.query_text(user_msg)
89
+ )
90
+ clean_output = raw_output
 
 
 
 
 
 
 
 
91
  return (
92
+ updated_history + [(None, clean_output)],
 
93
  "",
94
  img_path,
95
+ ) # 保持 img_path 不变
96
+
97
+ if mode == "Generate Image":
98
+ if not user_msg:
99
+ return (
100
+ updated_history
101
+ + [(None, "⚠️ Please enter your message for generating.")],
102
+ "",
103
+ img_path,
104
+ )
105
+ imgs = inferencer.generate_image(user_msg, num_inference_steps=infer_steps, seed=seed, guidance_scale=cfg_scale)
106
+ path = save_temp_image(imgs[0])
 
 
 
 
 
 
107
  return (
108
  updated_history
109
+ + [
110
+ (None, (path,)),
111
+ (
112
+ None,
113
+ "✅ You can continue editing this image by switching the mode to 'Edit Image' and entering your instruction. ",
114
+ ),
115
+ ],
116
  "",
117
+ path,
118
+ ) # 更新 img_state
119
+
120
+ elif mode == "Edit Image":
121
+ if img_path is None:
122
+ return (
123
+ updated_history
124
+ + [(None, "⚠️ Please upload or generate an image first.")],
125
+ "",
126
+ img_path,
127
+ )
128
+ if not user_msg:
129
+ return (
130
+ updated_history
131
+ + [(None, "⚠️ Please enter your message for editing.")],
132
+ "",
133
+ img_path,
134
+ )
135
+ edited_img = inferencer.edit_image(Image.open(img_path), user_msg, num_inference_steps=infer_steps, seed=seed, guidance_scale=cfg_scale)[0]
136
+ path = save_temp_image(edited_img)
137
  return (
138
  updated_history
139
+ + [
140
+ (None, (path,)),
141
+ (
142
+ None,
143
+ "✅ You can continue editing this image by entering your instruction. ",
144
+ ),
145
+ ],
146
  "",
147
+ path,
148
+ ) # 更新 img_state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
+ except Exception as e:
151
+ return updated_history + [(None, f"⚠️ Failed to process: {e}")], "", img_path
152
 
153
 
154
  # 定义CSS样式(兼容旧版 Gradio)