Saravutw commited on
Commit
264d402
·
verified ·
1 Parent(s): 37d21ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -26
app.py CHANGED
@@ -3,35 +3,44 @@ 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),
@@ -39,20 +48,31 @@ 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"### 🛠️ 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()
 
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()