Spaces:
Sleeping
Sleeping
fix: ensure output is PIL image
#3
by
ferrymangmi - opened
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from PIL import Image
|
|
| 3 |
import torch
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model_name = "Yaquv/rickthenpc"
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
|
@@ -11,46 +11,54 @@ try:
|
|
| 11 |
pipe = StableDiffusionPipeline.from_pretrained(model_name)
|
| 12 |
pipe = pipe.to(device)
|
| 13 |
except Exception as e:
|
| 14 |
-
print(f"
|
| 15 |
pipe = None
|
| 16 |
|
| 17 |
-
# Fonction de génération et de post-traitement
|
| 18 |
def generate_image(prompt):
|
| 19 |
"""
|
| 20 |
-
|
| 21 |
"""
|
| 22 |
if pipe is None:
|
| 23 |
raise ValueError("The model couldn't be loaded.")
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
result = pipe(prompt)
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
if not hasattr(result, 'images') or len(result.images) == 0:
|
| 31 |
raise ValueError("The model couldn't generate an image.")
|
| 32 |
|
| 33 |
image = result.images[0]
|
| 34 |
|
| 35 |
-
#
|
| 36 |
if not isinstance(image, Image.Image):
|
| 37 |
image = Image.fromarray(image)
|
| 38 |
|
|
|
|
|
|
|
| 39 |
return image
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
-
#
|
| 43 |
-
raise ValueError(f"
|
| 44 |
|
| 45 |
-
#
|
| 46 |
iface = gr.Interface(
|
| 47 |
fn=generate_image,
|
| 48 |
-
inputs=gr.Textbox(
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
title="Rick Generator",
|
| 51 |
description="Enter a prompt to generate an image with the Rick Generator model."
|
| 52 |
)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
if __name__ == "__main__":
|
| 56 |
iface.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
|
| 6 |
+
# Load the diffusion pipeline from Hugging Face
|
| 7 |
model_name = "Yaquv/rickthenpc"
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
|
|
|
| 11 |
pipe = StableDiffusionPipeline.from_pretrained(model_name)
|
| 12 |
pipe = pipe.to(device)
|
| 13 |
except Exception as e:
|
| 14 |
+
print(f"Error loading the model: {e}")
|
| 15 |
pipe = None
|
| 16 |
|
|
|
|
| 17 |
def generate_image(prompt):
|
| 18 |
"""
|
| 19 |
+
Generates an image from the given prompt using the Hugging Face model.
|
| 20 |
"""
|
| 21 |
if pipe is None:
|
| 22 |
raise ValueError("The model couldn't be loaded.")
|
| 23 |
|
| 24 |
try:
|
| 25 |
+
# Generate the image
|
| 26 |
result = pipe(prompt)
|
| 27 |
+
|
| 28 |
+
# Check that the result contains images
|
| 29 |
if not hasattr(result, 'images') or len(result.images) == 0:
|
| 30 |
raise ValueError("The model couldn't generate an image.")
|
| 31 |
|
| 32 |
image = result.images[0]
|
| 33 |
|
| 34 |
+
# Ensure the image is in PIL.Image format and convert to RGB
|
| 35 |
if not isinstance(image, Image.Image):
|
| 36 |
image = Image.fromarray(image)
|
| 37 |
|
| 38 |
+
image = image.convert("RGB")
|
| 39 |
+
|
| 40 |
return image
|
| 41 |
|
| 42 |
except Exception as e:
|
| 43 |
+
# Raise an exception for Gradio to handle
|
| 44 |
+
raise ValueError(f"Error during image generation: {str(e)}")
|
| 45 |
|
| 46 |
+
# Define the Gradio Interface
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=generate_image,
|
| 49 |
+
inputs=gr.Textbox(
|
| 50 |
+
label="Prompt",
|
| 51 |
+
lines=2,
|
| 52 |
+
placeholder="Enter your prompt here..."
|
| 53 |
+
),
|
| 54 |
+
outputs=gr.Image(
|
| 55 |
+
label="Generated Image",
|
| 56 |
+
type="pil" # Ensure the output is a PIL Image
|
| 57 |
+
),
|
| 58 |
title="Rick Generator",
|
| 59 |
description="Enter a prompt to generate an image with the Rick Generator model."
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Launch the Gradio app
|
| 63 |
if __name__ == "__main__":
|
| 64 |
iface.launch()
|