Nawinkumar15 commited on
Commit
d38413b
·
verified ·
1 Parent(s): 6e0a583

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -2
app.py CHANGED
@@ -2,25 +2,29 @@ import gradio as gr
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,5 +36,6 @@ demo = gr.Interface(
32
  description="Generate high-quality, photorealistic images using Stable Diffusion v1.5 + DPM Scheduler"
33
  )
34
 
 
35
  if __name__ == "__main__":
36
- demo.queue().launch(max_concurrent=1)
 
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 Pipeline ---- #
12
  pipe = StableDiffusionPipeline.from_pretrained(
13
  MODEL_ID,
14
  torch_dtype=DTYPE
15
  ).to(DEVICE)
16
 
17
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
18
+ pipe.enable_attention_slicing()
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
  with torch.inference_mode():
24
  result = pipe(enhanced_prompt, num_inference_steps=25)
25
  return result.images[0]
26
 
27
+ # ---- Gradio UI ---- #
28
  demo = gr.Interface(
29
  fn=generate_image,
30
  inputs=gr.Textbox(
 
36
  description="Generate high-quality, photorealistic images using Stable Diffusion v1.5 + DPM Scheduler"
37
  )
38
 
39
+ # ---- Launch ---- #
40
  if __name__ == "__main__":
41
+ demo.queue().launch()