Update app.py
Browse files
app.py
CHANGED
|
@@ -107,7 +107,8 @@ async def generate(
|
|
| 107 |
image2: Optional[UploadFile] = File(None),
|
| 108 |
user=Depends(verify_firebase_token)
|
| 109 |
):
|
| 110 |
-
"""Generate edited image using HF Inference (secured by Firebase Auth)."""
|
|
|
|
| 111 |
try:
|
| 112 |
img1_bytes = await image1.read()
|
| 113 |
pil_img1 = Image.open(io.BytesIO(img1_bytes)).convert("RGB")
|
|
@@ -123,11 +124,6 @@ async def generate(
|
|
| 123 |
except Exception as e:
|
| 124 |
raise HTTPException(status_code=400, detail=f"Failed to read second image: {e}")
|
| 125 |
|
| 126 |
-
input1_id = fs.put(img1_bytes, filename=image1.filename, contentType=image1.content_type, role="input")
|
| 127 |
-
input2_id = None
|
| 128 |
-
if pil_img2:
|
| 129 |
-
input2_id = fs.put(img2_bytes, filename=image2.filename, contentType=image2.content_type, role="input")
|
| 130 |
-
|
| 131 |
if pil_img2:
|
| 132 |
total_width = pil_img1.width + pil_img2.width
|
| 133 |
max_height = max(pil_img1.height, pil_img2.height)
|
|
@@ -152,26 +148,31 @@ async def generate(
|
|
| 152 |
|
| 153 |
out_id = fs.put(
|
| 154 |
out_bytes,
|
| 155 |
-
filename=f"result_{
|
| 156 |
contentType="image/png",
|
| 157 |
-
prompt=prompt,
|
| 158 |
role="output",
|
| 159 |
-
input1_id=str(input1_id),
|
| 160 |
-
input2_id=str(input2_id) if input2_id else None,
|
| 161 |
user_email=user.get("email"),
|
| 162 |
)
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
"user": user.get("email"),
|
|
|
|
| 171 |
})
|
| 172 |
|
| 173 |
-
return JSONResponse({"output_image_id": str(out_id), "user": user.get("email")})
|
| 174 |
-
|
| 175 |
|
| 176 |
@app.get("/image/{image_id}")
|
| 177 |
def get_image(image_id: str, download: Optional[bool] = False):
|
|
|
|
| 107 |
image2: Optional[UploadFile] = File(None),
|
| 108 |
user=Depends(verify_firebase_token)
|
| 109 |
):
|
| 110 |
+
"""Generate edited image using HF Inference (secured by Firebase Auth) and log only response time."""
|
| 111 |
+
start_time = datetime.utcnow() # Start time for response time logging
|
| 112 |
try:
|
| 113 |
img1_bytes = await image1.read()
|
| 114 |
pil_img1 = Image.open(io.BytesIO(img1_bytes)).convert("RGB")
|
|
|
|
| 124 |
except Exception as e:
|
| 125 |
raise HTTPException(status_code=400, detail=f"Failed to read second image: {e}")
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
if pil_img2:
|
| 128 |
total_width = pil_img1.width + pil_img2.width
|
| 129 |
max_height = max(pil_img1.height, pil_img2.height)
|
|
|
|
| 148 |
|
| 149 |
out_id = fs.put(
|
| 150 |
out_bytes,
|
| 151 |
+
filename=f"result_{datetime.utcnow().timestamp()}.png",
|
| 152 |
contentType="image/png",
|
|
|
|
| 153 |
role="output",
|
|
|
|
|
|
|
| 154 |
user_email=user.get("email"),
|
| 155 |
)
|
| 156 |
|
| 157 |
+
# Calculate response time in milliseconds
|
| 158 |
+
response_time_ms = (datetime.utcnow() - start_time).total_seconds() * 1000
|
| 159 |
+
|
| 160 |
+
# Log only response time
|
| 161 |
+
try:
|
| 162 |
+
logs_collection.insert_one({
|
| 163 |
+
"timestamp": datetime.utcnow(),
|
| 164 |
+
"user": user.get("email"),
|
| 165 |
+
"response_time_ms": response_time_ms
|
| 166 |
+
})
|
| 167 |
+
except Exception as e:
|
| 168 |
+
print("⚠️ Failed to log response time:", e)
|
| 169 |
+
|
| 170 |
+
return JSONResponse({
|
| 171 |
+
"output_image_id": str(out_id),
|
| 172 |
"user": user.get("email"),
|
| 173 |
+
"response_time_ms": response_time_ms
|
| 174 |
})
|
| 175 |
|
|
|
|
|
|
|
| 176 |
|
| 177 |
@app.get("/image/{image_id}")
|
| 178 |
def get_image(image_id: str, download: Optional[bool] = False):
|