Pichayada commited on
Commit
9f518df
·
verified ·
1 Parent(s): 20c77c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -6
app.py CHANGED
@@ -16,14 +16,20 @@ pipe = StableDiffusionPipeline.from_pretrained(
16
  # ส่งโมเดลไปยังอุปกรณ์ที่ใช้
17
  pipe = pipe.to(device)
18
 
19
- # ถ้าใช้ CPU แนะนำเปิด attention slicing เพื่อลดการใช้ RAM
20
- if device == "cpu":
21
- pipe.enable_attention_slicing()
 
22
 
23
- # ฟังก์ชันสำหรับแปลงข้อความเป็นภาพ
24
  def text_to_image(prompt):
25
  with torch.autocast(device) if device == "cuda" else torch.inference_mode():
26
- image = pipe(prompt).images[0]
 
 
 
 
 
27
  return image
28
 
29
  # Gradio Interface
@@ -32,5 +38,5 @@ gr.Interface(
32
  inputs=gr.Textbox(label="📝 Text Prompt"),
33
  outputs=gr.Image(label="🎨 Generated Image"),
34
  title="🖼 Text-to-Image with Stable Diffusion",
35
- description="แอปนี้สามารถรันบน CPU หรือ GPU ได้ตามเครื่องที่มี โดยใช้โมเดล Stable Diffusion จาก Hugging Face"
36
  ).launch()
 
16
  # ส่งโมเดลไปยังอุปกรณ์ที่ใช้
17
  pipe = pipe.to(device)
18
 
19
+ # เปิด optimization สำหรับความเร็ว
20
+ pipe.enable_attention_slicing()
21
+ if device == "cuda":
22
+ pipe.enable_model_cpu_offload() # เฉพาะถ้าใช้ GPU จะช่วยประหยัด VRAM
23
 
24
+ # ฟังก์ชันสำหรับแปลงข้อความเป็นภาพ (พร้อมปรับ speed)
25
  def text_to_image(prompt):
26
  with torch.autocast(device) if device == "cuda" else torch.inference_mode():
27
+ image = pipe(
28
+ prompt,
29
+ height=384, # ลด resolution เพื่อให้เร็วขึ้น
30
+ width=384,
31
+ num_inference_steps=10 # ลดจำนวน step เพื่อความเร็ว
32
+ ).images[0]
33
  return image
34
 
35
  # Gradio Interface
 
38
  inputs=gr.Textbox(label="📝 Text Prompt"),
39
  outputs=gr.Image(label="🎨 Generated Image"),
40
  title="🖼 Text-to-Image with Stable Diffusion",
41
+ description="แอปนี้สามารถรันบน CPU หรือ GPU ได้ตามเครื่องที่มี โดยใช้โมเดล Stable Diffusion (เร็วขึ้นด้วยเทคนิคประหยัดเวลา 🚀)"
42
  ).launch()