Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,7 @@ from razorpay.errors import SignatureVerificationError
|
|
| 14 |
from supabase import create_client, Client
|
| 15 |
from pydantic import BaseModel
|
| 16 |
from typing import Optional
|
|
|
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -75,28 +76,29 @@ def validate_image(image_content: bytes):
|
|
| 75 |
"""Validate image meets API requirements"""
|
| 76 |
try:
|
| 77 |
size_mb = len(image_content) / (1024 * 1024)
|
| 78 |
-
if size_mb >
|
| 79 |
-
raise HTTPException(status_code=400, detail="Image too large (max
|
| 80 |
img = Image.open(io.BytesIO(image_content))
|
| 81 |
-
if img.format
|
| 82 |
-
raise HTTPException(status_code=400, detail="Only PNG images are supported")
|
| 83 |
-
return True,
|
| 84 |
except Exception as e:
|
| 85 |
raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
|
| 86 |
|
| 87 |
# ===== API FUNCTIONS =====
|
| 88 |
def create_multi_image_task(subject_images: List[bytes], prompt: str):
|
| 89 |
-
"""Create image generation task with Gemini API (
|
| 90 |
headers = {
|
| 91 |
"Content-Type": "application/json"
|
| 92 |
}
|
| 93 |
subject_image_list = []
|
| 94 |
-
|
| 95 |
-
|
|
|
|
| 96 |
if base64_img:
|
| 97 |
subject_image_list.append({
|
| 98 |
"inline_data": {
|
| 99 |
-
"mime_type": "image/
|
| 100 |
"data": base64_img
|
| 101 |
}
|
| 102 |
})
|
|
@@ -130,9 +132,11 @@ def create_multi_image_task(subject_images: List[bytes], prompt: str):
|
|
| 130 |
# ===== MAIN PROCESSING =====
|
| 131 |
async def generate_image(subject_images: List[bytes], prompt: str):
|
| 132 |
"""Handle complete image generation workflow"""
|
|
|
|
|
|
|
|
|
|
| 133 |
for img_content in subject_images:
|
| 134 |
-
|
| 135 |
-
validate_image(img_content)
|
| 136 |
|
| 137 |
task_response = create_multi_image_task(subject_images, prompt)
|
| 138 |
try:
|
|
@@ -184,12 +188,12 @@ async def generate_image(subject_images: List[bytes], prompt: str):
|
|
| 184 |
@app.post("/generate")
|
| 185 |
async def generate_image_endpoint(
|
| 186 |
prompt: str = Form(...),
|
| 187 |
-
images: List[UploadFile] = File(
|
| 188 |
):
|
| 189 |
-
"""Endpoint to generate an image from
|
| 190 |
try:
|
| 191 |
-
if len(images)
|
| 192 |
-
raise HTTPException(status_code=400, detail="
|
| 193 |
image_contents = [await image.read() for image in images]
|
| 194 |
output_path = await generate_image(image_contents, prompt)
|
| 195 |
return FileResponse(
|
|
|
|
| 14 |
from supabase import create_client, Client
|
| 15 |
from pydantic import BaseModel
|
| 16 |
from typing import Optional
|
| 17 |
+
import time
|
| 18 |
|
| 19 |
# Configure logging
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 76 |
"""Validate image meets API requirements"""
|
| 77 |
try:
|
| 78 |
size_mb = len(image_content) / (1024 * 1024)
|
| 79 |
+
if size_mb > 20:
|
| 80 |
+
raise HTTPException(status_code=400, detail="Image too large (max 20MB)")
|
| 81 |
img = Image.open(io.BytesIO(image_content))
|
| 82 |
+
if img.format not in ["PNG", "JPEG"]:
|
| 83 |
+
raise HTTPException(status_code=400, detail="Only PNG or JPEG images are supported")
|
| 84 |
+
return True, img.format.lower()
|
| 85 |
except Exception as e:
|
| 86 |
raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
|
| 87 |
|
| 88 |
# ===== API FUNCTIONS =====
|
| 89 |
def create_multi_image_task(subject_images: List[bytes], prompt: str):
|
| 90 |
+
"""Create image generation task with Gemini API (exactly two images)"""
|
| 91 |
headers = {
|
| 92 |
"Content-Type": "application/json"
|
| 93 |
}
|
| 94 |
subject_image_list = []
|
| 95 |
+
for idx, img_content in enumerate(subject_images):
|
| 96 |
+
_, img_format = validate_image(img_content)
|
| 97 |
+
base64_img = prepare_image_base64(img_content)
|
| 98 |
if base64_img:
|
| 99 |
subject_image_list.append({
|
| 100 |
"inline_data": {
|
| 101 |
+
"mime_type": f"image/{img_format}",
|
| 102 |
"data": base64_img
|
| 103 |
}
|
| 104 |
})
|
|
|
|
| 132 |
# ===== MAIN PROCESSING =====
|
| 133 |
async def generate_image(subject_images: List[bytes], prompt: str):
|
| 134 |
"""Handle complete image generation workflow"""
|
| 135 |
+
if len(subject_images) != 2:
|
| 136 |
+
raise HTTPException(status_code=400, detail="Exactly two images are required")
|
| 137 |
+
|
| 138 |
for img_content in subject_images:
|
| 139 |
+
validate_image(img_content)
|
|
|
|
| 140 |
|
| 141 |
task_response = create_multi_image_task(subject_images, prompt)
|
| 142 |
try:
|
|
|
|
| 188 |
@app.post("/generate")
|
| 189 |
async def generate_image_endpoint(
|
| 190 |
prompt: str = Form(...),
|
| 191 |
+
images: List[UploadFile] = File(...)
|
| 192 |
):
|
| 193 |
+
"""Endpoint to generate an image from exactly two input images and a prompt"""
|
| 194 |
try:
|
| 195 |
+
if len(images) != 2:
|
| 196 |
+
raise HTTPException(status_code=400, detail="Exactly two images are required")
|
| 197 |
image_contents = [await image.read() for image in images]
|
| 198 |
output_path = await generate_image(image_contents, prompt)
|
| 199 |
return FileResponse(
|