Update app.py
Browse files
app.py
CHANGED
|
@@ -179,50 +179,7 @@ def compress_pil_image_to_2mb(
|
|
| 179 |
quality -= 5
|
| 180 |
|
| 181 |
return buffer.getvalue()
|
| 182 |
-
|
| 183 |
-
# def run_image_generation(
|
| 184 |
-
# image: Image.Image,
|
| 185 |
-
# prompt: str
|
| 186 |
-
# ) -> Image.Image:
|
| 187 |
-
# """
|
| 188 |
-
# Unified image generation interface.
|
| 189 |
-
# Returns PIL Image.
|
| 190 |
-
# """
|
| 191 |
-
|
| 192 |
-
# if MODEL == "QWEN":
|
| 193 |
-
# return hf_client.image_to_image(
|
| 194 |
-
# image=image,
|
| 195 |
-
# prompt=prompt,
|
| 196 |
-
# model="Qwen/Qwen-Image-Edit"
|
| 197 |
-
# )
|
| 198 |
-
|
| 199 |
-
# elif MODEL == "GEMINI":
|
| 200 |
-
# model = genai.GenerativeModel(GEMINI_IMAGE_MODEL)
|
| 201 |
-
|
| 202 |
-
# # Gemini expects bytes
|
| 203 |
-
# img_buffer = io.BytesIO()
|
| 204 |
-
# image.save(img_buffer, format="PNG")
|
| 205 |
-
# img_buffer.seek(0)
|
| 206 |
-
|
| 207 |
-
# response = model.generate_content(
|
| 208 |
-
# [
|
| 209 |
-
# prompt,
|
| 210 |
-
# {
|
| 211 |
-
# "mime_type": "image/png",
|
| 212 |
-
# "data": img_buffer.getvalue()
|
| 213 |
-
# }
|
| 214 |
-
# ]
|
| 215 |
-
# )
|
| 216 |
-
|
| 217 |
-
# # Extract image bytes from response
|
| 218 |
-
# for part in response.candidates[0].content.parts:
|
| 219 |
-
# if hasattr(part, "inline_data"):
|
| 220 |
-
# return Image.open(io.BytesIO(part.inline_data.data)).convert("RGB")
|
| 221 |
-
|
| 222 |
-
# raise RuntimeError("Gemini did not return an image")
|
| 223 |
-
|
| 224 |
-
# else:
|
| 225 |
-
# raise RuntimeError(f"Unsupported IMAGE_MODEL: {MODEL}")
|
| 226 |
def run_image_generation(
|
| 227 |
image1: Image.Image,
|
| 228 |
prompt: str,
|
|
@@ -373,18 +330,6 @@ async def generate(
|
|
| 373 |
except Exception as e:
|
| 374 |
raise HTTPException(400, f"Failed to read second image: {e}")
|
| 375 |
|
| 376 |
-
# -------------------------
|
| 377 |
-
# 2. COMBINE IF NEEDED
|
| 378 |
-
# -------------------------
|
| 379 |
-
# if pil_img2:
|
| 380 |
-
# total_width = pil_img1.width + pil_img2.width
|
| 381 |
-
# max_height = max(pil_img1.height, pil_img2.height)
|
| 382 |
-
# combined_img = Image.new("RGB", (total_width, max_height))
|
| 383 |
-
# combined_img.paste(pil_img1, (0, 0))
|
| 384 |
-
# combined_img.paste(pil_img2, (pil_img1.width, 0))
|
| 385 |
-
# else:
|
| 386 |
-
# combined_img = pil_img1
|
| 387 |
-
|
| 388 |
# -------------------------
|
| 389 |
# 3. CATEGORY CLICK LOGIC
|
| 390 |
# -------------------------
|
|
@@ -525,16 +470,6 @@ async def generate(
|
|
| 525 |
# 4. HF INFERENCE
|
| 526 |
# -------------------------
|
| 527 |
try:
|
| 528 |
-
# pil_output = hf_client.image_to_image(
|
| 529 |
-
# image=combined_img,
|
| 530 |
-
# prompt=prompt,
|
| 531 |
-
# model="Qwen/Qwen-Image-Edit"
|
| 532 |
-
# )
|
| 533 |
-
#-------------------------------------#
|
| 534 |
-
# pil_output = run_image_generation(
|
| 535 |
-
# image=combined_img,
|
| 536 |
-
# prompt=prompt
|
| 537 |
-
# )
|
| 538 |
# --------------------------------------------------
|
| 539 |
# MODEL OVERRIDE BASED ON CATEGORY
|
| 540 |
# --------------------------------------------------
|
|
@@ -629,119 +564,6 @@ async def generate(
|
|
| 629 |
f"https://logicgoinfotechspaces-polaroidimage.hf.space/image/{compressed_id}"
|
| 630 |
)
|
| 631 |
})
|
| 632 |
-
####################---------------------------------------------------------------------------------------------------###
|
| 633 |
-
###-----OLD CODE--------------####
|
| 634 |
-
# @app.post("/generate")
|
| 635 |
-
# async def generate(
|
| 636 |
-
# prompt: str = Form(...),
|
| 637 |
-
# image1: UploadFile = File(...),
|
| 638 |
-
# image2: Optional[UploadFile] = File(None),
|
| 639 |
-
# user=Depends(verify_firebase_token)
|
| 640 |
-
# ):
|
| 641 |
-
# start_time = time.time()
|
| 642 |
-
|
| 643 |
-
# # PREPARE IMAGE(S)
|
| 644 |
-
# try:
|
| 645 |
-
# img1_bytes = await image1.read()
|
| 646 |
-
# pil_img1 = prepare_image(img1_bytes)
|
| 647 |
-
# input1_id = fs.put(
|
| 648 |
-
# img1_bytes,
|
| 649 |
-
# filename=image1.filename,
|
| 650 |
-
# contentType=image1.content_type,
|
| 651 |
-
# metadata={"role": "input"}
|
| 652 |
-
# )
|
| 653 |
-
# except Exception as e:
|
| 654 |
-
# raise HTTPException(status_code=400, detail=f"Failed to read first image: {e}")
|
| 655 |
-
|
| 656 |
-
# img2_bytes = None
|
| 657 |
-
# input2_id = None
|
| 658 |
-
# pil_img2 = None
|
| 659 |
-
|
| 660 |
-
# if image2:
|
| 661 |
-
# try:
|
| 662 |
-
# img2_bytes = await image2.read()
|
| 663 |
-
# pil_img2 = prepare_image(img2_bytes)
|
| 664 |
-
# input2_id = fs.put(
|
| 665 |
-
# img2_bytes,
|
| 666 |
-
# filename=image2.filename,
|
| 667 |
-
# contentType=image2.content_type,
|
| 668 |
-
# metadata={"role": "input"}
|
| 669 |
-
# )
|
| 670 |
-
# except Exception as e:
|
| 671 |
-
# raise HTTPException(status_code=400, detail=f"Failed to read second image: {e}")
|
| 672 |
-
|
| 673 |
-
# # COMBINE IF NEEDED
|
| 674 |
-
# if pil_img2:
|
| 675 |
-
# total_width = pil_img1.width + pil_img2.width
|
| 676 |
-
# max_height = max(pil_img1.height, pil_img2.height)
|
| 677 |
-
# combined_img = Image.new("RGB", (total_width, max_height))
|
| 678 |
-
# combined_img.paste(pil_img1, (0, 0))
|
| 679 |
-
# combined_img.paste(pil_img2, (pil_img1.width, 0))
|
| 680 |
-
# else:
|
| 681 |
-
# combined_img = pil_img1
|
| 682 |
-
|
| 683 |
-
# # INFERENCE
|
| 684 |
-
# try:
|
| 685 |
-
# pil_output = hf_client.image_to_image(
|
| 686 |
-
# image=combined_img,
|
| 687 |
-
# prompt=prompt,
|
| 688 |
-
# model="Qwen/Qwen-Image-Edit"
|
| 689 |
-
# )
|
| 690 |
-
# except Exception as e:
|
| 691 |
-
# # LOG FAILURE
|
| 692 |
-
# response_time_ms = round((time.time() - start_time) * 1000)
|
| 693 |
-
# logs_collection.insert_one({
|
| 694 |
-
# "timestamp": datetime.utcnow(),
|
| 695 |
-
# "status": "failure",
|
| 696 |
-
# "input1_id": str(input1_id),
|
| 697 |
-
# "input2_id": str(input2_id) if input2_id else None,
|
| 698 |
-
# "prompt": prompt,
|
| 699 |
-
# "user_email": user.get("email"),
|
| 700 |
-
# "error": str(e),
|
| 701 |
-
# "response_time_ms": response_time_ms
|
| 702 |
-
# })
|
| 703 |
-
# raise HTTPException(status_code=500, detail=f"Inference failed: {e}")
|
| 704 |
-
|
| 705 |
-
# # SAVE OUTPUT IMAGE
|
| 706 |
-
# out_buf = io.BytesIO()
|
| 707 |
-
# pil_output.save(out_buf, format="PNG")
|
| 708 |
-
# out_bytes = out_buf.getvalue()
|
| 709 |
-
|
| 710 |
-
# out_id = fs.put(
|
| 711 |
-
# out_bytes,
|
| 712 |
-
# filename=f"result_{input1_id}.png",
|
| 713 |
-
# contentType="image/png",
|
| 714 |
-
# metadata={
|
| 715 |
-
# "role": "output",
|
| 716 |
-
# "prompt": prompt,
|
| 717 |
-
# "input1_id": str(input1_id),
|
| 718 |
-
# "input2_id": str(input2_id) if input2_id else None,
|
| 719 |
-
# "user_email": user.get("email"),
|
| 720 |
-
# }
|
| 721 |
-
# )
|
| 722 |
-
|
| 723 |
-
# # FINAL RESPONSE TIME
|
| 724 |
-
# response_time_ms = round((time.time() - start_time) * 1000)
|
| 725 |
-
|
| 726 |
-
# # LOG SUCCESS - THIS WAS MISSING THE STATUS AND RESPONSE_TIME_MS FIELDS!
|
| 727 |
-
# logs_collection.insert_one({
|
| 728 |
-
# "timestamp": datetime.utcnow(),
|
| 729 |
-
# "status": "success", # ✅ FIXED: Added status field
|
| 730 |
-
# "input1_id": str(input1_id),
|
| 731 |
-
# "input2_id": str(input2_id) if input2_id else None,
|
| 732 |
-
# "output_id": str(out_id),
|
| 733 |
-
# "prompt": prompt,
|
| 734 |
-
# "user_email": user.get("email"), # ✅ FIXED: Changed from "user" to "user_email"
|
| 735 |
-
# "response_time_ms": response_time_ms # ✅ FIXED: Added response_time_ms field
|
| 736 |
-
# })
|
| 737 |
-
|
| 738 |
-
# return JSONResponse({
|
| 739 |
-
# "output_image_id": str(out_id),
|
| 740 |
-
# "user": user.get("email"),
|
| 741 |
-
# "response_time_ms": response_time_ms
|
| 742 |
-
# })
|
| 743 |
-
|
| 744 |
-
|
| 745 |
|
| 746 |
@app.get("/image/{image_id}")
|
| 747 |
def get_image(image_id: str, download: Optional[bool] = False):
|
|
@@ -765,7 +587,6 @@ def get_image(image_id: str, download: Optional[bool] = False):
|
|
| 765 |
headers=headers
|
| 766 |
)
|
| 767 |
|
| 768 |
-
|
| 769 |
# ---------------------------------------------------------------------
|
| 770 |
# Run locally
|
| 771 |
# ---------------------------------------------------------------------
|
|
|
|
| 179 |
quality -= 5
|
| 180 |
|
| 181 |
return buffer.getvalue()
|
| 182 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def run_image_generation(
|
| 184 |
image1: Image.Image,
|
| 185 |
prompt: str,
|
|
|
|
| 330 |
except Exception as e:
|
| 331 |
raise HTTPException(400, f"Failed to read second image: {e}")
|
| 332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
# -------------------------
|
| 334 |
# 3. CATEGORY CLICK LOGIC
|
| 335 |
# -------------------------
|
|
|
|
| 470 |
# 4. HF INFERENCE
|
| 471 |
# -------------------------
|
| 472 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 473 |
# --------------------------------------------------
|
| 474 |
# MODEL OVERRIDE BASED ON CATEGORY
|
| 475 |
# --------------------------------------------------
|
|
|
|
| 564 |
f"https://logicgoinfotechspaces-polaroidimage.hf.space/image/{compressed_id}"
|
| 565 |
)
|
| 566 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
|
| 568 |
@app.get("/image/{image_id}")
|
| 569 |
def get_image(image_id: str, download: Optional[bool] = False):
|
|
|
|
| 587 |
headers=headers
|
| 588 |
)
|
| 589 |
|
|
|
|
| 590 |
# ---------------------------------------------------------------------
|
| 591 |
# Run locally
|
| 592 |
# ---------------------------------------------------------------------
|