Teddyoloo-2 commited on
Commit
f5e6732
·
verified ·
1 Parent(s): 5e2fab2

Fix error handling and add image Resizing for stable diffusion upscaler

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -1,32 +1,36 @@
1
  import gradio as gr
2
  from PIL import Image
3
- import requests
4
- from io import BytesIO
5
-
6
  from diffusers import StableDiffusionUpscalePipeline
7
 
8
- # Load the upscaling pipeline (this may take some seconds at first run)
9
- pipe = StableDiffusionUpscalePipeline.from_pretrained("stabilityai/stable-diffusion-x4-upscaler")
10
- pipe = pipe.to("cpu") # use cpu, can switch to "cuda" if GPU available
 
 
 
 
11
 
12
  def upscale_image(input_image):
13
- # input_image is a PIL image from Gradio uploader
14
- if input_image is None:
15
- return None
16
- # Run the upscaler pipeline
17
- upscaled = pipe(input_image).images[0]
18
- return upscaled
19
 
20
- title = "Free AI Image Upscaler"
21
- description = "Upload an image, and this app will upscale it 4x using Stable Diffusion upscaler."
 
 
 
22
 
23
  iface = gr.Interface(
24
  fn=upscale_image,
25
  inputs=gr.Image(type="pil"),
26
- outputs=gr.Image(type="pil"),
27
- title=title,
28
- description=description,
29
- allow_flagging="never"
30
  )
31
 
32
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from PIL import Image
3
+ import torch
 
 
4
  from diffusers import StableDiffusionUpscalePipeline
5
 
6
+ # Load the pipeline
7
+ pipe = StableDiffusionUpscalePipeline.from_pretrained(
8
+ "stabilityai/stable-diffusion-x4-upscaler",
9
+ revision="fp16",
10
+ torch_dtype=torch.float16
11
+ )
12
+ pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
13
 
14
  def upscale_image(input_image):
15
+ try:
16
+ if input_image is None:
17
+ return "Please upload an image."
18
+
19
+ # Resize image to match what the model expects
20
+ input_image = input_image.resize((512, 512)) # model requires 512x512 input
21
 
22
+ # Run the model with a default prompt
23
+ result = pipe(prompt="a high quality image", image=input_image).images[0]
24
+ return result
25
+ except Exception as e:
26
+ return f"Error: {str(e)}"
27
 
28
  iface = gr.Interface(
29
  fn=upscale_image,
30
  inputs=gr.Image(type="pil"),
31
+ outputs="image",
32
+ title="Free AI Image Upscaler",
33
+ description="Upload an image to upscale using Stable Diffusion x4 Upscaler."
 
34
  )
35
 
36
  if __name__ == "__main__":