Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,43 @@
|
|
1 |
-
|
2 |
-
from PIL import Image
|
3 |
import torch
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
""
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
pipeline = StableDiffusionPipeline.from_pretrained(model_id).to(device)
|
21 |
|
22 |
-
|
23 |
-
|
|
|
|
|
24 |
return image
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
import subprocess
|
5 |
|
6 |
+
# ติดตั้ง flash-attn แม้จะไม่ได้ใช้โดยตรง (ข้าม build CUDA)
|
7 |
+
subprocess.run(
|
8 |
+
'pip install flash-attn --no-build-isolation',
|
9 |
+
env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"},
|
10 |
+
shell=True
|
11 |
+
)
|
12 |
|
13 |
+
# ใช้ CPU
|
14 |
+
device = "cpu"
|
15 |
|
16 |
+
# โหลดโมเดลเบา: sd-turbo
|
17 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
18 |
+
"stabilityai/sd-turbo",
|
19 |
+
torch_dtype=torch.float32
|
20 |
+
)
|
21 |
+
pipe = pipe.to(device)
|
22 |
+
pipe.safety_checker = None # ปิด safety checker เพื่อความเร็ว
|
23 |
|
24 |
+
# เปิด attention_slicing (แม้บน CPU ก็ช่วยเรื่องหน่วยความจำ)
|
25 |
+
pipe.enable_attention_slicing()
|
|
|
26 |
|
27 |
+
# ฟังก์ชันสร้างภาพ
|
28 |
+
def generate_image(prompt):
|
29 |
+
result = pipe(prompt, num_inference_steps=10, guidance_scale=3.0)
|
30 |
+
image = result.images[0]
|
31 |
return image
|
32 |
|
33 |
+
# Gradio UI
|
34 |
+
io = gr.Interface(
|
35 |
+
fn=generate_image,
|
36 |
+
inputs=[gr.Textbox(label="Enter your prompt")],
|
37 |
+
outputs=[gr.Image(label="Generated Image")],
|
38 |
+
theme="Yntec/HaleyCH_Theme_Orange",
|
39 |
+
description="⚠ Running on CPU using sd-turbo. Optimized for speed with low inference steps."
|
40 |
+
)
|
41 |
+
|
42 |
+
# เปิด Gradio ด้วย queue ป้องกันค้างถ้ามีหลายคำสั่ง
|
43 |
+
io.queue().launch(debug=True)
|