Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,835 Bytes
42429d0 aa0a0cc 587c0d6 14ebc1a 42429d0 2114407 42429d0 da489db 42429d0 0711bac 42429d0 39ba970 42429d0 7db5491 2114407 0711bac 7db5491 42429d0 7c9df88 42429d0 |
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 |
import gradio as gr
from PIL import Image
import os
import tempfile
import sys
import time
from inferencer import Inferencer
from accelerate.utils import set_seed
from huggingface_hub import snapshot_download
model_dir = snapshot_download(repo_id="Skywork/Skywork-UniPic-1.5B")
model_path = os.path.join(model_dir,"pytorch_model.bin")
ckpt_name = "UniPic"
inferencer = Inferencer(
config_file="qwen2_5_1_5b_kl16_mar_h.py",
model_path=model_path,
image_size=1024,
#cfg_prompt="Generate an image.",
)
TEMP_DIR = tempfile.mkdtemp()
print(f"Temporary directory created at: {TEMP_DIR}")
def save_temp_image(pil_img):
# 只支持512——>1024的编辑
# img_resized = pil_img.resize((512, 512))
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, "Understand Image"
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, grid_size=1):
# 把 history 中的 tuples 全部换成 lists
updated_history = [list(item) for item in history]
user_msg = user_msg.strip()
updated_history.append([user_msg, None])
# set_seed(42)
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 = (
inferencer.query_image(Image.open(img_path), user_msg)
if img_path else inferencer.query_text(user_msg)
)
reply = extract_assistant_reply(raw)
updated_history.append([None, reply])
return updated_history, "", img_path
elif mode == "Generate Image":
if not user_msg:
updated_history.append([None, "⚠️ Please enter a prompt."])
return updated_history, "", img_path
imgs = inferencer.gen_image(
raw_prompt=user_msg,
images_to_generate=grid_size**2,
cfg=3.0,
num_iter=48,
cfg_schedule="constant",
temperature=1.0,
)
paths = [save_temp_image(img) for img in imgs]
# 多图必须是列表格式
updated_history.append([None, paths])
return updated_history, "", paths[-1]
elif mode == "Edit Image":
if img_path is None:
updated_history.append([None, "⚠️ Please upload or generate an image first."])
return updated_history, "", img_path
if not user_msg:
updated_history.append([None, "⚠️ Please enter an edit instruction."])
return updated_history, "", img_path
img = Image.open(img_path)
imgs = inferencer.edit_image(
source_image=img,
prompt=user_msg,
cfg=3.0,
cfg_prompt="repeat this image.",
cfg_schedule="constant",
temperature=0.85,
grid_size=grid_size,
num_iter=48,
)
paths = [save_temp_image(img) for img in imgs]
updated_history.append([None, paths])
return updated_history, "", paths[-1]
except Exception as e:
updated_history.append([None, f"⚠️ Failed to process: {e}"])
return updated_history, "", img_path
CSS = """
/* 整体布局:上下两块 */
.gradio-container {
display: flex !important;
flex-direction: column;
height: 100vh;
margin: 0;
padding: 0;
}
.gr-tabs { /* ✅ 新增:确保 tab 能继承高度 */
flex: 1 1 auto;
display: flex;
flex-direction: column;
}
/* 聊天 tab */
#tab_item_4, #tab_item_5 {
display: flex;
flex-direction: column;
flex: 1 1 auto;
overflow: hidden; /* 防止出现双滚动条 */
padding: 8px;
}
/* Chatbot 撑满 */
#chatbot1, #chatbot2{
flex-grow: 1 !important;
max-height: 66vh !important; /* 限制聊天框最大高度为屏幕的2/3 */
overflow-y: auto !important; /* 当内容溢出时显示滚动条 */
border: 1px solid #ddd;
border-radius: 8px;
padding: 12px;
margin-bottom: 8px;
}
/* 图片消息放大 */
#chatbot1 img, #chatbot2 img {
max-width: 80vw !important;
height: auto !important;
border-radius: 4px;
}
/* 底部输入区:固定高度 */
.input-row {
flex: 0 0 auto;
display: flex;
align-items: center;
padding: 8px;
border-top: 1px solid #eee;
background: #fafafa;
}
/* 文本框和按钮排布 */
.input-row .textbox-col { flex: 5; }
.input-row .upload-col, .input-row .clear-col { flex: 1; margin-left: 8px; }
/* 文本框样式 */
.gr-text-input {
width: 100% !important;
border-radius: 18px !important;
padding: 8px 16px !important;
border: 1px solid #ddd !important;
font-size: 16px !important;
}
/* 按钮和上传组件样式 */
.gr-button, .gr-upload {
width: 100% !important;
border-radius: 18px !important;
padding: 8px 16px !important;
font-size: 16px !important;
}
"""
with gr.Blocks(css=CSS) as demo:
img_state = gr.State(value=None)
mode_state = gr.State(value="Understand Image")
with gr.Tabs():
with gr.Tab("Skywork UniPic Chatbot", elem_id="tab_item_4"):
chatbot = gr.Chatbot(
elem_id="chatbot1",
show_label=False,
avatar_images=(
"user.png",
"ai.png",
),
)
with gr.Row():
mode_selector = gr.Radio(
choices=["Generate Image","Edit Image","Understand Image"],
value="Generate Image",
label="Mode",
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...",
show_label=False,
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,
[chatbot, user_input, img_state, mode_selector],
[chatbot, user_input, img_state],
)
image_input.upload(
handle_image_upload, [image_input, chatbot], [img_state, chatbot]
)
clear_btn.click(clear_all, outputs=[chatbot, img_state, mode_selector])
# if __name__ == "__main__":
# demo.launch(server_name="0.0.0.0", share=True, debug=True, server_port=7689)
demo.launch() |