Nawinkumar15 commited on
Commit
8fd6d99
·
verified ·
1 Parent(s): 1ccd9e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -8
app.py CHANGED
@@ -2,28 +2,25 @@ import gradio as gr
2
  import torch
3
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
4
 
5
- # ---- Configuration ---- #
6
  MODEL_ID = "runwayml/stable-diffusion-v1-5"
7
  USE_CUDA = torch.cuda.is_available()
8
  DEVICE = "cuda" if USE_CUDA else "cpu"
9
  DTYPE = torch.float16 if USE_CUDA else torch.float32
10
 
11
- # ---- Load Stable Diffusion Pipeline ---- #
12
  pipe = StableDiffusionPipeline.from_pretrained(
13
  MODEL_ID,
14
  torch_dtype=DTYPE
15
  ).to(DEVICE)
16
 
17
- # ---- Swap the Scheduler for Better Quality ---- #
18
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
 
19
 
20
- # ---- Image Generation Function ---- #
21
  def generate_image(prompt):
22
  enhanced_prompt = f"{prompt}, ultra realistic, high detail, 8k resolution, DSLR photography, natural lighting"
23
- result = pipe(enhanced_prompt)
 
24
  return result.images[0]
25
 
26
- # ---- Gradio Interface ---- #
27
  demo = gr.Interface(
28
  fn=generate_image,
29
  inputs=gr.Textbox(
@@ -35,6 +32,5 @@ demo = gr.Interface(
35
  description="Generate high-quality, photorealistic images using Stable Diffusion v1.5 + DPM Scheduler"
36
  )
37
 
38
- # ---- Launch App ---- #
39
  if __name__ == "__main__":
40
- demo.launch()
 
2
  import torch
3
  from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
4
 
 
5
  MODEL_ID = "runwayml/stable-diffusion-v1-5"
6
  USE_CUDA = torch.cuda.is_available()
7
  DEVICE = "cuda" if USE_CUDA else "cpu"
8
  DTYPE = torch.float16 if USE_CUDA else torch.float32
9
 
 
10
  pipe = StableDiffusionPipeline.from_pretrained(
11
  MODEL_ID,
12
  torch_dtype=DTYPE
13
  ).to(DEVICE)
14
 
 
15
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
16
+ pipe.enable_attention_slicing() # Helps with memory optimization
17
 
 
18
  def generate_image(prompt):
19
  enhanced_prompt = f"{prompt}, ultra realistic, high detail, 8k resolution, DSLR photography, natural lighting"
20
+ with torch.inference_mode():
21
+ result = pipe(enhanced_prompt, num_inference_steps=25)
22
  return result.images[0]
23
 
 
24
  demo = gr.Interface(
25
  fn=generate_image,
26
  inputs=gr.Textbox(
 
32
  description="Generate high-quality, photorealistic images using Stable Diffusion v1.5 + DPM Scheduler"
33
  )
34
 
 
35
  if __name__ == "__main__":
36
+ demo.queue(concurrency_count=1).launch()