AkashKumarave commited on
Commit
ca1a88e
·
verified ·
1 Parent(s): 3450986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
app.py CHANGED
@@ -8,6 +8,7 @@ import os
8
  from pathlib import Path
9
  from typing import List
10
  import io
 
11
  import razorpay
12
  from razorpay.errors import SignatureVerificationError
13
  from supabase import create_client, Client
@@ -76,30 +77,29 @@ def validate_image(image_content: bytes):
76
  size_mb = len(image_content) / (1024 * 1024)
77
  if size_mb > 10:
78
  raise HTTPException(status_code=400, detail="Image too large (max 10MB)")
 
 
 
79
  return True, ""
80
  except Exception as e:
81
  raise HTTPException(status_code=400, detail=f"Image validation error: {str(e)}")
82
 
83
  # ===== API FUNCTIONS =====
84
  def create_multi_image_task(subject_images: List[bytes], prompt: str):
85
- """Create multi-image generation task with Gemini API"""
86
  headers = {
87
  "Content-Type": "application/json"
88
  }
89
  subject_image_list = []
90
- for img_content in subject_images:
91
- if img_content:
92
- base64_img = prepare_image_base64(img_content)
93
- if base64_img:
94
- subject_image_list.append({
95
- "inline_data": {
96
- "mime_type": "image/png",
97
- "data": base64_img
98
- }
99
- })
100
-
101
- if len(subject_image_list) < 2:
102
- raise HTTPException(status_code=400, detail="At least 2 subject images required")
103
 
104
  payload = {
105
  "contents": [
@@ -136,6 +136,13 @@ async def generate_image(subject_images: List[bytes], prompt: str):
136
 
137
  task_response = create_multi_image_task(subject_images, prompt)
138
  try:
 
 
 
 
 
 
 
139
  # Check response structure
140
  if "candidates" not in task_response or not task_response["candidates"]:
141
  logger.error(f"Invalid response structure: {task_response}")
@@ -147,12 +154,15 @@ async def generate_image(subject_images: List[bytes], prompt: str):
147
  raise HTTPException(status_code=500, detail="Invalid API response: No content parts found")
148
 
149
  parts = candidate["content"]["parts"]
 
150
  # Find the part with inline_data
151
  image_base64 = None
152
  for part in parts:
153
  if "inline_data" in part and "data" in part["inline_data"]:
154
  image_base64 = part["inline_data"]["data"]
155
  break
 
 
156
 
157
  if not image_base64:
158
  logger.error(f"No inline_data found in response parts: {parts}")
@@ -174,14 +184,12 @@ async def generate_image(subject_images: List[bytes], prompt: str):
174
  @app.post("/generate")
175
  async def generate_image_endpoint(
176
  prompt: str = Form(...),
177
- images: List[UploadFile] = File(...)
178
  ):
179
- """Endpoint to generate an image from multiple input images and a prompt"""
180
  try:
181
- if len(images) < 2:
182
- raise HTTPException(status_code=400, detail="At least 2 images are required")
183
- if len(images) > 4:
184
- raise HTTPException(status_code=400, detail="Maximum 4 images allowed")
185
  image_contents = [await image.read() for image in images]
186
  output_path = await generate_image(image_contents, prompt)
187
  return FileResponse(
 
8
  from pathlib import Path
9
  from typing import List
10
  import io
11
+ from PIL import Image
12
  import razorpay
13
  from razorpay.errors import SignatureVerificationError
14
  from supabase import create_client, Client
 
77
  size_mb = len(image_content) / (1024 * 1024)
78
  if size_mb > 10:
79
  raise HTTPException(status_code=400, detail="Image too large (max 10MB)")
80
+ img = Image.open(io.BytesIO(image_content))
81
+ if img.format != "PNG":
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 (single image or prompt-only)"""
90
  headers = {
91
  "Content-Type": "application/json"
92
  }
93
  subject_image_list = []
94
+ if subject_images: # Use only the first image if provided
95
+ base64_img = prepare_image_base64(subject_images[0])
96
+ if base64_img:
97
+ subject_image_list.append({
98
+ "inline_data": {
99
+ "mime_type": "image/png",
100
+ "data": base64_img
101
+ }
102
+ })
 
 
 
 
103
 
104
  payload = {
105
  "contents": [
 
136
 
137
  task_response = create_multi_image_task(subject_images, prompt)
138
  try:
139
+ # Check for error in response
140
+ if "error" in task_response:
141
+ error_message = task_response["error"].get("message", "Unknown error")
142
+ error_code = task_response["error"].get("code", 500)
143
+ logger.error(f"API returned error: {error_code} - {error_message}")
144
+ raise HTTPException(status_code=500, detail=f"API error: {error_code} - {error_message}")
145
+
146
  # Check response structure
147
  if "candidates" not in task_response or not task_response["candidates"]:
148
  logger.error(f"Invalid response structure: {task_response}")
 
154
  raise HTTPException(status_code=500, detail="Invalid API response: No content parts found")
155
 
156
  parts = candidate["content"]["parts"]
157
+ logger.info(f"Response parts: {parts}")
158
  # Find the part with inline_data
159
  image_base64 = None
160
  for part in parts:
161
  if "inline_data" in part and "data" in part["inline_data"]:
162
  image_base64 = part["inline_data"]["data"]
163
  break
164
+ elif "text" in part:
165
+ logger.info(f"Text part found: {part['text']}")
166
 
167
  if not image_base64:
168
  logger.error(f"No inline_data found in response parts: {parts}")
 
184
  @app.post("/generate")
185
  async def generate_image_endpoint(
186
  prompt: str = Form(...),
187
+ images: List[UploadFile] = File(default=[])
188
  ):
189
+ """Endpoint to generate an image from an optional input image and a prompt"""
190
  try:
191
+ if len(images) > 1:
192
+ raise HTTPException(status_code=400, detail="Only one image is supported")
 
 
193
  image_contents = [await image.read() for image in images]
194
  output_path = await generate_image(image_contents, prompt)
195
  return FileResponse(