Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
import os
|
| 4 |
+
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
+
# --- เปลี่ยนชื่อโมเดลที่พังๆ ตรงนี้ได้เลย ---
|
| 7 |
+
# เช่น "stabilityai/sdxl-turbo", "RunDiffusion/Juggernaut-XL-v9", ฯลฯ
|
| 8 |
+
MODEL_ID = "models/Tongyi-MAI/Z-Image-Turbo"
|
| 9 |
+
|
| 10 |
+
print(f"Starting Recovery Mode for: {MODEL_ID}")
|
| 11 |
+
|
| 12 |
+
# 1. โหลดแบบประหยัด RAM ขั้นสุด
|
| 13 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
torch_dtype=torch.float32, # บังคับ float32 เพราะ CPU ไม่รองรับ float16 ในบางคำสั่ง
|
| 16 |
+
low_cpu_mem_usage=True
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# 2. ย้ายเข้า CPU และตัดการเชื่อมต่อกับ CUDA Driver (เพื่อไม่ให้ Exit Code: 1)
|
| 20 |
+
pipe.to("cpu")
|
| 21 |
+
|
| 22 |
+
# 3. ใส่เกราะกันระเบิด (Never OOM)
|
| 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):
|
| 31 |
+
if not prompt: return None
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
image = pipe(
|
| 34 |
+
prompt=prompt,
|
| 35 |
+
num_inference_steps=int(steps),
|
| 36 |
+
guidance_scale=float(cfg),
|
| 37 |
+
width=int(width),
|
| 38 |
+
height=int(height)
|
| 39 |
+
).images[0]
|
| 40 |
+
return image
|
| 41 |
+
|
| 42 |
+
# UI แบบคลีนๆ ใช้ง่าย
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown(f"### 🛠️ Space Recovery: {MODEL_ID}")
|
| 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.2, step=0.1, label="CFG")
|
| 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("Recover & Generate", variant="primary")
|
| 53 |
+
with gr.Column():
|
| 54 |
+
output_img = gr.Image(label="Result")
|
| 55 |
+
|
| 56 |
+
btn.click(fn=gen, inputs=[prompt, steps, cfg, width, height], outputs=[output_img])
|
| 57 |
+
|
| 58 |
+
demo.launch()
|