Update app.py
Browse files
app.py
CHANGED
|
@@ -91,6 +91,23 @@ class HealthResponse(BaseModel):
|
|
| 91 |
db: str
|
| 92 |
model: str
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
# ---------------------------------------------------------------------
|
| 95 |
# Endpoints
|
| 96 |
# ---------------------------------------------------------------------
|
|
@@ -113,7 +130,7 @@ async def generate(
|
|
| 113 |
# PREPARE IMAGE(S)
|
| 114 |
try:
|
| 115 |
img1_bytes = await image1.read()
|
| 116 |
-
pil_img1 =
|
| 117 |
input1_id = fs.put(
|
| 118 |
img1_bytes,
|
| 119 |
filename=image1.filename,
|
|
@@ -130,7 +147,7 @@ async def generate(
|
|
| 130 |
if image2:
|
| 131 |
try:
|
| 132 |
img2_bytes = await image2.read()
|
| 133 |
-
pil_img2 =
|
| 134 |
input2_id = fs.put(
|
| 135 |
img2_bytes,
|
| 136 |
filename=image2.filename,
|
|
|
|
| 91 |
db: str
|
| 92 |
model: str
|
| 93 |
|
| 94 |
+
# --------------------- UTILS ---------------------
|
| 95 |
+
def resize_image_if_needed(img: Image.Image, max_size=(1024, 1024)) -> Image.Image:
|
| 96 |
+
"""
|
| 97 |
+
Resize image to fit within max_size while keeping aspect ratio.
|
| 98 |
+
"""
|
| 99 |
+
if img.width > max_size[0] or img.height > max_size[1]:
|
| 100 |
+
img.thumbnail(max_size, Image.ANTIALIAS)
|
| 101 |
+
return img
|
| 102 |
+
|
| 103 |
+
def prepare_image(file_bytes: bytes) -> Image.Image:
|
| 104 |
+
"""
|
| 105 |
+
Open image and resize if larger than 1024x1024
|
| 106 |
+
"""
|
| 107 |
+
img = Image.open(io.BytesIO(file_bytes)).convert("RGB")
|
| 108 |
+
img = resize_image_if_needed(img, max_size=(1024, 1024))
|
| 109 |
+
return img
|
| 110 |
+
|
| 111 |
# ---------------------------------------------------------------------
|
| 112 |
# Endpoints
|
| 113 |
# ---------------------------------------------------------------------
|
|
|
|
| 130 |
# PREPARE IMAGE(S)
|
| 131 |
try:
|
| 132 |
img1_bytes = await image1.read()
|
| 133 |
+
pil_img1 = prepare_image(img1_bytes)
|
| 134 |
input1_id = fs.put(
|
| 135 |
img1_bytes,
|
| 136 |
filename=image1.filename,
|
|
|
|
| 147 |
if image2:
|
| 148 |
try:
|
| 149 |
img2_bytes = await image2.read()
|
| 150 |
+
pil_img2 = prepare_image(img2_bytes)
|
| 151 |
input2_id = fs.put(
|
| 152 |
img2_bytes,
|
| 153 |
filename=image2.filename,
|