File size: 917 Bytes
b51ccfe
 
 
 
 
 
 
 
 
 
41c7051
b51ccfe
 
 
 
 
 
 
 
 
 
 
 
41c7051
b51ccfe
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import torch
from diffusers import StableDiffusionPipeline
import gradio as gr

# Load pre-trained model từ Hugging Face Hub
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
)

pipe.to("cpu")
pipe.enable_attention_slicing()

# Hàm sinh ảnh từ prompt
def generate_image(prompt):
    image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5).images[0]
    return image

# Giao diện Gradio
demo = gr.Interface(
    fn=generate_image,
    inputs=gr.Textbox(lines=2, placeholder="Nhập mô tả bằng tiếng Anh...", label="Prompt"),
    outputs=gr.Image(label="Ảnh tạo ra"),
    title="Tạo ảnh từ văn bản với Stable Diffusion",
    description="Ví dụ: 'handwritten Vietnamese text on white paper' hoặc 'bold black Vietnamese text on wooden background'"
)

# Chạy app
if __name__ == "__main__":
    demo.launch()