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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -20
app.py CHANGED
@@ -1,37 +1,40 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
3
  import torch
 
4
 
5
- # Load the model
6
- model_id = "runwayml/stable-diffusion-v1-5"
7
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
8
 
9
- # Load the pipeline
10
  pipe = StableDiffusionPipeline.from_pretrained(
11
- model_id,
12
- torch_dtype=torch.float16 if device == "cuda" else torch.float32
13
- )
14
-
15
- # ✅ Move to device
16
- pipe = pipe.to(device)
17
 
18
- # Change the scheduler *after* pipe is defined
19
  pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
20
 
21
- # Inference function
22
  def generate_image(prompt):
23
- enhanced_prompt = f"{prompt}, highly detailed, 8k, photorealistic, ultra-realistic lighting"
24
- image = pipe(enhanced_prompt).images[0]
25
- return image
26
 
27
- # Gradio UI
28
  demo = gr.Interface(
29
  fn=generate_image,
30
- inputs=gr.Textbox(label="Enter image description", placeholder="e.g. A magical tree in a glowing forest"),
 
 
 
31
  outputs=gr.Image(type="pil"),
32
- title="Text to Image Generator",
33
- description="Enter a prompt and generate a photorealistic image using Stable Diffusion"
34
  )
35
 
 
36
  if __name__ == "__main__":
37
  demo.launch()
 
1
  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(
30
+ label="Enter image description",
31
+ placeholder="e.g. A cozy cabin in a snowy forest"
32
+ ),
33
  outputs=gr.Image(type="pil"),
34
+ title="🖼️ Realistic Text-to-Image Generator",
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()