Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,31 +9,36 @@ pipe = StableDiffusionPipeline.from_pretrained(
|
|
| 9 |
torch_dtype=torch.float16,
|
| 10 |
use_auth_token=False
|
| 11 |
)
|
| 12 |
-
#pipe = pipe.to("cuda") # Giả sử bạn
|
|
|
|
| 13 |
|
| 14 |
# Hàm tạo hình ảnh
|
| 15 |
def generate_image(prompt, negative_prompt="", num_inference_steps=50, guidance_scale=7.5):
|
| 16 |
image = pipe(
|
| 17 |
prompt,
|
| 18 |
negative_prompt=negative_prompt,
|
| 19 |
-
num_inference_steps=num_inference_steps,
|
| 20 |
guidance_scale=guidance_scale
|
| 21 |
).images[0]
|
| 22 |
return image
|
| 23 |
|
| 24 |
-
# Tạo giao diện
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
gr.Textbox(label="
|
| 30 |
-
gr.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# Khởi chạy
|
| 39 |
-
|
|
|
|
| 9 |
torch_dtype=torch.float16,
|
| 10 |
use_auth_token=False
|
| 11 |
)
|
| 12 |
+
#pipe = pipe.to("cuda") # Giả sử bạn dùng GPU
|
| 13 |
+
pipe.enable_attention_slicing() # Tối ưu RAM
|
| 14 |
|
| 15 |
# Hàm tạo hình ảnh
|
| 16 |
def generate_image(prompt, negative_prompt="", num_inference_steps=50, guidance_scale=7.5):
|
| 17 |
image = pipe(
|
| 18 |
prompt,
|
| 19 |
negative_prompt=negative_prompt,
|
| 20 |
+
num_inference_steps=int(num_inference_steps),
|
| 21 |
guidance_scale=guidance_scale
|
| 22 |
).images[0]
|
| 23 |
return image
|
| 24 |
|
| 25 |
+
# Tạo giao diện với Blocks
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# Text-to-Image with Stable Diffusion")
|
| 28 |
+
gr.Markdown("Enter a prompt to generate an image.")
|
| 29 |
+
with gr.Row():
|
| 30 |
+
prompt = gr.Textbox(label="Prompt", placeholder="E.g., 'A futuristic city at sunset'")
|
| 31 |
+
negative_prompt = gr.Textbox(label="Negative Prompt (optional)", placeholder="E.g., 'blurry, low quality'")
|
| 32 |
+
with gr.Row():
|
| 33 |
+
steps = gr.Slider(label="Inference Steps", minimum=10, maximum=100, value=50, step=1)
|
| 34 |
+
guidance = gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7.5, step=0.5)
|
| 35 |
+
btn = gr.Button("Generate")
|
| 36 |
+
output = gr.Image(label="Generated Image")
|
| 37 |
+
btn.click(
|
| 38 |
+
fn=generate_image,
|
| 39 |
+
inputs=[prompt, negative_prompt, steps, guidance],
|
| 40 |
+
outputs=output
|
| 41 |
+
)
|
| 42 |
|
| 43 |
+
# Khởi chạy
|
| 44 |
+
demo.launch()
|