vanhai123 commited on
Commit
b51ccfe
·
verified ·
1 Parent(s): 7fdf492

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import StableDiffusionPipeline
3
+ import gradio as gr
4
+
5
+ # Load pre-trained model từ Hugging Face Hub
6
+ pipe = StableDiffusionPipeline.from_pretrained(
7
+ "runwayml/stable-diffusion-v1-5",
8
+ torch_dtype=torch.float16,
9
+ revision="fp16",
10
+ use_auth_token=True # Nếu chạy trên HF Spaces thì không cần dòng này
11
+ )
12
+
13
+ # Đưa mô hình lên GPU (nếu có)
14
+ pipe.to("cuda")
15
+ pipe.enable_attention_slicing()
16
+
17
+ # Hàm sinh ảnh từ prompt
18
+ def generate_image(prompt):
19
+ image = pipe(prompt, num_inference_steps=30, guidance_scale=7.5).images[0]
20
+ return image
21
+
22
+ # Giao diện Gradio
23
+ demo = gr.Interface(
24
+ fn=generate_image,
25
+ inputs=gr.Textbox(lines=2, placeholder="Nhập mô tả bằng tiếng Anh...", label="Prompt"),
26
+ outputs=gr.Image(label="Ảnh tạo ra"),
27
+ title="🧠 Tạo ảnh từ văn bản với Stable Diffusion",
28
+ description="Ví dụ: 'handwritten Vietnamese text on white paper' hoặc 'bold black Vietnamese text on wooden background'"
29
+ )
30
+
31
+ # Chạy app
32
+ if __name__ == "__main__":
33
+ demo.launch()