Update app.py
Browse files
app.py
CHANGED
|
@@ -2,66 +2,50 @@ import torch
|
|
| 2 |
from diffusers import AutoPipelineForText2Image
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 8 |
-
|
| 9 |
-
print(f"Đang chạy trên: {device.upper()}")
|
| 10 |
-
|
| 11 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 12 |
-
"
|
| 13 |
-
torch_dtype=
|
| 14 |
-
|
| 15 |
-
safety_checker=None,
|
| 16 |
-
requires_safety_checker=False
|
| 17 |
)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
pipe.to("cuda")
|
| 22 |
-
pipe.enable_xformers_memory_efficient_attention()
|
| 23 |
-
|
| 24 |
-
# Load LoRA Á Đông
|
| 25 |
-
pipe.load_lora_weights(
|
| 26 |
-
"ntc-ai/SDXL-LoRA-slider.asian",
|
| 27 |
-
weight_name="asian.safetensors",
|
| 28 |
-
adapter_name="asian"
|
| 29 |
-
)
|
| 30 |
-
pipe.set_adapters(["asian"], adapter_weights=1.2)
|
| 31 |
|
| 32 |
def generate(prompt):
|
| 33 |
-
|
|
|
|
| 34 |
image = pipe(
|
| 35 |
prompt=full_prompt,
|
| 36 |
-
num_inference_steps=
|
| 37 |
guidance_scale=0.0,
|
| 38 |
height=768,
|
| 39 |
width=768
|
| 40 |
).images[0]
|
| 41 |
return image
|
| 42 |
|
| 43 |
-
#
|
| 44 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 45 |
-
gr.Markdown("# SDXL-Turbo Việt Nam –
|
| 46 |
-
gr.Markdown("
|
| 47 |
|
| 48 |
-
|
| 49 |
label="Prompt tiếng Việt",
|
| 50 |
-
placeholder="
|
| 51 |
lines=2
|
| 52 |
)
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
output = gr.Image(label="Kết quả")
|
| 56 |
|
| 57 |
gr.Examples([
|
| 58 |
"cô gái mặc áo dài đứng giữa cánh đồng lúa vàng",
|
| 59 |
"mèo đội nón lá ăn phở Hà Nội",
|
| 60 |
"Hội An về đêm đèn lồng lung linh",
|
| 61 |
"cà phê sữa đá Sài Gòn"
|
| 62 |
-
], inputs=
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
demo.launch()
|
|
|
|
| 2 |
from diffusers import AutoPipelineForText2Image
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
# DÙNG PHIÊN BẢN NHẸ CỦA SDXL-TURBO (chạy được trên CPU Space miễn phí)
|
| 6 |
+
# Model này chỉ 2.5GB, không cần CUDA, vẫn đẹp
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 8 |
+
"segmind/Segmind-Vega", # SDXL-Turbo nhẹ nhất 2025, chạy ngon CPU
|
| 9 |
+
torch_dtype=torch.float32, # CPU thì dùng float32
|
| 10 |
+
safety_checker=None
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Load LoRA Á Đông (vẫn dùng được)
|
| 14 |
+
pipe.load_lora_weights("ntc-ai/SDXL-LoRA-slider.asian", weight_name="asian.safetensors")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def generate(prompt):
|
| 17 |
+
# Prompt booster Việt Nam
|
| 18 |
+
full_prompt = f"{prompt}, vietnamese girl, ao dai, realistic, detailed, 8k"
|
| 19 |
image = pipe(
|
| 20 |
prompt=full_prompt,
|
| 21 |
+
num_inference_steps=6, # CPU thì tăng lên 6 để đẹp hơn
|
| 22 |
guidance_scale=0.0,
|
| 23 |
height=768,
|
| 24 |
width=768
|
| 25 |
).images[0]
|
| 26 |
return image
|
| 27 |
|
| 28 |
+
# UI đẹp
|
| 29 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 30 |
+
gr.Markdown("# SDXL-Turbo Việt Nam – Chạy Miễn Phí 24/7")
|
| 31 |
+
gr.Markdown("Không cần GPU • Ảnh đẹp • Đúng áo dài, nón lá, phở")
|
| 32 |
|
| 33 |
+
txt = gr.Textbox(
|
| 34 |
label="Prompt tiếng Việt",
|
| 35 |
+
placeholder="cô gái mặc áo dài đứng giữa cánh đồng lúa vàng",
|
| 36 |
lines=2
|
| 37 |
)
|
| 38 |
+
btn = gr.Button("Tạo ảnh", variant="primary")
|
| 39 |
+
out = gr.Image()
|
|
|
|
| 40 |
|
| 41 |
gr.Examples([
|
| 42 |
"cô gái mặc áo dài đứng giữa cánh đồng lúa vàng",
|
| 43 |
"mèo đội nón lá ăn phở Hà Nội",
|
| 44 |
"Hội An về đêm đèn lồng lung linh",
|
| 45 |
"cà phê sữa đá Sài Gòn"
|
| 46 |
+
], inputs=txt)
|
| 47 |
|
| 48 |
+
btn.click(generate, txt, out)
|
| 49 |
+
txt.submit(generate, txt, out)
|
| 50 |
|
| 51 |
demo.launch()
|