Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,84 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
"""
|
| 4 |
-
Ovis-U1-3B
|
| 5 |
-
|
| 6 |
-
ไพ่ต๏ผPython 3.10+ใgradio 4.*, torch 2.*ใtransformers 4.41.*
|
| 7 |
"""
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
import os
|
|
|
|
|
|
|
| 10 |
import subprocess
|
| 11 |
import random
|
| 12 |
import numpy as np
|
| 13 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
from PIL import Image
|
| 15 |
import gradio as gr
|
| 16 |
import spaces
|
| 17 |
from transformers import AutoModelForCausalLM
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
subprocess.run(
|
| 24 |
-
"pip install flash-attn==2.6.3 --no-build-isolation",
|
| 25 |
-
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
| 26 |
-
shell=True,
|
| 27 |
-
check=True,
|
| 28 |
-
)
|
| 29 |
-
except subprocess.CalledProcessError:
|
| 30 |
-
print("[INFO] flash-attn ๅฎ่ฃ
ๅคฑ่ดฅ๏ผCPU ็ฏๅขๅฏๅฟฝ็ฅ๏ผ")
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
MAX_SEED = 10_000
|
| 36 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 37 |
-
DTYPE = torch.bfloat16 if DEVICE == "cuda" else torch.float32 # CPU ้ป่ฎค็จ fp32
|
| 38 |
|
| 39 |
def set_global_seed(seed: int = 42) -> None:
|
| 40 |
"""็ปไธ่ฎพ็ฝฎ้ๆบ็งๅญ๏ผCPU / CUDA ่ช้ๅบ๏ผ"""
|
|
@@ -45,37 +89,29 @@ def set_global_seed(seed: int = 42) -> None:
|
|
| 45 |
torch.cuda.manual_seed_all(seed)
|
| 46 |
|
| 47 |
def randomize_seed_fn(seed: int, randomize: bool) -> int:
|
| 48 |
-
"""
|
| 49 |
return random.randint(0, MAX_SEED) if randomize else seed
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
# ๅ ่ฝฝๆจกๅ
|
| 53 |
-
#
|
| 54 |
-
HF_TOKEN
|
| 55 |
-
|
| 56 |
|
| 57 |
-
print(f"[INFO] Loading {
|
| 58 |
model = AutoModelForCausalLM.from_pretrained(
|
| 59 |
-
|
| 60 |
torch_dtype=DTYPE,
|
| 61 |
-
low_cpu_mem_usage=True,
|
| 62 |
-
device_map="auto",
|
| 63 |
token=HF_TOKEN,
|
| 64 |
trust_remote_code=True
|
| 65 |
-
).eval()
|
| 66 |
-
|
| 67 |
print("[INFO] Model ready!")
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
#
|
| 71 |
-
#
|
| 72 |
-
from test_img_edit import pipe_img_edit
|
| 73 |
-
from test_img_to_txt import pipe_txt_gen
|
| 74 |
-
from test_txt_to_img import pipe_t2i
|
| 75 |
-
|
| 76 |
-
# -------------------------------------------------------------------------
|
| 77 |
-
# ๆจ็ๅฐ่ฃ
๏ผๅ่ฟ่กๅจ DEVICE๏ผ
|
| 78 |
-
# -------------------------------------------------------------------------
|
| 79 |
def process_txt_to_img(prompt: str, height: int, width: int, steps: int,
|
| 80 |
final_seed: int, guidance_scale: float,
|
| 81 |
progress: gr.Progress = gr.Progress(track_tqdm=True)) -> list[Image.Image]:
|
|
@@ -94,23 +130,24 @@ def process_img_txt_to_img(prompt: str, img: Image.Image, steps: int,
|
|
| 94 |
return pipe_img_edit(model, img, prompt, steps,
|
| 95 |
txt_cfg, img_cfg, seed=final_seed)
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
# Gradio UI
|
| 99 |
-
#
|
| 100 |
-
with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
| 101 |
-
gr.Markdown("# Ovis-U1-3B\n
|
| 102 |
|
| 103 |
with gr.Row():
|
|
|
|
| 104 |
with gr.Column():
|
| 105 |
with gr.Tabs():
|
| 106 |
-
#
|
| 107 |
with gr.TabItem("Image + Text โ Image"):
|
| 108 |
edit_image_input = gr.Image(label="Input Image", type="pil")
|
| 109 |
with gr.Row():
|
| 110 |
edit_prompt_input = gr.Textbox(
|
| 111 |
label="Prompt",
|
| 112 |
show_label=False,
|
| 113 |
-
placeholder="Describe the editing instruction
|
| 114 |
container=False,
|
| 115 |
lines=1
|
| 116 |
)
|
|
@@ -130,8 +167,7 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 130 |
label="Steps", minimum=40, maximum=100, value=50, step=1
|
| 131 |
)
|
| 132 |
edit_seed_slider = gr.Slider(
|
| 133 |
-
label="Seed", minimum=0, maximum=MAX_SEED,
|
| 134 |
-
step=1, value=42
|
| 135 |
)
|
| 136 |
edit_randomize_checkbox = gr.Checkbox(
|
| 137 |
label="Randomize seed", value=False
|
|
@@ -148,15 +184,13 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 148 |
label="Image Editing Examples"
|
| 149 |
)
|
| 150 |
|
| 151 |
-
#
|
| 152 |
with gr.TabItem("Text โ Image"):
|
| 153 |
with gr.Row():
|
| 154 |
prompt_gen_input = gr.Textbox(
|
| 155 |
-
label="Prompt",
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
container=False,
|
| 159 |
-
lines=1
|
| 160 |
)
|
| 161 |
run_image_gen_btn = gr.Button("Run", scale=0)
|
| 162 |
|
|
@@ -171,15 +205,14 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 171 |
value=1024, step=32
|
| 172 |
)
|
| 173 |
guidance_slider = gr.Slider(
|
| 174 |
-
label="Guidance Scale",
|
| 175 |
-
|
| 176 |
)
|
| 177 |
num_steps_slider = gr.Slider(
|
| 178 |
label="Steps", minimum=40, maximum=100, value=50, step=1
|
| 179 |
)
|
| 180 |
seed_slider = gr.Slider(
|
| 181 |
-
label="Seed", minimum=0, maximum=MAX_SEED,
|
| 182 |
-
step=1, value=42
|
| 183 |
)
|
| 184 |
randomize_checkbox = gr.Checkbox(
|
| 185 |
label="Randomize seed", value=False
|
|
@@ -196,13 +229,13 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 196 |
label="Image Generation Examples"
|
| 197 |
)
|
| 198 |
|
| 199 |
-
#
|
| 200 |
with gr.TabItem("Image โ Text"):
|
| 201 |
image_understand_input = gr.Image(label="Input Image", type="pil")
|
| 202 |
with gr.Row():
|
| 203 |
prompt_understand_input = gr.Textbox(
|
| 204 |
label="Prompt", show_label=False,
|
| 205 |
-
placeholder="Describe the question about image
|
| 206 |
container=False, lines=1
|
| 207 |
)
|
| 208 |
run_image_understand_btn = gr.Button("Run", scale=0)
|
|
@@ -220,55 +253,38 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 220 |
|
| 221 |
clean_btn = gr.Button("Clear All Inputs / Outputs")
|
| 222 |
|
| 223 |
-
#
|
| 224 |
with gr.Column():
|
| 225 |
-
output_gallery = gr.Gallery(label="Generated Images",
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
interactive=False)
|
| 230 |
-
|
| 231 |
-
# ---------------------------------------------------------------------
|
| 232 |
-
# ไบไปถ็ปๅฎ
|
| 233 |
-
# ---------------------------------------------------------------------
|
| 234 |
def run_img_txt_to_img_tab(prompt, img, steps, seed, txt_cfg, img_cfg,
|
| 235 |
progress=gr.Progress(track_tqdm=True)):
|
| 236 |
if img is None:
|
| 237 |
-
return (
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
txt_cfg, img_cfg, progress)
|
| 243 |
-
return (
|
| 244 |
-
gr.update(value=imgs, visible=True),
|
| 245 |
-
gr.update(value="", visible=False)
|
| 246 |
-
)
|
| 247 |
|
| 248 |
def run_txt_to_img_tab(prompt, height, width, steps, seed, guidance,
|
| 249 |
progress=gr.Progress(track_tqdm=True)):
|
| 250 |
-
imgs = process_txt_to_img(prompt, height, width, steps, seed,
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
gr.update(value=imgs, visible=True),
|
| 254 |
-
gr.update(value="", visible=False)
|
| 255 |
-
)
|
| 256 |
|
| 257 |
def run_img_to_txt_tab(img, prompt,
|
| 258 |
progress=gr.Progress(track_tqdm=True)):
|
| 259 |
if img is None:
|
| 260 |
-
return (
|
| 261 |
-
|
| 262 |
-
gr.update(value="Please upload an image for understanding.", visible=True)
|
| 263 |
-
)
|
| 264 |
txt = process_img_to_txt(prompt, img, progress)
|
| 265 |
-
return (
|
| 266 |
-
|
| 267 |
-
gr.update(value=txt, visible=True)
|
| 268 |
-
)
|
| 269 |
|
| 270 |
def clean_all_fn():
|
| 271 |
-
"""
|
| 272 |
return (
|
| 273 |
# Tab 1
|
| 274 |
gr.update(value=None), gr.update(value=""),
|
|
@@ -287,7 +303,7 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 287 |
gr.update(value="", visible=False)
|
| 288 |
)
|
| 289 |
|
| 290 |
-
#
|
| 291 |
edit_inputs = [
|
| 292 |
edit_prompt_input, edit_image_input,
|
| 293 |
edit_num_steps_slider, edit_seed_slider,
|
|
@@ -306,7 +322,7 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 306 |
[output_gallery, output_text]
|
| 307 |
)
|
| 308 |
|
| 309 |
-
#
|
| 310 |
gen_inputs = [
|
| 311 |
prompt_gen_input, height_slider, width_slider,
|
| 312 |
num_steps_slider, seed_slider, guidance_slider
|
|
@@ -324,7 +340,7 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 324 |
[output_gallery, output_text]
|
| 325 |
)
|
| 326 |
|
| 327 |
-
#
|
| 328 |
understand_inputs = [image_understand_input, prompt_understand_input]
|
| 329 |
run_image_understand_btn.click(run_img_to_txt_tab,
|
| 330 |
understand_inputs,
|
|
@@ -333,7 +349,7 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 333 |
understand_inputs,
|
| 334 |
[output_gallery, output_text])
|
| 335 |
|
| 336 |
-
#
|
| 337 |
clean_btn.click(clean_all_fn, [], [
|
| 338 |
edit_image_input, edit_prompt_input, edit_img_guidance_slider,
|
| 339 |
edit_txt_guidance_slider, edit_num_steps_slider, edit_seed_slider,
|
|
@@ -343,8 +359,9 @@ with gr.Blocks(title="Ovis-U1-3B (CPU)") as demo:
|
|
| 343 |
output_gallery, output_text
|
| 344 |
])
|
| 345 |
|
| 346 |
-
#
|
| 347 |
-
# ๅฏๅจ
|
| 348 |
-
#
|
| 349 |
if __name__ == "__main__":
|
| 350 |
-
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
"""
|
| 4 |
+
Ovis-U1-3B ๅคๆจกๆ DEMO๏ผCPU / GPU ่ช้ๅบ็ๆฌ๏ผ
|
| 5 |
+
ไพ่ต๏ผPython 3.10+ใtorch 2.*ใtransformers 4.41.*ใgradio 4.*
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 9 |
+
# โ ๅจไปปไฝ transformers / flash_attn ๅฏผๅ
ฅไนๅๅฎๆ็ฏๅขๅค็
|
| 10 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 11 |
import os
|
| 12 |
+
import sys
|
| 13 |
+
import types
|
| 14 |
import subprocess
|
| 15 |
import random
|
| 16 |
import numpy as np
|
| 17 |
import torch
|
| 18 |
+
|
| 19 |
+
# ๅคๆญๆฏๅฆๆ CUDA
|
| 20 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
DTYPE = torch.bfloat16 if DEVICE == "cuda" else torch.float32 # CPU โ float32
|
| 22 |
+
|
| 23 |
+
# ---------- CPU ็ฏๅข๏ผๅฑ่ฝ flash-attn ----------
|
| 24 |
+
if DEVICE == "cpu":
|
| 25 |
+
# ๅฐ่ฏๅธ่ฝฝๅทฒๅญๅจ็ flash-attn๏ผ่ฅๅบ็ก้ๅ้ข่ฃ
๏ผ
|
| 26 |
+
subprocess.run("pip uninstall -y flash-attn", shell=True,
|
| 27 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 28 |
+
|
| 29 |
+
# ๆ้ โ็ฉบๅฃณโ flash_attn ๆจกๅ๏ผๆไพๆ็ฎไฝ็ฝฎๆ่ฝฌๅฎ็ฐ
|
| 30 |
+
fake_flash_attn = types.ModuleType("flash_attn")
|
| 31 |
+
fake_layers = types.ModuleType("flash_attn.layers")
|
| 32 |
+
fake_rotary = types.ModuleType("flash_attn.layers.rotary")
|
| 33 |
+
|
| 34 |
+
def _cpu_apply_rotary_emb(x, cos, sin):
|
| 35 |
+
"""็บฏ CPU ็ๆ่ฝฌไฝ็ฝฎ็ผ็ ๏ผๆ
ข๏ผไฝๅฏ็จ๏ผ"""
|
| 36 |
+
x1, x2 = x[..., ::2], x[..., 1::2]
|
| 37 |
+
rot_x1 = x1 * cos - x2 * sin
|
| 38 |
+
rot_x2 = x1 * sin + x2 * cos
|
| 39 |
+
out = torch.empty_like(x)
|
| 40 |
+
out[..., ::2] = rot_x1
|
| 41 |
+
out[..., 1::2] = rot_x2
|
| 42 |
+
return out
|
| 43 |
+
|
| 44 |
+
fake_rotary.apply_rotary_emb = _cpu_apply_rotary_emb
|
| 45 |
+
fake_layers.rotary = fake_rotary
|
| 46 |
+
fake_flash_attn.layers = fake_layers
|
| 47 |
+
|
| 48 |
+
sys.modules.update({
|
| 49 |
+
"flash_attn": fake_flash_attn,
|
| 50 |
+
"flash_attn.layers": fake_layers,
|
| 51 |
+
"flash_attn.layers.rotary": fake_rotary,
|
| 52 |
+
})
|
| 53 |
+
else:
|
| 54 |
+
# GPU ็ฏๅข๏ผๅฆๆ้่ฆๅฎ่ฃ
flash-attn๏ผๅฎ่ฃ
ๅคฑ่ดฅไธ่ดๅฝ๏ผ
|
| 55 |
+
try:
|
| 56 |
+
subprocess.run(
|
| 57 |
+
"pip install flash-attn==2.6.3 --no-build-isolation",
|
| 58 |
+
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
| 59 |
+
shell=True,
|
| 60 |
+
check=True,
|
| 61 |
+
)
|
| 62 |
+
except subprocess.CalledProcessError:
|
| 63 |
+
print("[WARN] flash-attn ๅฎ่ฃ
ๅคฑ่ดฅ๏ผๆจ็ๆง่ฝๅฏ่ฝๅๅฝฑๅใ")
|
| 64 |
+
|
| 65 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 66 |
+
# โก ๅธธ่งไพ่ต
|
| 67 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 68 |
from PIL import Image
|
| 69 |
import gradio as gr
|
| 70 |
import spaces
|
| 71 |
from transformers import AutoModelForCausalLM
|
| 72 |
|
| 73 |
+
# ็จๆท่ชๅฎไน็ฎก็บฟ่ๆฌ๏ผไฟๆๅๆ่ทฏๅพ๏ผ
|
| 74 |
+
from test_img_edit import pipe_img_edit
|
| 75 |
+
from test_img_to_txt import pipe_txt_gen
|
| 76 |
+
from test_txt_to_img import pipe_t2i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 79 |
+
# โข ๅทฅๅ
ทๅฝๆฐ & ๅธธ้
|
| 80 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 81 |
MAX_SEED = 10_000
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def set_global_seed(seed: int = 42) -> None:
|
| 84 |
"""็ปไธ่ฎพ็ฝฎ้ๆบ็งๅญ๏ผCPU / CUDA ่ช้ๅบ๏ผ"""
|
|
|
|
| 89 |
torch.cuda.manual_seed_all(seed)
|
| 90 |
|
| 91 |
def randomize_seed_fn(seed: int, randomize: bool) -> int:
|
| 92 |
+
"""ๆ นๆฎๅค้ๆกๅณๅฎๆฏๅฆ้ๆบ็งๅญ"""
|
| 93 |
return random.randint(0, MAX_SEED) if randomize else seed
|
| 94 |
|
| 95 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 96 |
+
# โฃ ๅ ่ฝฝๆจกๅ
|
| 97 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 98 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # ็งๆไปๅบ่ฏทๅจ Space Secret ่ฎพ็ฝฎ
|
| 99 |
+
MODEL_ID = "AIDC-AI/Ovis-U1-3B"
|
| 100 |
|
| 101 |
+
print(f"[INFO] Loading {MODEL_ID} on {DEVICE} โฆ")
|
| 102 |
model = AutoModelForCausalLM.from_pretrained(
|
| 103 |
+
MODEL_ID,
|
| 104 |
torch_dtype=DTYPE,
|
| 105 |
+
low_cpu_mem_usage=True, # ๅไฝ RSS
|
| 106 |
+
device_map="auto", # CPU ็ฏๅขๅ
จ้จๆพ CPU
|
| 107 |
token=HF_TOKEN,
|
| 108 |
trust_remote_code=True
|
| 109 |
+
).eval()
|
|
|
|
| 110 |
print("[INFO] Model ready!")
|
| 111 |
|
| 112 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 113 |
+
# โค ๆจ็ๅฐ่ฃ
|
| 114 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def process_txt_to_img(prompt: str, height: int, width: int, steps: int,
|
| 116 |
final_seed: int, guidance_scale: float,
|
| 117 |
progress: gr.Progress = gr.Progress(track_tqdm=True)) -> list[Image.Image]:
|
|
|
|
| 130 |
return pipe_img_edit(model, img, prompt, steps,
|
| 131 |
txt_cfg, img_cfg, seed=final_seed)
|
| 132 |
|
| 133 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 134 |
+
# โฅ Gradio UI
|
| 135 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 136 |
+
with gr.Blocks(title="Ovis-U1-3B (CPU/GPU adaptive)") as demo:
|
| 137 |
+
gr.Markdown("# Ovis-U1-3B\nๅคๆจกๆๆๆฌ-ๅพๅ DEMO๏ผCPU/GPU ่ช้ๅบ็๏ผ")
|
| 138 |
|
| 139 |
with gr.Row():
|
| 140 |
+
# -------- ๅทฆไพง๏ผ่พๅ
ฅๅบ --------
|
| 141 |
with gr.Column():
|
| 142 |
with gr.Tabs():
|
| 143 |
+
# โโ Tab 1: Image + Text โ Image โโ
|
| 144 |
with gr.TabItem("Image + Text โ Image"):
|
| 145 |
edit_image_input = gr.Image(label="Input Image", type="pil")
|
| 146 |
with gr.Row():
|
| 147 |
edit_prompt_input = gr.Textbox(
|
| 148 |
label="Prompt",
|
| 149 |
show_label=False,
|
| 150 |
+
placeholder="Describe the editing instructionโฆ",
|
| 151 |
container=False,
|
| 152 |
lines=1
|
| 153 |
)
|
|
|
|
| 167 |
label="Steps", minimum=40, maximum=100, value=50, step=1
|
| 168 |
)
|
| 169 |
edit_seed_slider = gr.Slider(
|
| 170 |
+
label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42
|
|
|
|
| 171 |
)
|
| 172 |
edit_randomize_checkbox = gr.Checkbox(
|
| 173 |
label="Randomize seed", value=False
|
|
|
|
| 184 |
label="Image Editing Examples"
|
| 185 |
)
|
| 186 |
|
| 187 |
+
# โโ Tab 2: Text โ Image โโ
|
| 188 |
with gr.TabItem("Text โ Image"):
|
| 189 |
with gr.Row():
|
| 190 |
prompt_gen_input = gr.Textbox(
|
| 191 |
+
label="Prompt", show_label=False,
|
| 192 |
+
placeholder="Describe the image you wantโฆ",
|
| 193 |
+
container=False, lines=1
|
|
|
|
|
|
|
| 194 |
)
|
| 195 |
run_image_gen_btn = gr.Button("Run", scale=0)
|
| 196 |
|
|
|
|
| 205 |
value=1024, step=32
|
| 206 |
)
|
| 207 |
guidance_slider = gr.Slider(
|
| 208 |
+
label="Guidance Scale", minimum=1.0,
|
| 209 |
+
maximum=30.0, step=0.5, value=5.0
|
| 210 |
)
|
| 211 |
num_steps_slider = gr.Slider(
|
| 212 |
label="Steps", minimum=40, maximum=100, value=50, step=1
|
| 213 |
)
|
| 214 |
seed_slider = gr.Slider(
|
| 215 |
+
label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42
|
|
|
|
| 216 |
)
|
| 217 |
randomize_checkbox = gr.Checkbox(
|
| 218 |
label="Randomize seed", value=False
|
|
|
|
| 229 |
label="Image Generation Examples"
|
| 230 |
)
|
| 231 |
|
| 232 |
+
# โโ Tab 3: Image โ Text โโ
|
| 233 |
with gr.TabItem("Image โ Text"):
|
| 234 |
image_understand_input = gr.Image(label="Input Image", type="pil")
|
| 235 |
with gr.Row():
|
| 236 |
prompt_understand_input = gr.Textbox(
|
| 237 |
label="Prompt", show_label=False,
|
| 238 |
+
placeholder="Describe the question about imageโฆ",
|
| 239 |
container=False, lines=1
|
| 240 |
)
|
| 241 |
run_image_understand_btn = gr.Button("Run", scale=0)
|
|
|
|
| 253 |
|
| 254 |
clean_btn = gr.Button("Clear All Inputs / Outputs")
|
| 255 |
|
| 256 |
+
# -------- ๅณไพง๏ผ่พๅบๅบ --------
|
| 257 |
with gr.Column():
|
| 258 |
+
output_gallery = gr.Gallery(label="Generated Images", columns=2, visible=True)
|
| 259 |
+
output_text = gr.Textbox(label="Generated Text", visible=False, lines=5, interactive=False)
|
| 260 |
+
|
| 261 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโ ไบไปถ็ปๅฎ โโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
def run_img_txt_to_img_tab(prompt, img, steps, seed, txt_cfg, img_cfg,
|
| 263 |
progress=gr.Progress(track_tqdm=True)):
|
| 264 |
if img is None:
|
| 265 |
+
return (gr.update(value=[], visible=False),
|
| 266 |
+
gr.update(value="Please upload an image for editing.", visible=True))
|
| 267 |
+
imgs = process_img_txt_to_img(prompt, img, steps, seed, txt_cfg, img_cfg, progress)
|
| 268 |
+
return (gr.update(value=imgs, visible=True),
|
| 269 |
+
gr.update(value="", visible=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
def run_txt_to_img_tab(prompt, height, width, steps, seed, guidance,
|
| 272 |
progress=gr.Progress(track_tqdm=True)):
|
| 273 |
+
imgs = process_txt_to_img(prompt, height, width, steps, seed, guidance, progress)
|
| 274 |
+
return (gr.update(value=imgs, visible=True),
|
| 275 |
+
gr.update(value="", visible=False))
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
def run_img_to_txt_tab(img, prompt,
|
| 278 |
progress=gr.Progress(track_tqdm=True)):
|
| 279 |
if img is None:
|
| 280 |
+
return (gr.update(value=[], visible=False),
|
| 281 |
+
gr.update(value="Please upload an image for understanding.", visible=True))
|
|
|
|
|
|
|
| 282 |
txt = process_img_to_txt(prompt, img, progress)
|
| 283 |
+
return (gr.update(value=[], visible=False),
|
| 284 |
+
gr.update(value=txt, visible=True))
|
|
|
|
|
|
|
| 285 |
|
| 286 |
def clean_all_fn():
|
| 287 |
+
"""้็ฝฎๅ
จ้จ่พๅ
ฅ/่พๅบๆงไปถ"""
|
| 288 |
return (
|
| 289 |
# Tab 1
|
| 290 |
gr.update(value=None), gr.update(value=""),
|
|
|
|
| 303 |
gr.update(value="", visible=False)
|
| 304 |
)
|
| 305 |
|
| 306 |
+
# ------ Tab 1 ็ปๅฎ ------
|
| 307 |
edit_inputs = [
|
| 308 |
edit_prompt_input, edit_image_input,
|
| 309 |
edit_num_steps_slider, edit_seed_slider,
|
|
|
|
| 322 |
[output_gallery, output_text]
|
| 323 |
)
|
| 324 |
|
| 325 |
+
# ------ Tab 2 ็ปๅฎ ------
|
| 326 |
gen_inputs = [
|
| 327 |
prompt_gen_input, height_slider, width_slider,
|
| 328 |
num_steps_slider, seed_slider, guidance_slider
|
|
|
|
| 340 |
[output_gallery, output_text]
|
| 341 |
)
|
| 342 |
|
| 343 |
+
# ------ Tab 3 ็ปๅฎ ------
|
| 344 |
understand_inputs = [image_understand_input, prompt_understand_input]
|
| 345 |
run_image_understand_btn.click(run_img_to_txt_tab,
|
| 346 |
understand_inputs,
|
|
|
|
| 349 |
understand_inputs,
|
| 350 |
[output_gallery, output_text])
|
| 351 |
|
| 352 |
+
# ๆธ
็ฉบ
|
| 353 |
clean_btn.click(clean_all_fn, [], [
|
| 354 |
edit_image_input, edit_prompt_input, edit_img_guidance_slider,
|
| 355 |
edit_txt_guidance_slider, edit_num_steps_slider, edit_seed_slider,
|
|
|
|
| 359 |
output_gallery, output_text
|
| 360 |
])
|
| 361 |
|
| 362 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 363 |
+
# โฆ ๅฏๅจ Space
|
| 364 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 365 |
if __name__ == "__main__":
|
| 366 |
+
# HF Spaces ้ป่ฎค็ๅฌ 0.0.0.0:7860๏ผๆ ้ share=True
|
| 367 |
+
demo.launch()
|