Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,29 +2,37 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
|
| 7 |
# Initialize the DiffusionPipeline model with LoRA weights
|
| 8 |
pipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
|
| 9 |
pipeline.load_lora_weights("ostris/super-cereal-sdxl-lora")
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def text_to_image(prompt):
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
generated_img_tensor = output.images[0]
|
| 15 |
-
|
| 16 |
-
# Convert torch tensor to numpy array
|
| 17 |
generated_img_array = generated_img_tensor.cpu().numpy().transpose((1, 2, 0))
|
| 18 |
return generated_img_array
|
| 19 |
|
| 20 |
def create_cereal_box(input_image):
|
| 21 |
-
# Convert the input numpy array to PIL Image
|
| 22 |
cover_img = Image.fromarray((input_image.astype(np.uint8)))
|
| 23 |
-
|
| 24 |
-
# Load the template image
|
| 25 |
-
template_img = Image.open('CerealBoxMaker/template.jpeg')
|
| 26 |
-
|
| 27 |
-
# Simplified cereal box creation logic
|
| 28 |
scaling_factor = 1.5
|
| 29 |
rect_height = int(template_img.height * 0.32)
|
| 30 |
new_width = int(rect_height * 0.70)
|
|
@@ -41,10 +49,7 @@ def create_cereal_box(input_image):
|
|
| 41 |
template_copy = template_img.copy()
|
| 42 |
template_copy.paste(cover_resized_scaled, left_position)
|
| 43 |
template_copy.paste(cover_resized_scaled, right_position)
|
| 44 |
-
|
| 45 |
-
# Convert the PIL Image back to a numpy array
|
| 46 |
template_copy_array = np.array(template_copy)
|
| 47 |
-
|
| 48 |
return template_copy_array
|
| 49 |
|
| 50 |
def combined_function(prompt):
|
|
@@ -53,4 +58,4 @@ def combined_function(prompt):
|
|
| 53 |
return final_img
|
| 54 |
|
| 55 |
# Create a Gradio Interface
|
| 56 |
-
gr.Interface(fn=combined_function, inputs="text", outputs="image").launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import random
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
|
| 8 |
# Initialize the DiffusionPipeline model with LoRA weights
|
| 9 |
pipeline = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0")
|
| 10 |
pipeline.load_lora_weights("ostris/super-cereal-sdxl-lora")
|
| 11 |
+
pipeline.to("cuda:0")
|
| 12 |
+
|
| 13 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 14 |
|
| 15 |
def text_to_image(prompt):
|
| 16 |
+
seed = random.randint(0, MAX_SEED)
|
| 17 |
+
negative_prompt = "ugly, blurry, nsfw, gore, blood"
|
| 18 |
+
|
| 19 |
+
output = pipeline(
|
| 20 |
+
prompt=prompt,
|
| 21 |
+
negative_prompt=negative_prompt,
|
| 22 |
+
width=1024,
|
| 23 |
+
height=1024,
|
| 24 |
+
guidance_scale=7.0,
|
| 25 |
+
num_inference_steps=25,
|
| 26 |
+
generator=torch.Generator().manual_seed(seed),
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
generated_img_tensor = output.images[0]
|
|
|
|
|
|
|
| 30 |
generated_img_array = generated_img_tensor.cpu().numpy().transpose((1, 2, 0))
|
| 31 |
return generated_img_array
|
| 32 |
|
| 33 |
def create_cereal_box(input_image):
|
|
|
|
| 34 |
cover_img = Image.fromarray((input_image.astype(np.uint8)))
|
| 35 |
+
template_img = Image.open('CerealBoxMaker/template.jpeg')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
scaling_factor = 1.5
|
| 37 |
rect_height = int(template_img.height * 0.32)
|
| 38 |
new_width = int(rect_height * 0.70)
|
|
|
|
| 49 |
template_copy = template_img.copy()
|
| 50 |
template_copy.paste(cover_resized_scaled, left_position)
|
| 51 |
template_copy.paste(cover_resized_scaled, right_position)
|
|
|
|
|
|
|
| 52 |
template_copy_array = np.array(template_copy)
|
|
|
|
| 53 |
return template_copy_array
|
| 54 |
|
| 55 |
def combined_function(prompt):
|
|
|
|
| 58 |
return final_img
|
| 59 |
|
| 60 |
# Create a Gradio Interface
|
| 61 |
+
gr.Interface(fn=combined_function, inputs="text", outputs="image").launch(debug=True)
|