Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,28 +3,22 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
MODEL_ID = "Lykon/dreamshaper-xl-turbo"
|
| 9 |
|
| 10 |
-
print(f"
|
| 11 |
|
| 12 |
-
# 1. โหลดแบบประหยัด RAM ขั้นสุด
|
| 13 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 14 |
MODEL_ID,
|
| 15 |
-
torch_dtype=torch.float32,
|
| 16 |
low_cpu_mem_usage=True
|
| 17 |
)
|
| 18 |
-
|
| 19 |
-
# 2. ย้ายเข้า CPU และตัดการเชื่อมต่อกับ CUDA Driver (เพื่อไม่ให้ Exit Code: 1)
|
| 20 |
pipe.to("cpu")
|
| 21 |
|
| 22 |
-
#
|
| 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 |
def gen(prompt, steps, cfg, width, height):
|
|
@@ -39,17 +33,17 @@ def gen(prompt, steps, cfg, width, height):
|
|
| 39 |
).images[0]
|
| 40 |
return image
|
| 41 |
|
| 42 |
-
# UI แบบ
|
| 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 |
steps = gr.Slider(1, 10, 4, step=1, label="Steps")
|
| 49 |
-
cfg = gr.Slider(0.0, 3.0, 1.
|
| 50 |
width = gr.Slider(256, 512, 384, step=64, label="Width")
|
| 51 |
height = gr.Slider(256, 512, 512, step=64, label="Height")
|
| 52 |
-
btn = gr.Button("
|
| 53 |
with gr.Column():
|
| 54 |
output_img = gr.Image(label="Result")
|
| 55 |
|
|
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
+
# ชื่อโมเดลที่ถูกต้องสำหรับเรียกผ่าน API ของ Hugging Face
|
| 7 |
+
MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
|
|
|
|
| 8 |
|
| 9 |
+
print(f"🚀 Recovering Space with: {MODEL_ID}")
|
| 10 |
|
|
|
|
| 11 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
+
torch_dtype=torch.float32,
|
| 14 |
low_cpu_mem_usage=True
|
| 15 |
)
|
|
|
|
|
|
|
| 16 |
pipe.to("cpu")
|
| 17 |
|
| 18 |
+
# มาตรการ Never OOM สำหรับเครื่องฟรี
|
| 19 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 20 |
pipe.enable_attention_slicing("max")
|
| 21 |
pipe.enable_vae_tiling()
|
|
|
|
|
|
|
| 22 |
torch.set_num_threads(os.cpu_count())
|
| 23 |
|
| 24 |
def gen(prompt, steps, cfg, width, height):
|
|
|
|
| 33 |
).images[0]
|
| 34 |
return image
|
| 35 |
|
| 36 |
+
# UI แบบประหยัด RAM
|
| 37 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 38 |
+
gr.Markdown(f"### 🛠️ Z-Image-Turbo CPU Mode")
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column():
|
| 41 |
prompt = gr.Textbox(label="Prompt", placeholder="ใส่คำที่อยากเจน...")
|
| 42 |
steps = gr.Slider(1, 10, 4, step=1, label="Steps")
|
| 43 |
+
cfg = gr.Slider(0.0, 3.0, 1.0, step=0.1, label="CFG (Z-Image ชอบ 1.0)")
|
| 44 |
width = gr.Slider(256, 512, 384, step=64, label="Width")
|
| 45 |
height = gr.Slider(256, 512, 512, step=64, label="Height")
|
| 46 |
+
btn = gr.Button("Generate", variant="primary")
|
| 47 |
with gr.Column():
|
| 48 |
output_img = gr.Image(label="Result")
|
| 49 |
|