Nawinkumar15 commited on
Commit
dc83634
·
verified ·
1 Parent(s): 3bfcbfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -2,26 +2,33 @@ import gradio as gr
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
- # Correct model ID
6
  model_id = "runwayml/stable-diffusion-v1-5"
7
 
8
- # ✅ Removed the bad 'revision' argument
 
 
 
9
  pipe = StableDiffusionPipeline.from_pretrained(
10
  model_id,
11
- torch_dtype=torch.float16,
12
- use_auth_token=True # Optional on Hugging Face Spaces
13
- ).to("cuda")
 
 
14
 
 
15
  def generate_image(prompt):
16
  image = pipe(prompt).images[0]
17
  return image
18
 
 
19
  demo = gr.Interface(
20
  fn=generate_image,
21
- inputs=gr.Textbox(label="Enter image description", placeholder="e.g. A fantasy tree in the fog"),
22
  outputs=gr.Image(type="pil"),
23
  title="Text to Image Generator",
24
- description="Enter a description to generate an image using Stable Diffusion"
25
  )
26
 
27
  if __name__ == "__main__":
 
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
 
5
+ # Load the model
6
  model_id = "runwayml/stable-diffusion-v1-5"
7
 
8
+ # ✅ Detect whether GPU is available
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+
11
+ # ✅ Load pipeline without 'use_auth_token'
12
  pipe = StableDiffusionPipeline.from_pretrained(
13
  model_id,
14
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32
15
+ )
16
+
17
+ # ✅ Move model to correct device
18
+ pipe = pipe.to(device)
19
 
20
+ # Inference function
21
  def generate_image(prompt):
22
  image = pipe(prompt).images[0]
23
  return image
24
 
25
+ # UI
26
  demo = gr.Interface(
27
  fn=generate_image,
28
+ inputs=gr.Textbox(label="Enter image description", placeholder="e.g. A magical tree in a glowing forest"),
29
  outputs=gr.Image(type="pil"),
30
  title="Text to Image Generator",
31
+ description="Enter a prompt and generate an image using Stable Diffusion"
32
  )
33
 
34
  if __name__ == "__main__":