akclick1401 commited on
Commit
5f86598
·
verified ·
1 Parent(s): 1062805

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
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 đã nâng cấp lên GPU
 
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 Gradio
25
- interface = gr.Interface(
26
- fn=generate_image,
27
- inputs=[
28
- gr.Textbox(label="Prompt", placeholder="Enter your prompt here, e.g., 'A futuristic city at sunset'"),
29
- gr.Textbox(label="Negative Prompt (optional)", placeholder="Things to avoid, e.g., 'blurry, low quality'"),
30
- gr.Slider(label="Inference Steps", minimum=10, maximum=100, value=50, step=1),
31
- gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7.5, step=0.5)
32
- ],
33
- outputs=gr.Image(label="Generated Image"),
34
- title="Text-to-Image with Stable Diffusion",
35
- description="Enter a prompt to generate an image using Stable Diffusion."
36
- )
 
 
 
 
37
 
38
- # Khởi chạy giao diện
39
- interface.launch() # Xóa server_name và server_port
 
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()