Spaces:
Sleeping
Sleeping
Fix error handling and add image Resizing for stable diffusion upscaler
Browse files
app.py
CHANGED
|
@@ -1,32 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
-
from io import BytesIO
|
| 5 |
-
|
| 6 |
from diffusers import StableDiffusionUpscalePipeline
|
| 7 |
|
| 8 |
-
# Load the
|
| 9 |
-
pipe = StableDiffusionUpscalePipeline.from_pretrained(
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def upscale_image(input_image):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=upscale_image,
|
| 25 |
inputs=gr.Image(type="pil"),
|
| 26 |
-
outputs=
|
| 27 |
-
title=
|
| 28 |
-
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__":
|