Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from PIL import ImageOps
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def create_cereal_box(input_image):
|
| 7 |
# Convert the input numpy array to PIL Image
|
| 8 |
cover_img = Image.fromarray((input_image.astype(np.uint8)))
|
| 9 |
|
| 10 |
# Load the template image
|
| 11 |
-
template_img = Image.open('CerealBoxMaker/template.jpeg')
|
| 12 |
|
| 13 |
-
#
|
| 14 |
scaling_factor = 1.5
|
| 15 |
-
|
| 16 |
-
# Resize cover image
|
| 17 |
rect_height = int(template_img.height * 0.32)
|
| 18 |
new_width = int(rect_height * 0.70)
|
| 19 |
cover_resized = cover_img.resize((new_width, rect_height), Image.LANCZOS)
|
| 20 |
-
|
| 21 |
-
# Apply diagonal scaling
|
| 22 |
new_width_scaled = int(new_width * scaling_factor)
|
| 23 |
new_height_scaled = int(rect_height * scaling_factor)
|
| 24 |
cover_resized_scaled = cover_resized.resize((new_width_scaled, new_height_scaled), Image.LANCZOS)
|
| 25 |
-
|
| 26 |
-
# Positioning the resized cover image on the template
|
| 27 |
left_x = int(template_img.width * 0.085)
|
| 28 |
left_y = int((template_img.height - new_height_scaled) // 2 + template_img.height * 0.012)
|
| 29 |
left_position = (left_x, left_y)
|
| 30 |
-
|
| 31 |
right_x = int(template_img.width * 0.82) - new_width_scaled
|
| 32 |
right_y = left_y
|
| 33 |
right_position = (right_x, right_y)
|
| 34 |
-
|
| 35 |
-
# Create a copy of the template to paste on
|
| 36 |
template_copy = template_img.copy()
|
| 37 |
-
|
| 38 |
-
# Paste the resized and scaled cover image
|
| 39 |
template_copy.paste(cover_resized_scaled, left_position)
|
| 40 |
template_copy.paste(cover_resized_scaled, right_position)
|
| 41 |
|
|
@@ -44,11 +47,10 @@ def create_cereal_box(input_image):
|
|
| 44 |
|
| 45 |
return template_copy_array
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
chained_iface = gr.Interface(create_cereal_box, inputs=iface.outputs, outputs="image")
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
|
|
|
| 1 |
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 |
+
# Generate image using the DiffusionPipeline
|
| 13 |
+
output = pipeline(prompt)
|
| 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') # Replace with your actual path
|
| 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)
|
| 31 |
cover_resized = cover_img.resize((new_width, rect_height), Image.LANCZOS)
|
|
|
|
|
|
|
| 32 |
new_width_scaled = int(new_width * scaling_factor)
|
| 33 |
new_height_scaled = int(rect_height * scaling_factor)
|
| 34 |
cover_resized_scaled = cover_resized.resize((new_width_scaled, new_height_scaled), Image.LANCZOS)
|
|
|
|
|
|
|
| 35 |
left_x = int(template_img.width * 0.085)
|
| 36 |
left_y = int((template_img.height - new_height_scaled) // 2 + template_img.height * 0.012)
|
| 37 |
left_position = (left_x, left_y)
|
|
|
|
| 38 |
right_x = int(template_img.width * 0.82) - new_width_scaled
|
| 39 |
right_y = left_y
|
| 40 |
right_position = (right_x, right_y)
|
|
|
|
|
|
|
| 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 |
|
|
|
|
| 47 |
|
| 48 |
return template_copy_array
|
| 49 |
|
| 50 |
+
def combined_function(prompt):
|
| 51 |
+
generated_img_array = text_to_image(prompt)
|
| 52 |
+
final_img = create_cereal_box(generated_img_array)
|
| 53 |
+
return final_img
|
|
|
|
| 54 |
|
| 55 |
+
# Create a Gradio Interface
|
| 56 |
+
gr.Interface(fn=combined_function, inputs="text", outputs="image").launch()
|