Spaces:
Running
on
Zero
Running
on
Zero
File size: 10,057 Bytes
40888ec e21fd5e 02ecbb6 40888ec 02ecbb6 4e4789a d094756 4e4789a 699ec16 e21fd5e 5338785 d094756 40888ec d094756 40888ec 36d936f 40888ec 189b26e 40888ec 8b7ed75 40888ec 8b7ed75 40888ec 8b7ed75 40888ec 8b7ed75 40888ec 8b7ed75 40888ec 8b7ed75 40888ec 8b7ed75 40888ec d094756 40888ec d094756 40888ec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
import spaces
import gradio as gr
from PIL import Image
import os
import tempfile
import sys
import time
from accelerate.utils import set_seed
from huggingface_hub import snapshot_download
import subprocess
#os.system("pip install ./flash_attn-2.7.4.post1+cu12torch2.6cxx11abiFALSE-cp310-cp310-linux_x86_64.whl")
subprocess.check_call([sys.executable, "-m", "pip", "install", "./flash_attn-2.7.4.post1+cu12torch2.6cxx11abiFALSE-cp310-cp310-linux_x86_64.whl"])
def ensure_flash_attn():
try:
import flash_attn
print("当前 flash-attn 已安装,版本:", flash_attn.__version__)
except ImportError:
print("未安装 flash-attn")
ensure_flash_attn()
from inferencer import UniPicV2Inferencer
os.environ["HF_HUB_REQUEST_TIMEOUT"] = "60.0"
model_path = snapshot_download(repo_id="Skywork/UniPic2-Metaquery-9B")
qwen_vl_path = snapshot_download(repo_id="Qwen/Qwen2.5-VL-7B-Instruct")
inferencer = UniPicV2Inferencer(
model_path=model_path,
qwen_vl_path=qwen_vl_path,
quant="fp16"
)
#inferencer.pipeline = inferencer._init_pipeline()
TEMP_DIR = tempfile.mkdtemp()
print(f"Temporary directory created at: {TEMP_DIR}")
def save_temp_image(pil_img):
path = os.path.join(TEMP_DIR, f"temp_{int(time.time())}.png")
pil_img.save(path, format="PNG")
return path
def handle_image_upload(file, history):
if file is None:
return None, history
file_path = file.name if hasattr(file, "name") else file
pil_img = Image.open(file_path)
saved_path = save_temp_image(pil_img)
return saved_path, history + [((saved_path,), None)]
def clear_all():
for file in os.listdir(TEMP_DIR):
path = os.path.join(TEMP_DIR, file)
try:
if os.path.isfile(path):
os.remove(path)
except Exception as e:
print(f"Failed to delete temp file: {path}, error: {e}")
return [], None, "Language Output"
def extract_assistant_reply(full_text):
if "assistant" in full_text:
parts = full_text.strip().split("assistant")
return parts[-1].lstrip(":").strip()
return full_text.replace("<|im_end|>", "").strip()
def on_submit(history, user_msg, img_path, mode, infer_steps, cfg_scale, model_version='sd3.5-512', seed=42):
user_msg = user_msg.strip()
updated_history = history + [(user_msg, None)]
edit_tip = "✅ You can continue editing this image by switching the mode to 'Edit Image' and entering your instruction. "
# seed = int(seed)
# set_seed(seed) # 设置随机种子以确保可重复性
try:
if mode == "Understand Image":
if img_path is None:
updated_history.append([None, "⚠️ Please upload or generate an image first."])
return updated_history, "", img_path
raw_output = (
inferencer.understand_image(Image.open(img_path), user_msg)
if img_path
else inferencer.query_text(user_msg)
)
clean_output = raw_output
return (
updated_history + [(None, clean_output)],
"",
img_path,
) # 保持 img_path 不变
if mode == "Generate Image":
if not user_msg:
return (
updated_history
+ [(None, "⚠️ Please enter your message for generating.")],
"",
img_path,
)
imgs = inferencer.generate_image(user_msg, num_inference_steps=infer_steps, seed=seed, guidance_scale=cfg_scale)
path = save_temp_image(imgs[0])
return (
updated_history
+ [
(None, (path,)),
(
None,
"✅ You can continue editing this image by switching the mode to 'Edit Image' and entering your instruction. ",
),
],
"",
path,
) # 更新 img_state
elif mode == "Edit Image":
if img_path is None:
return (
updated_history
+ [(None, "⚠️ Please upload or generate an image first.")],
"",
img_path,
)
if not user_msg:
return (
updated_history
+ [(None, "⚠️ Please enter your message for editing.")],
"",
img_path,
)
edited_img = inferencer.edit_image(Image.open(img_path), user_msg, num_inference_steps=infer_steps, seed=seed, guidance_scale=cfg_scale)[0]
path = save_temp_image(edited_img)
return (
updated_history
+ [
(None, (path,)),
(
None,
"✅ You can continue editing this image by entering your instruction. ",
),
],
"",
path,
) # 更新 img_state
except Exception as e:
return updated_history + [(None, f"⚠️ Failed to process: {e}")], "", img_path
# 定义CSS样式(兼容旧版 Gradio)
CSS = """
/* 整体布局 */
.gradio-container {
display: flex !important;
flex-direction: column;
height: 100vh;
margin: 0;
padding: 0;
}
/* 让 tab 自适应填满剩余高度 */
.gr-tabs {
flex: 1 1 auto;
display: flex;
flex-direction: column;
}
/* 聊天 tab 主体 */
#tab_item_4 {
display: flex;
flex-direction: column;
flex: 1 1 auto;
overflow: hidden;
padding: 8px;
}
/* Chatbot 区域 */
#chatbot1 {
flex: 0 0 55vh !important;
overflow-y: auto !important;
border: 1px solid #ddd;
border-radius: 8px;
padding: 12px;
margin-bottom: 8px;
}
#chatbot1 img {
max-width: 80vw !important;
height: auto !important;
border-radius: 4px;
}
/* 控制面板外框 */
.control-panel {
border: 1px solid #ddd;
border-radius: 8px;
padding: 8px;
margin-bottom: 8px;
}
/* 控制行:三列不换行 */
.control-row {
display: flex;
align-items: stretch;
gap: 8px;
flex-wrap: nowrap; /* 不换行 */
}
/* 单个控件卡片样式 */
.control-box {
border: 1px solid #ccc;
border-radius: 8px;
padding: 8px;
background: #f9f9f9;
flex: 1;
min-width: 150px;
box-sizing: border-box;
}
/* 强制旧版 Radio 横向排列 */
.control-box .wrap {
display: flex !important;
flex-direction: row !important;
gap: 8px !important;
}
/* 输入区 */
.input-row {
flex: 0 0 auto;
display: flex;
align-items: center;
padding: 8px;
border-top: 1px solid #eee;
background: #fafafa;
}
.textbox-col { flex: 1; }
.upload-col, .clear-col { flex: 0 0 120px; }
.gr-text-input {
width: 100% !important;
border-radius: 18px !important;
padding: 8px 16px !important;
border: 1px solid #ddd !important;
font-size: 16px !important;
}
"""
with gr.Blocks(css=CSS) as demo:
img_state = gr.State(value=None)
with gr.Tabs():
with gr.Tab("Skywork UniPic2-Metaquery", elem_id="tab_item_4"):
chatbot = gr.Chatbot(
elem_id="chatbot1",
show_label=False,
avatar_images=("user.png", "ai.png"),
)
# 控制区域
with gr.Column(elem_classes="control-panel"):
with gr.Row(elem_classes="control-row"):
with gr.Column(elem_classes="control-box", scale=2, min_width=200):
mode_selector = gr.Radio(
choices=["Generate Image", "Edit Image", "Understand Image"],
value="Generate Image",
label="Mode",
interactive=True
)
with gr.Column(elem_classes="control-box", scale=1, min_width=150):
infer_steps = gr.Slider(
label="Sample Steps",
minimum=1,
maximum=200,
value=50,
step=1,
interactive=True,
)
with gr.Column(elem_classes="control-box", scale=1, min_width=150):
cfg_scale = gr.Slider(
label="CFG Scale",
minimum=1.0,
maximum=16.0,
value=3.5,
step=0.5,
interactive=True,
)
# 输入区域
with gr.Row(elem_classes="input-row"):
with gr.Column(elem_classes="textbox-col"):
user_input = gr.Textbox(
placeholder="Type your message here...",
label="prompt",
lines=1,
)
with gr.Column(elem_classes="upload-col"):
image_input = gr.UploadButton(
"📷 Upload Image",
file_types=["image"],
file_count="single",
type="filepath",
)
with gr.Column(elem_classes="clear-col"):
clear_btn = gr.Button("🧹 Clear History")
# 交互绑定
user_input.submit(
on_submit,
inputs=[chatbot, user_input, img_state, mode_selector, infer_steps, cfg_scale],
outputs=[chatbot, user_input, img_state],
)
image_input.upload(
handle_image_upload, inputs=[image_input, chatbot], outputs=[img_state, chatbot]
)
clear_btn.click(clear_all, outputs=[chatbot, img_state, mode_selector])
if __name__ == "__main__":
demo.launch()
# demo.launch(server_name="0.0.0.0", debug=True, server_port=8004)
|