Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ from pymongo import MongoClient
|
|
| 12 |
import gridfs
|
| 13 |
from bson.objectid import ObjectId
|
| 14 |
from PIL import Image
|
| 15 |
-
import torch
|
| 16 |
|
| 17 |
# Hugging Face Inference client
|
| 18 |
from huggingface_hub import InferenceClient
|
|
@@ -20,19 +19,19 @@ from huggingface_hub import InferenceClient
|
|
| 20 |
# ---------------------------------------------------------------------
|
| 21 |
# Environment & MongoDB setup
|
| 22 |
# ---------------------------------------------------------------------
|
| 23 |
-
HF_TOKEN = os.getenv("HF_TOKEN") #
|
| 24 |
if not HF_TOKEN:
|
| 25 |
raise RuntimeError("HF_TOKEN not set in environment variables")
|
| 26 |
|
| 27 |
-
MONGODB_URI = "mongodb+srv://harilogicgo_db_user:
|
| 28 |
-
DB_NAME = "
|
| 29 |
|
| 30 |
mongo = MongoClient(MONGODB_URI)
|
| 31 |
db = mongo[DB_NAME]
|
| 32 |
fs = gridfs.GridFS(db)
|
| 33 |
logs_collection = db["logs"]
|
| 34 |
|
| 35 |
-
#
|
| 36 |
hf_client = InferenceClient(token=HF_TOKEN)
|
| 37 |
|
| 38 |
# ---------------------------------------------------------------------
|
|
@@ -60,6 +59,7 @@ class HealthResponse(BaseModel):
|
|
| 60 |
# ---------------------------------------------------------------------
|
| 61 |
@app.get("/health", response_model=HealthResponse)
|
| 62 |
def health():
|
|
|
|
| 63 |
try:
|
| 64 |
mongo.admin.command("ping")
|
| 65 |
return HealthResponse(status="ok", db=db.name, model="Qwen/Qwen-Image-Edit")
|
|
@@ -73,7 +73,9 @@ async def generate(
|
|
| 73 |
image1: UploadFile = File(...),
|
| 74 |
image2: Optional[UploadFile] = File(None),
|
| 75 |
):
|
| 76 |
-
"""
|
|
|
|
|
|
|
| 77 |
# -----------------------------
|
| 78 |
# 1. Read first image
|
| 79 |
# -----------------------------
|
|
@@ -108,23 +110,24 @@ async def generate(
|
|
| 108 |
raise HTTPException(status_code=500, detail=f"Failed saving input images: {e}")
|
| 109 |
|
| 110 |
# -----------------------------
|
| 111 |
-
# 4.
|
| 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, #
|
| 128 |
prompt=prompt,
|
| 129 |
model="Qwen/Qwen-Image-Edit"
|
| 130 |
)
|
|
@@ -170,7 +173,6 @@ async def generate(
|
|
| 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"""
|
|
|
|
| 12 |
import gridfs
|
| 13 |
from bson.objectid import ObjectId
|
| 14 |
from PIL import Image
|
|
|
|
| 15 |
|
| 16 |
# Hugging Face Inference client
|
| 17 |
from huggingface_hub import InferenceClient
|
|
|
|
| 19 |
# ---------------------------------------------------------------------
|
| 20 |
# Environment & MongoDB setup
|
| 21 |
# ---------------------------------------------------------------------
|
| 22 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Make sure to set this in your environment
|
| 23 |
if not HF_TOKEN:
|
| 24 |
raise RuntimeError("HF_TOKEN not set in environment variables")
|
| 25 |
|
| 26 |
+
MONGODB_URI = "mongodb+srv://harilogicgo_db_user:EpWocJKRpXau8eBW@images.nh9bx9l.mongodb.net/?appName=images"
|
| 27 |
+
DB_NAME = "polaroid_db"
|
| 28 |
|
| 29 |
mongo = MongoClient(MONGODB_URI)
|
| 30 |
db = mongo[DB_NAME]
|
| 31 |
fs = gridfs.GridFS(db)
|
| 32 |
logs_collection = db["logs"]
|
| 33 |
|
| 34 |
+
# Hugging Face Inference client
|
| 35 |
hf_client = InferenceClient(token=HF_TOKEN)
|
| 36 |
|
| 37 |
# ---------------------------------------------------------------------
|
|
|
|
| 59 |
# ---------------------------------------------------------------------
|
| 60 |
@app.get("/health", response_model=HealthResponse)
|
| 61 |
def health():
|
| 62 |
+
"""Health check endpoint"""
|
| 63 |
try:
|
| 64 |
mongo.admin.command("ping")
|
| 65 |
return HealthResponse(status="ok", db=db.name, model="Qwen/Qwen-Image-Edit")
|
|
|
|
| 73 |
image1: UploadFile = File(...),
|
| 74 |
image2: Optional[UploadFile] = File(None),
|
| 75 |
):
|
| 76 |
+
"""
|
| 77 |
+
Upload 1 or 2 images + prompt and get edited image via HF Inference
|
| 78 |
+
"""
|
| 79 |
# -----------------------------
|
| 80 |
# 1. Read first image
|
| 81 |
# -----------------------------
|
|
|
|
| 110 |
raise HTTPException(status_code=500, detail=f"Failed saving input images: {e}")
|
| 111 |
|
| 112 |
# -----------------------------
|
| 113 |
+
# 4. Prepare image for HF Inference
|
| 114 |
# -----------------------------
|
| 115 |
if pil_img2:
|
| 116 |
+
# Combine two images side by side
|
| 117 |
total_width = pil_img1.width + pil_img2.width
|
| 118 |
max_height = max(pil_img1.height, pil_img2.height)
|
| 119 |
combined_img = Image.new("RGB", (total_width, max_height))
|
| 120 |
combined_img.paste(pil_img1, (0, 0))
|
| 121 |
combined_img.paste(pil_img2, (pil_img1.width, 0))
|
| 122 |
else:
|
| 123 |
+
combined_img = pil_img1 # single image
|
| 124 |
|
| 125 |
# -----------------------------
|
| 126 |
# 5. Run HF Inference
|
| 127 |
# -----------------------------
|
| 128 |
try:
|
| 129 |
pil_output = hf_client.image_to_image(
|
| 130 |
+
image=combined_img, # Always pass single combined image
|
| 131 |
prompt=prompt,
|
| 132 |
model="Qwen/Qwen-Image-Edit"
|
| 133 |
)
|
|
|
|
| 173 |
return JSONResponse({"output_image_id": str(out_id)})
|
| 174 |
|
| 175 |
|
|
|
|
| 176 |
@app.get("/image/{image_id}")
|
| 177 |
def get_image(image_id: str, download: Optional[bool] = False):
|
| 178 |
"""Retrieve image by GridFS ID"""
|