Saravutw commited on
Commit
01afb5b
·
verified ·
1 Parent(s): e0ed766

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -35
app.py CHANGED
@@ -3,42 +3,35 @@ import gradio as gr
3
  import os
4
  from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
5
 
6
- # เปลี่ยนเป็นโมเดลตระกูล XL ที่เน้นความสมจริและนไ (Lightning/Turbo)
7
- # ตัวนี้ยังอยู่ในตระกูลดียวกับทีทำไว้ แต่ให้งาผิวแะแสงที่ต่างออกไป
8
- MODEL_ID = "SG_161222/RealVisXL_V4.0_Lightning"
9
 
10
- print(f"Loading {MODEL_ID} using existing CPU-optimized structure...")
11
 
 
12
  pipe = AutoPipelineForText2Image.from_pretrained(
13
  MODEL_ID,
14
- torch_dtype=torch.float32,
15
  low_cpu_mem_usage=True
16
  )
17
 
 
18
  pipe.to("cpu")
19
 
20
- # ใช้บรทัดฐานเมที่ทำให้เรา Never OOM
21
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
22
  pipe.enable_attention_slicing("max")
23
  pipe.enable_vae_tiling()
24
- torch.set_num_threads(os.cpu_count())
25
 
26
- STYLE_MAP = {
27
- "สมจริง (Photo)": "cinematic photo, highly detailed, 8k wallpaper, realistic skin texture",
28
- "ศิลปะ (Artistic)": "digital art, masterpiece, intricate details, vibrant",
29
- "ไม่เน้นสไตล์": ""
30
- }
31
 
32
- def gen(prompt, style_name, negative_prompt, steps, cfg, width, height):
33
  if not prompt: return None
34
-
35
- style_prompt = STYLE_MAP.get(style_name, "")
36
- full_prompt = f"{prompt}, {style_prompt}"
37
-
38
  with torch.no_grad():
39
  image = pipe(
40
- prompt=full_prompt,
41
- negative_prompt=negative_prompt,
42
  num_inference_steps=int(steps),
43
  guidance_scale=float(cfg),
44
  width=int(width),
@@ -46,26 +39,20 @@ def gen(prompt, style_name, negative_prompt, steps, cfg, width, height):
46
  ).images[0]
47
  return image
48
 
49
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
50
- gr.Markdown(f"### 🚀 CPU Optimized: {MODEL_ID}")
51
-
52
  with gr.Row():
53
  with gr.Column():
54
- prompt = gr.Textbox(label="Prompt", lines=2)
55
- style_name = gr.Radio(choices=list(STYLE_MAP.keys()), value="สมจริง (Photo)", label="Style")
56
-
57
- with gr.Accordion("Advanced Settings", open=False):
58
- negative = gr.Textbox(label="Negative", value="low quality, blurry, deformed")
59
- steps = gr.Slider(1, 10, 4, step=1, label="Steps (Lightning/Turbo use 4-8)")
60
- cfg = gr.Slider(0.0, 2.0, 1.0, step=0.1, label="CFG (Lightning use 1.0-2.0)")
61
- width = gr.Slider(256, 512, 384, step=64, label="Width")
62
- height = gr.Slider(256, 512, 512, step=64, label="Height")
63
-
64
- btn = gr.Button("Generate", variant="primary")
65
-
66
  with gr.Column():
67
  output_img = gr.Image(label="Result")
68
 
69
- btn.click(fn=gen, inputs=[prompt, style_name, negative, steps, cfg, width, height], outputs=[output_img])
70
 
71
  demo.launch()
 
3
  import os
4
  from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
5
 
6
+ # --- เปลี่ยนชื่อโมเดลที่พัๆ ตี้ด้เลย ---
7
+ # เ่น "stabilityai/sdxl-turbo", "RunDiffusion/Juggernaut-XL-v9", ฯ
8
+ MODEL_ID = "Lykon/dreamshaper-xl-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
  ).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()