Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,35 +3,44 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
MODEL_ID = "models/Tongyi-MAI/Z-Image-Turbo"
|
| 9 |
|
| 10 |
-
print(f"
|
| 11 |
|
| 12 |
-
# 1. โหลด
|
| 13 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 14 |
MODEL_ID,
|
| 15 |
-
torch_dtype=torch.float32,
|
| 16 |
low_cpu_mem_usage=True
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# 2.
|
| 20 |
pipe.to("cpu")
|
| 21 |
|
| 22 |
-
# 3.
|
| 23 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 24 |
pipe.enable_attention_slicing("max")
|
| 25 |
pipe.enable_vae_tiling()
|
| 26 |
-
|
| 27 |
-
# 4. รีดพลัง CPU ทั้งหมดที่มี
|
| 28 |
torch.set_num_threads(os.cpu_count())
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if not prompt: return None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
with torch.no_grad():
|
| 33 |
image = pipe(
|
| 34 |
-
prompt=
|
| 35 |
num_inference_steps=int(steps),
|
| 36 |
guidance_scale=float(cfg),
|
| 37 |
width=int(width),
|
|
@@ -39,20 +48,31 @@ def gen(prompt, steps, cfg, width, height):
|
|
| 39 |
).images[0]
|
| 40 |
return image
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown(f"##
|
|
|
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
-
with gr.Column():
|
| 47 |
-
prompt = gr.Textbox(label="Prompt", placeholder="
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
demo.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
+
# ตั้งค่า Model ID เป็นตัวที่คุณต้องการ
|
| 7 |
+
MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
|
|
|
|
| 8 |
|
| 9 |
+
print(f"🚀 Initializing Recovery Mode for: {MODEL_ID}")
|
| 10 |
|
| 11 |
+
# 1. โหลดโมเดลด้วยระบบ Low RAM Usage
|
| 12 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 13 |
MODEL_ID,
|
| 14 |
+
torch_dtype=torch.float32,
|
| 15 |
low_cpu_mem_usage=True
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# 2. บังคับรันบน CPU เพื่อหลีกเลี่ยง Error เกี่ยวกับ CUDA Driver
|
| 19 |
pipe.to("cpu")
|
| 20 |
|
| 21 |
+
# 3. มาตรการ Never OOM: หั่นการประมวลผลเป็นส่วนย่อย
|
| 22 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 23 |
pipe.enable_attention_slicing("max")
|
| 24 |
pipe.enable_vae_tiling()
|
|
|
|
|
|
|
| 25 |
torch.set_num_threads(os.cpu_count())
|
| 26 |
|
| 27 |
+
# ระบบ Style สำเร็จรูปเพื่อความสะดวก
|
| 28 |
+
STYLE_MAP = {
|
| 29 |
+
"สมจริง (Realistic)": "hyperrealistic, 8k, highly detailed, cinematic lighting",
|
| 30 |
+
"อนิเมะ (Anime)": "anime style, vibrant colors, masterpiece, clean lines",
|
| 31 |
+
"ศิลปะดิจิทัล (Digital Art)": "detailed digital painting, concept art, artistic",
|
| 32 |
+
"ดั้งเดิม (None)": ""
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
def gen(prompt, style, steps, cfg, width, height):
|
| 36 |
if not prompt: return None
|
| 37 |
+
|
| 38 |
+
# รวม Prompt กับ Style เข้าด้วยกัน
|
| 39 |
+
full_prompt = f"{prompt}, {STYLE_MAP[style]}" if style != "ดั้งเดิม (None)" else prompt
|
| 40 |
+
|
| 41 |
with torch.no_grad():
|
| 42 |
image = pipe(
|
| 43 |
+
prompt=full_prompt,
|
| 44 |
num_inference_steps=int(steps),
|
| 45 |
guidance_scale=float(cfg),
|
| 46 |
width=int(width),
|
|
|
|
| 48 |
).images[0]
|
| 49 |
return image
|
| 50 |
|
| 51 |
+
# จัดหน้าจอ UI ให้ใช้ง่ายและดูดี
|
| 52 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 53 |
+
gr.Markdown(f"## 🛠️ Space Recovery: {MODEL_ID}")
|
| 54 |
+
gr.Markdown("สถานะ: **CPU Mode (Never OOM Enabled)**")
|
| 55 |
+
|
| 56 |
with gr.Row():
|
| 57 |
+
with gr.Column(scale=1):
|
| 58 |
+
prompt = gr.Textbox(label="คำสั่ง (Prompt)", placeholder="เช่น futuristic city, cyberpunk style", lines=3)
|
| 59 |
+
style = gr.Radio(choices=list(STYLE_MAP.keys()), value="ดั้งเดิม (None)", label="เลือกสไตล์")
|
| 60 |
+
|
| 61 |
+
with gr.Accordion("การตั้งค่าขั้นสูง (Advanced)", open=False):
|
| 62 |
+
steps = gr.Slider(1, 10, 4, step=1, label="Steps (แนะนำ 4-6)")
|
| 63 |
+
cfg = gr.Slider(0.0, 3.0, 1.2, step=0.1, label="CFG (แนะนำ 1.0-1.5)")
|
| 64 |
+
width = gr.Slider(256, 512, 384, step=64, label="Width")
|
| 65 |
+
height = gr.Slider(256, 512, 512, step=64, label="Height")
|
| 66 |
+
|
| 67 |
+
btn = gr.Button("Generate", variant="primary")
|
| 68 |
+
|
| 69 |
+
with gr.Column(scale=1):
|
| 70 |
+
output_img = gr.Image(label="ผลลัพธ์", interactive=False)
|
| 71 |
+
|
| 72 |
+
btn.click(
|
| 73 |
+
fn=gen,
|
| 74 |
+
inputs=[prompt, style, steps, cfg, width, height],
|
| 75 |
+
outputs=[output_img]
|
| 76 |
+
)
|
| 77 |
|
| 78 |
demo.launch()
|