Update app.py
Browse files
app.py
CHANGED
|
@@ -74,13 +74,18 @@ async def generate(
|
|
| 74 |
image2: Optional[UploadFile] = File(None),
|
| 75 |
):
|
| 76 |
"""Upload 1 or 2 images + prompt and get edited image via HF Inference"""
|
| 77 |
-
#
|
|
|
|
|
|
|
| 78 |
try:
|
| 79 |
img1_bytes = await image1.read()
|
| 80 |
pil_img1 = Image.open(io.BytesIO(img1_bytes)).convert("RGB")
|
| 81 |
except Exception as e:
|
| 82 |
raise HTTPException(status_code=400, detail=f"Failed to read first image: {e}")
|
| 83 |
|
|
|
|
|
|
|
|
|
|
| 84 |
pil_img2 = None
|
| 85 |
img2_bytes = None
|
| 86 |
if image2:
|
|
@@ -91,7 +96,9 @@ async def generate(
|
|
| 91 |
except Exception as e:
|
| 92 |
raise HTTPException(status_code=400, detail=f"Failed to read second image: {e}")
|
| 93 |
|
| 94 |
-
#
|
|
|
|
|
|
|
| 95 |
try:
|
| 96 |
input1_id = fs.put(img1_bytes, filename=image1.filename, contentType=image1.content_type, role="input")
|
| 97 |
input2_id = None
|
|
@@ -100,28 +107,39 @@ async def generate(
|
|
| 100 |
except Exception as e:
|
| 101 |
raise HTTPException(status_code=500, detail=f"Failed saving input images: {e}")
|
| 102 |
|
| 103 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
try:
|
| 105 |
-
images_to_pass = [pil_img1]
|
| 106 |
-
if pil_img2:
|
| 107 |
-
images_to_pass.append(pil_img2)
|
| 108 |
-
|
| 109 |
pil_output = hf_client.image_to_image(
|
| 110 |
-
|
| 111 |
prompt=prompt,
|
| 112 |
model="Qwen/Qwen-Image-Edit"
|
| 113 |
)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
out_buf = io.BytesIO()
|
| 118 |
-
|
| 119 |
out_bytes = out_buf.getvalue()
|
| 120 |
except Exception as e:
|
| 121 |
traceback.print_exc()
|
| 122 |
raise HTTPException(status_code=500, detail=f"Inference failed: {e}")
|
| 123 |
|
| 124 |
-
#
|
|
|
|
|
|
|
| 125 |
try:
|
| 126 |
out_id = fs.put(
|
| 127 |
out_bytes,
|
|
@@ -135,7 +153,9 @@ async def generate(
|
|
| 135 |
except Exception as e:
|
| 136 |
raise HTTPException(status_code=500, detail=f"Failed saving output image: {e}")
|
| 137 |
|
| 138 |
-
#
|
|
|
|
|
|
|
| 139 |
try:
|
| 140 |
logs_collection.insert_one({
|
| 141 |
"timestamp": datetime.utcnow(),
|
|
@@ -150,6 +170,7 @@ async def generate(
|
|
| 150 |
return JSONResponse({"output_image_id": str(out_id)})
|
| 151 |
|
| 152 |
|
|
|
|
| 153 |
@app.get("/image/{image_id}")
|
| 154 |
def get_image(image_id: str, download: Optional[bool] = False):
|
| 155 |
"""Retrieve image by GridFS ID"""
|
|
|
|
| 74 |
image2: Optional[UploadFile] = File(None),
|
| 75 |
):
|
| 76 |
"""Upload 1 or 2 images + prompt and get edited image via HF Inference"""
|
| 77 |
+
# -----------------------------
|
| 78 |
+
# 1. Read first image
|
| 79 |
+
# -----------------------------
|
| 80 |
try:
|
| 81 |
img1_bytes = await image1.read()
|
| 82 |
pil_img1 = Image.open(io.BytesIO(img1_bytes)).convert("RGB")
|
| 83 |
except Exception as e:
|
| 84 |
raise HTTPException(status_code=400, detail=f"Failed to read first image: {e}")
|
| 85 |
|
| 86 |
+
# -----------------------------
|
| 87 |
+
# 2. Read second image if provided
|
| 88 |
+
# -----------------------------
|
| 89 |
pil_img2 = None
|
| 90 |
img2_bytes = None
|
| 91 |
if image2:
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
raise HTTPException(status_code=400, detail=f"Failed to read second image: {e}")
|
| 98 |
|
| 99 |
+
# -----------------------------
|
| 100 |
+
# 3. Save input images to GridFS
|
| 101 |
+
# -----------------------------
|
| 102 |
try:
|
| 103 |
input1_id = fs.put(img1_bytes, filename=image1.filename, contentType=image1.content_type, role="input")
|
| 104 |
input2_id = None
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
raise HTTPException(status_code=500, detail=f"Failed saving input images: {e}")
|
| 109 |
|
| 110 |
+
# -----------------------------
|
| 111 |
+
# 4. Combine images if two
|
| 112 |
+
# -----------------------------
|
| 113 |
+
if pil_img2:
|
| 114 |
+
total_width = pil_img1.width + pil_img2.width
|
| 115 |
+
max_height = max(pil_img1.height, pil_img2.height)
|
| 116 |
+
combined_img = Image.new("RGB", (total_width, max_height))
|
| 117 |
+
combined_img.paste(pil_img1, (0, 0))
|
| 118 |
+
combined_img.paste(pil_img2, (pil_img1.width, 0))
|
| 119 |
+
else:
|
| 120 |
+
combined_img = pil_img1
|
| 121 |
+
|
| 122 |
+
# -----------------------------
|
| 123 |
+
# 5. Run HF Inference
|
| 124 |
+
# -----------------------------
|
| 125 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
pil_output = hf_client.image_to_image(
|
| 127 |
+
image=combined_img, # ✅ single image
|
| 128 |
prompt=prompt,
|
| 129 |
model="Qwen/Qwen-Image-Edit"
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# Convert output PIL to bytes
|
|
|
|
| 133 |
out_buf = io.BytesIO()
|
| 134 |
+
pil_output.save(out_buf, format="PNG")
|
| 135 |
out_bytes = out_buf.getvalue()
|
| 136 |
except Exception as e:
|
| 137 |
traceback.print_exc()
|
| 138 |
raise HTTPException(status_code=500, detail=f"Inference failed: {e}")
|
| 139 |
|
| 140 |
+
# -----------------------------
|
| 141 |
+
# 6. Save output image
|
| 142 |
+
# -----------------------------
|
| 143 |
try:
|
| 144 |
out_id = fs.put(
|
| 145 |
out_bytes,
|
|
|
|
| 153 |
except Exception as e:
|
| 154 |
raise HTTPException(status_code=500, detail=f"Failed saving output image: {e}")
|
| 155 |
|
| 156 |
+
# -----------------------------
|
| 157 |
+
# 7. Log the request
|
| 158 |
+
# -----------------------------
|
| 159 |
try:
|
| 160 |
logs_collection.insert_one({
|
| 161 |
"timestamp": datetime.utcnow(),
|
|
|
|
| 170 |
return JSONResponse({"output_image_id": str(out_id)})
|
| 171 |
|
| 172 |
|
| 173 |
+
|
| 174 |
@app.get("/image/{image_id}")
|
| 175 |
def get_image(image_id: str, download: Optional[bool] = False):
|
| 176 |
"""Retrieve image by GridFS ID"""
|