Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -162,48 +162,108 @@ async def log_faceswap_hit(user_email: str, status: str, start_time: datetime, e
|
|
| 162 |
})
|
| 163 |
|
| 164 |
# --------------------- Media Click Logging Helper ---------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
# async def log_media_click(user_id: str, category_oid_str: str):
|
| 166 |
# """
|
| 167 |
-
# Logs a click event to the media_clicks collection against the Category
|
| 168 |
-
# and tracks AI edit usage.
|
| 169 |
# """
|
| 170 |
# try:
|
| 171 |
# user_oid = ObjectId(user_id.strip())
|
| 172 |
# category_oid = ObjectId(category_oid_str.strip())
|
| 173 |
# now = datetime.utcnow()
|
| 174 |
|
| 175 |
-
# #
|
| 176 |
-
# # OPTIONAL: VALIDATE CATEGORY EXISTS
|
| 177 |
-
# # --------------------------------------------------
|
| 178 |
# if not await categories_col.find_one({"_id": category_oid}):
|
| 179 |
-
# logger.warning(
|
| 180 |
-
# f"Category ID {category_oid_str} not found. Skipping click logging."
|
| 181 |
-
# )
|
| 182 |
# return
|
| 183 |
|
| 184 |
-
# #
|
| 185 |
-
# # 1. AI EDIT USAGE TRACKING (PER USER)
|
| 186 |
-
# # --------------------------------------------------
|
| 187 |
-
# await media_clicks_col.update_one(
|
| 188 |
-
# {"userId": user_oid},
|
| 189 |
-
# {
|
| 190 |
-
# "$set": {
|
| 191 |
-
# "ai_edit_last_date": now,
|
| 192 |
-
# "updatedAt": now
|
| 193 |
-
# },
|
| 194 |
-
# "$inc": {
|
| 195 |
-
# "ai_edit_complete": 1
|
| 196 |
-
# },
|
| 197 |
-
# "$setOnInsert": {
|
| 198 |
-
# "createdAt": now
|
| 199 |
-
# }
|
| 200 |
-
# },
|
| 201 |
-
# upsert=True
|
| 202 |
-
# )
|
| 203 |
-
|
| 204 |
-
# # --------------------------------------------------
|
| 205 |
-
# # 2. UPDATE EXISTING CATEGORY CLICK
|
| 206 |
-
# # --------------------------------------------------
|
| 207 |
# update_result = await media_clicks_col.update_one(
|
| 208 |
# {
|
| 209 |
# "userId": user_oid,
|
|
@@ -220,16 +280,13 @@ async def log_faceswap_hit(user_email: str, status: str, start_time: datetime, e
|
|
| 220 |
# }
|
| 221 |
# )
|
| 222 |
|
| 223 |
-
# #
|
| 224 |
-
# # 3. PUSH CATEGORY IF NOT EXISTS
|
| 225 |
-
# # --------------------------------------------------
|
| 226 |
# if update_result.matched_count == 0:
|
| 227 |
# await media_clicks_col.update_one(
|
| 228 |
-
# {"userId": user_oid},
|
| 229 |
# {
|
| 230 |
-
# "$
|
| 231 |
-
#
|
| 232 |
-
# },
|
| 233 |
# "$push": {
|
| 234 |
# "categories": {
|
| 235 |
# "categoryId": category_oid,
|
|
@@ -240,68 +297,12 @@ async def log_faceswap_hit(user_email: str, status: str, start_time: datetime, e
|
|
| 240 |
# },
|
| 241 |
# upsert=True
|
| 242 |
# )
|
| 243 |
-
|
| 244 |
-
# logger.info(
|
| 245 |
-
# f"Media click logged for User {user_id} on Category {category_oid_str}"
|
| 246 |
-
# )
|
| 247 |
|
| 248 |
# except Exception as media_err:
|
| 249 |
# logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 250 |
|
| 251 |
-
async def log_media_click(user_id: str, category_oid_str: str):
|
| 252 |
-
"""
|
| 253 |
-
Logs a click event to the media_clicks collection against the Category.
|
| 254 |
-
"""
|
| 255 |
-
try:
|
| 256 |
-
user_oid = ObjectId(user_id.strip())
|
| 257 |
-
category_oid = ObjectId(category_oid_str.strip())
|
| 258 |
-
now = datetime.utcnow()
|
| 259 |
-
|
| 260 |
-
# Check if category exists (optional, but prevents logging invalid IDs)
|
| 261 |
-
if not await categories_col.find_one({"_id": category_oid}):
|
| 262 |
-
logger.warning(f"Category ID {category_oid_str} not found. Skipping click logging.")
|
| 263 |
-
return
|
| 264 |
-
|
| 265 |
-
# 1) Try updating existing category click entry
|
| 266 |
-
update_result = await media_clicks_col.update_one(
|
| 267 |
-
{
|
| 268 |
-
"userId": user_oid,
|
| 269 |
-
"categories.categoryId": category_oid
|
| 270 |
-
},
|
| 271 |
-
{
|
| 272 |
-
"$set": {
|
| 273 |
-
"updatedAt": now,
|
| 274 |
-
"categories.$.lastClickedAt": now
|
| 275 |
-
},
|
| 276 |
-
"$inc": {
|
| 277 |
-
"categories.$.click_count": 1
|
| 278 |
-
}
|
| 279 |
-
}
|
| 280 |
-
)
|
| 281 |
-
|
| 282 |
-
# 2) If no category entry exists → push new one (or create doc)
|
| 283 |
-
if update_result.matched_count == 0:
|
| 284 |
-
await media_clicks_col.update_one(
|
| 285 |
-
{ "userId": user_oid },
|
| 286 |
-
{
|
| 287 |
-
"$setOnInsert": { "createdAt": now },
|
| 288 |
-
"$set": { "updatedAt": now },
|
| 289 |
-
"$push": {
|
| 290 |
-
"categories": {
|
| 291 |
-
"categoryId": category_oid,
|
| 292 |
-
"click_count": 1,
|
| 293 |
-
"lastClickedAt": now
|
| 294 |
-
}
|
| 295 |
-
}
|
| 296 |
-
},
|
| 297 |
-
upsert=True
|
| 298 |
-
)
|
| 299 |
-
|
| 300 |
-
logger.info(f"Media click logged for User {user_id} on Category {category_oid_str}")
|
| 301 |
-
|
| 302 |
-
except Exception as media_err:
|
| 303 |
-
logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 304 |
-
|
| 305 |
# --------------------- Face Swap Pipeline ---------------------
|
| 306 |
swap_lock = threading.Lock()
|
| 307 |
def face_swap_and_enhance(src_img, tgt_img, temp_dir="/tmp/faceswap_work"):
|
|
|
|
| 162 |
})
|
| 163 |
|
| 164 |
# --------------------- Media Click Logging Helper ---------------------
|
| 165 |
+
async def log_media_click(user_id: str, category_oid_str: str):
|
| 166 |
+
"""
|
| 167 |
+
Logs a click event to the media_clicks collection against the Category.
|
| 168 |
+
Safely updates ai_edit_last_date and ai_edit_complete at ROOT level.
|
| 169 |
+
"""
|
| 170 |
+
try:
|
| 171 |
+
user_oid = ObjectId(user_id.strip())
|
| 172 |
+
category_oid = ObjectId(category_oid_str.strip())
|
| 173 |
+
now = datetime.utcnow()
|
| 174 |
+
|
| 175 |
+
# Optional safety check
|
| 176 |
+
if not await categories_col.find_one({"_id": category_oid}):
|
| 177 |
+
logger.warning(
|
| 178 |
+
"Category ID %s not found. Skipping media click logging.",
|
| 179 |
+
category_oid_str
|
| 180 |
+
)
|
| 181 |
+
return
|
| 182 |
+
|
| 183 |
+
# -------------------------------------------------
|
| 184 |
+
# STEP 1: Ensure user document + root fields exist
|
| 185 |
+
# -------------------------------------------------
|
| 186 |
+
await media_clicks_col.update_one(
|
| 187 |
+
{"userId": user_oid},
|
| 188 |
+
{
|
| 189 |
+
"$setOnInsert": {
|
| 190 |
+
"userId": user_oid,
|
| 191 |
+
"createdAt": now,
|
| 192 |
+
"ai_edit_complete": 0
|
| 193 |
+
}
|
| 194 |
+
},
|
| 195 |
+
upsert=True
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
# -------------------------------------------------
|
| 199 |
+
# STEP 2: Try updating existing category
|
| 200 |
+
# -------------------------------------------------
|
| 201 |
+
update_result = await media_clicks_col.update_one(
|
| 202 |
+
{
|
| 203 |
+
"userId": user_oid,
|
| 204 |
+
"categories.categoryId": category_oid
|
| 205 |
+
},
|
| 206 |
+
{
|
| 207 |
+
"$inc": {
|
| 208 |
+
"categories.$.click_count": 1,
|
| 209 |
+
"ai_edit_complete": 1
|
| 210 |
+
},
|
| 211 |
+
"$set": {
|
| 212 |
+
"categories.$.lastClickedAt": now,
|
| 213 |
+
"ai_edit_last_date": now,
|
| 214 |
+
"updatedAt": now
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
# -------------------------------------------------
|
| 220 |
+
# STEP 3: Category not present → push new
|
| 221 |
+
# -------------------------------------------------
|
| 222 |
+
if update_result.matched_count == 0:
|
| 223 |
+
await media_clicks_col.update_one(
|
| 224 |
+
{"userId": user_oid},
|
| 225 |
+
{
|
| 226 |
+
"$inc": {
|
| 227 |
+
"ai_edit_complete": 1
|
| 228 |
+
},
|
| 229 |
+
"$set": {
|
| 230 |
+
"ai_edit_last_date": now,
|
| 231 |
+
"updatedAt": now
|
| 232 |
+
},
|
| 233 |
+
"$push": {
|
| 234 |
+
"categories": {
|
| 235 |
+
"categoryId": category_oid,
|
| 236 |
+
"click_count": 1,
|
| 237 |
+
"lastClickedAt": now
|
| 238 |
+
}
|
| 239 |
+
}
|
| 240 |
+
}
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
logger.info(
|
| 244 |
+
"[MEDIA_CLICK] user=%s category=%s ai_edit_complete incremented",
|
| 245 |
+
user_id,
|
| 246 |
+
category_oid_str
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
except Exception as media_err:
|
| 250 |
+
logger.error("MEDIA_CLICK LOGGING ERROR: %s", media_err)
|
| 251 |
+
|
| 252 |
# async def log_media_click(user_id: str, category_oid_str: str):
|
| 253 |
# """
|
| 254 |
+
# Logs a click event to the media_clicks collection against the Category.
|
|
|
|
| 255 |
# """
|
| 256 |
# try:
|
| 257 |
# user_oid = ObjectId(user_id.strip())
|
| 258 |
# category_oid = ObjectId(category_oid_str.strip())
|
| 259 |
# now = datetime.utcnow()
|
| 260 |
|
| 261 |
+
# # Check if category exists (optional, but prevents logging invalid IDs)
|
|
|
|
|
|
|
| 262 |
# if not await categories_col.find_one({"_id": category_oid}):
|
| 263 |
+
# logger.warning(f"Category ID {category_oid_str} not found. Skipping click logging.")
|
|
|
|
|
|
|
| 264 |
# return
|
| 265 |
|
| 266 |
+
# # 1) Try updating existing category click entry
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
# update_result = await media_clicks_col.update_one(
|
| 268 |
# {
|
| 269 |
# "userId": user_oid,
|
|
|
|
| 280 |
# }
|
| 281 |
# )
|
| 282 |
|
| 283 |
+
# # 2) If no category entry exists → push new one (or create doc)
|
|
|
|
|
|
|
| 284 |
# if update_result.matched_count == 0:
|
| 285 |
# await media_clicks_col.update_one(
|
| 286 |
+
# { "userId": user_oid },
|
| 287 |
# {
|
| 288 |
+
# "$setOnInsert": { "createdAt": now },
|
| 289 |
+
# "$set": { "updatedAt": now },
|
|
|
|
| 290 |
# "$push": {
|
| 291 |
# "categories": {
|
| 292 |
# "categoryId": category_oid,
|
|
|
|
| 297 |
# },
|
| 298 |
# upsert=True
|
| 299 |
# )
|
| 300 |
+
|
| 301 |
+
# logger.info(f"Media click logged for User {user_id} on Category {category_oid_str}")
|
|
|
|
|
|
|
| 302 |
|
| 303 |
# except Exception as media_err:
|
| 304 |
# logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
# --------------------- Face Swap Pipeline ---------------------
|
| 307 |
swap_lock = threading.Lock()
|
| 308 |
def face_swap_and_enhance(src_img, tgt_img, temp_dir="/tmp/faceswap_work"):
|