Saravutw commited on
Commit
03c151a
·
verified ·
1 Parent(s): 9298fbf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -28
app.py CHANGED
@@ -3,62 +3,104 @@ import gradio as gr
3
  import os
4
  from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
5
 
 
6
  MODEL_ID = "Lykon/dreamshaper-xl-turbo"
7
 
8
- # โหลดโมเดลและตั้งค่า Never-OOM
9
- pipe = AutoPipelineForText2Image.from_pretrained(MODEL_ID, torch_dtype=torch.float32, low_cpu_mem_usage=True)
 
 
 
 
 
 
 
 
10
  pipe.to("cpu")
 
 
11
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
12
  pipe.enable_attention_slicing("max")
13
- pipe.enable_vae_tiling()
14
- torch.set_num_threads(os.cpu_count())
15
 
16
- # ดิกชัรีเ็บไตล์ (Prompt ส็จรูป)
17
- STYLE_MAP = {
18
- "สมจริง (Cinematic)": "highly detailed film still, cinematic lighting, masterpiece, 8k, realistic skin, professional photography",
19
- "อนิเมะ (Anime)": "anime style, high quality, vibrant colors, clean lines, masterpiece, aesthetic",
20
- "วาดเขียน (Digital Art)": "detailed digital illustration, sharp focus, artistic, vibrant, concept art",
21
- "ไม่เน้นสไตล์": ""
 
 
22
  }
23
 
24
- def gen(prompt, style_name, negative_prompt, steps, cfg, width, height):
 
 
 
 
 
 
 
 
 
 
 
25
  if not prompt: return None
26
 
27
- # รวม Prompt จาปุ่มเลือกสไตล์
28
- style_prompt = STYLE_MAP.get(style_name, "")
29
- full_prompt = f"{prompt}, {style_prompt}"
 
 
30
 
31
  with torch.no_grad():
32
  image = pipe(
33
- prompt=full_prompt,
34
  negative_prompt=negative_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
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
43
- gr.Markdown("### 🚀 Easy One-Click CPU Turbo")
44
 
45
  with gr.Row():
46
  with gr.Column():
47
- prompt = gr.Textbox(label="1. พิมพ์คำสั้นๆ (เช่น woman, city)", lines=2)
48
- style_name = gr.Radio(choices=list(STYLE_MAP.keys()), value="สมจริง (Cinematic)", label="2. เลือกสไตล์ที่ต้องการ")
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  with gr.Accordion("Advanced Settings", open=False):
51
- negative = gr.Textbox(label="Negative", value="low quality, blurry, deformed, messy")
52
- steps = gr.Slider(1, 10, 4, step=1, label="Steps (แนะนำ 4-6)")
53
  cfg = gr.Slider(0.0, 3.0, 1.2, step=0.1, label="CFG Scale")
54
- width = gr.Slider(256, 512, 384, step=64, label="Width")
55
- height = gr.Slider(256, 512, 512, step=64, label="Height")
56
-
57
- btn = gr.Button("Generate Now", variant="primary")
58
 
59
  with gr.Column():
60
  output_img = gr.Image(label="Result")
61
 
62
- btn.click(fn=gen, inputs=[prompt, style_name, negative, steps, cfg, width, height], outputs=[output_img])
 
 
 
 
63
 
64
- demo.launch()
 
 
3
  import os
4
  from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
5
 
6
+ # ใช้โมเดลเดิม
7
  MODEL_ID = "Lykon/dreamshaper-xl-turbo"
8
 
9
+ print("Loading for Pure CPU Environment (No GPU)...")
10
+
11
+ # โหลดโมเดลแบบ CPU
12
+ pipe = AutoPipelineForText2Image.from_pretrained(
13
+ MODEL_ID,
14
+ torch_dtype=torch.float32,
15
+ low_cpu_mem_usage=True,
16
+ safety_checker=None,
17
+ requires_safety_checker=False
18
+ )
19
  pipe.to("cpu")
20
+
21
+ # [SAFE CPU OPTIMIZATIONS]
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_TEMPLATES = {
29
+ "None": "{prompt}",
30
+ "Cinematic": "cinematic photo, {prompt}, high resolution, detailed, 8k, masterpiece",
31
+ "Digital Art": "digital art, {prompt}, vibrant colors, detailed, trending on artstation",
32
+ "Anime": "anime style, {prompt}, vivid, expressive, high quality art",
33
+ "Oil Painting": "oil painting, {prompt}, brush strokes, classical style, textured canvas",
34
+ "Photography": "professional photography, {prompt}, realistic, 8k uhd, dslr, high detail"
35
  }
36
 
37
+ # ฟังก์ชันคำนวณสัดส่วนภาพ
38
+ def get_dimensions(ratio_str):
39
+ ratios = {
40
+ "1:1 (Square)": (512, 512),
41
+ "4:3 (Classic)": (512, 384),
42
+ "3:4 (Portrait)": (384, 512),
43
+ "16:9 (Widescreen)": (512, 288),
44
+ "9:16 (Vertical)": (288, 512)
45
+ }
46
+ return ratios.get(ratio_str, (512, 512))
47
+
48
+ def gen(prompt, negative_prompt, style, aspect_ratio, steps, cfg):
49
  if not prompt: return None
50
 
51
+ # รวม Prompt กับ Style
52
+ final_prompt = STYLE_TEMPLATES[style].format(prompt=prompt)
53
+
54
+ # กำหนดขนาดภาพจาก Aspect Ratio
55
+ width, height = get_dimensions(aspect_ratio)
56
 
57
  with torch.no_grad():
58
  image = pipe(
59
+ prompt=final_prompt,
60
  negative_prompt=negative_prompt,
61
  num_inference_steps=int(steps),
62
  guidance_scale=float(cfg),
63
+ width=width,
64
+ height=height
65
  ).images[0]
66
  return image
67
 
68
+ # UI
69
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
+ gr.Markdown("### 🚀 Pure CPU Turbo (Style & Aspect Ratio Support)")
71
 
72
  with gr.Row():
73
  with gr.Column():
74
+ prompt = gr.Textbox(label="Prompt", lines=3, placeholder="What do you want to create?")
75
+
76
+ with gr.Row():
77
+ style_dropdown = gr.Dropdown(
78
+ choices=list(STYLE_TEMPLATES.keys()),
79
+ value="None",
80
+ label="Select Style"
81
+ )
82
+ ratio_dropdown = gr.Dropdown(
83
+ choices=["1:1 (Square)", "4:3 (Classic)", "3:4 (Portrait)", "16:9 (Widescreen)", "9:16 (Vertical)"],
84
+ value="1:1 (Square)",
85
+ label="Aspect Ratio"
86
+ )
87
+
88
+ negative = gr.Textbox(label="Negative", value="deformed, lowres, blurry, nsfw")
89
 
90
  with gr.Accordion("Advanced Settings", open=False):
91
+ steps = gr.Slider(1, 10, 4, step=1, label="Steps (Turbo recommended 1-4)")
 
92
  cfg = gr.Slider(0.0, 3.0, 1.2, step=0.1, label="CFG Scale")
93
+
94
+ btn = gr.Button("Generate", variant="primary")
 
 
95
 
96
  with gr.Column():
97
  output_img = gr.Image(label="Result")
98
 
99
+ btn.click(
100
+ fn=gen,
101
+ inputs=[prompt, negative, style_dropdown, ratio_dropdown, steps, cfg],
102
+ outputs=[output_img]
103
+ )
104
 
105
+ if __name__ == "__main__":
106
+ demo.launch()