Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,6 +31,7 @@ app.add_middleware(
|
|
| 31 |
CORSMiddleware,
|
| 32 |
allow_origins=[
|
| 33 |
"https://hivili.web.app",
|
|
|
|
| 34 |
"http://localhost:3000",
|
| 35 |
"https://*.lovable.dev",
|
| 36 |
"https://*.sandbox.lovable.dev",
|
|
@@ -110,7 +111,7 @@ def validate_image(image_content: bytes):
|
|
| 110 |
raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
|
| 111 |
|
| 112 |
def generate_gemini_image(images: List[Image.Image], prompt: str):
|
| 113 |
-
"""Generate or edit image using Gemini API"""
|
| 114 |
try:
|
| 115 |
contents = images + [prompt]
|
| 116 |
response = genai.GenerativeModel(GEMINI_MODEL).generate_content(contents)
|
|
@@ -125,9 +126,15 @@ def generate_gemini_image(images: List[Image.Image], prompt: str):
|
|
| 125 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 126 |
temp_path = tmp.name
|
| 127 |
generated_image = Image.open(BytesIO(part.inline_data.data))
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
image_path = temp_path
|
| 130 |
-
logger.info(f"Generated image saved to: {temp_path} with prompt: {prompt}")
|
| 131 |
|
| 132 |
if image_path:
|
| 133 |
return image_path, ""
|
|
|
|
| 31 |
CORSMiddleware,
|
| 32 |
allow_origins=[
|
| 33 |
"https://hivili.web.app",
|
| 34 |
+
"https://hivili.com",
|
| 35 |
"http://localhost:3000",
|
| 36 |
"https://*.lovable.dev",
|
| 37 |
"https://*.sandbox.lovable.dev",
|
|
|
|
| 111 |
raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
|
| 112 |
|
| 113 |
def generate_gemini_image(images: List[Image.Image], prompt: str):
|
| 114 |
+
"""Generate or edit image using Gemini API and resize to 1024x1024 pixels"""
|
| 115 |
try:
|
| 116 |
contents = images + [prompt]
|
| 117 |
response = genai.GenerativeModel(GEMINI_MODEL).generate_content(contents)
|
|
|
|
| 126 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 127 |
temp_path = tmp.name
|
| 128 |
generated_image = Image.open(BytesIO(part.inline_data.data))
|
| 129 |
+
# Resize image to 1024x1024 pixels
|
| 130 |
+
generated_image = generated_image.resize((1024, 1024), Image.LANCZOS)
|
| 131 |
+
# Verify dimensions
|
| 132 |
+
if generated_image.size != (1024, 1024):
|
| 133 |
+
logger.error(f"Resized image dimensions {generated_image.size} do not match expected 1024x1024")
|
| 134 |
+
raise HTTPException(status_code=500, detail="Failed to resize image to 1024x1024")
|
| 135 |
+
generated_image.save(temp_path, format="PNG")
|
| 136 |
image_path = temp_path
|
| 137 |
+
logger.info(f"Generated and resized image to 1024x1024 saved to: {temp_path} with prompt: {prompt}")
|
| 138 |
|
| 139 |
if image_path:
|
| 140 |
return image_path, ""
|