Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,42 +3,35 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
-
# เปลี่ยน
|
| 7 |
-
#
|
| 8 |
-
MODEL_ID = "
|
| 9 |
|
| 10 |
-
print(f"
|
| 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 |
-
# ใ
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
"ศิลปะ (Artistic)": "digital art, masterpiece, intricate details, vibrant",
|
| 29 |
-
"ไม่เน้นสไตล์": ""
|
| 30 |
-
}
|
| 31 |
|
| 32 |
-
def gen(prompt,
|
| 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=
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
with gr.Row():
|
| 53 |
with gr.Column():
|
| 54 |
-
prompt = gr.Textbox(label="Prompt",
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 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,
|
| 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()
|