Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -971,59 +971,146 @@ def _verify_bearer_token(credentials: Optional[HTTPAuthorizationCredentials] = D
|
|
| 971 |
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
| 972 |
return True
|
| 973 |
def sync_log_media_click(user_id_str: str, category_id_str: str):
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
| 978 |
-
|
| 979 |
-
|
| 980 |
-
|
| 981 |
-
|
| 982 |
-
|
| 983 |
-
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
| 991 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 992 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 993 |
{
|
| 994 |
"$set": {
|
| 995 |
-
"updatedAt": now
|
| 996 |
-
"categories.$.lastClickedAt": now
|
| 997 |
},
|
| 998 |
-
"$
|
| 999 |
-
"categories
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1000 |
}
|
| 1001 |
-
}
|
|
|
|
| 1002 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1003 |
|
| 1004 |
-
|
| 1005 |
-
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
|
| 1009 |
-
|
| 1010 |
-
|
| 1011 |
-
|
| 1012 |
-
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
|
| 1016 |
-
|
| 1017 |
-
|
| 1018 |
-
|
| 1019 |
-
|
| 1020 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1021 |
|
| 1022 |
-
|
| 1023 |
|
| 1024 |
-
|
| 1025 |
-
|
| 1026 |
-
|
| 1027 |
|
| 1028 |
DEFAULT_FACE_MODEL = 'GFPGANv1.4.pth'
|
| 1029 |
DEFAULT_UPSCALE_MODEL = 'SRVGG, realesr-general-x4v3.pth'
|
|
|
|
| 971 |
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
| 972 |
return True
|
| 973 |
def sync_log_media_click(user_id_str: str, category_id_str: str):
|
| 974 |
+
"""
|
| 975 |
+
Synchronously logs a click event to the media_clicks collection.
|
| 976 |
+
This function should be run using asyncio.to_thread().
|
| 977 |
+
"""
|
| 978 |
+
|
| 979 |
+
if _media_clicks_col is None:
|
| 980 |
+
return
|
| 981 |
+
|
| 982 |
+
try:
|
| 983 |
+
user_oid = ObjectId(user_id_str.strip())
|
| 984 |
+
category_oid = ObjectId(category_id_str.strip())
|
| 985 |
+
now = datetime.utcnow()
|
| 986 |
+
|
| 987 |
+
logger.info(
|
| 988 |
+
f"Attempting background write for User:{user_id_str}, Category:{category_id_str}"
|
| 989 |
+
)
|
| 990 |
+
|
| 991 |
+
# --------------------------------------------------
|
| 992 |
+
# 1. AI EDIT USAGE TRACKING (GLOBAL PER USER)
|
| 993 |
+
# --------------------------------------------------
|
| 994 |
+
_media_clicks_col.update_one(
|
| 995 |
+
{"userId": user_oid},
|
| 996 |
+
{
|
| 997 |
+
"$set": {
|
| 998 |
+
"ai_edit_last_date": now,
|
| 999 |
+
"updatedAt": now
|
| 1000 |
+
},
|
| 1001 |
+
"$inc": {
|
| 1002 |
+
"ai_edit_complete": 1
|
| 1003 |
+
},
|
| 1004 |
+
"$setOnInsert": {
|
| 1005 |
+
"createdAt": now
|
| 1006 |
+
}
|
| 1007 |
+
},
|
| 1008 |
+
upsert=True
|
| 1009 |
+
)
|
| 1010 |
+
|
| 1011 |
+
# --------------------------------------------------
|
| 1012 |
+
# 2. CATEGORY CLICK LOGIC
|
| 1013 |
+
# --------------------------------------------------
|
| 1014 |
+
update_result = _media_clicks_col.update_one(
|
| 1015 |
+
{
|
| 1016 |
+
"userId": user_oid,
|
| 1017 |
+
"categories.categoryId": category_oid
|
| 1018 |
+
},
|
| 1019 |
+
{
|
| 1020 |
+
"$set": {
|
| 1021 |
+
"updatedAt": now,
|
| 1022 |
+
"categories.$.lastClickedAt": now
|
| 1023 |
},
|
| 1024 |
+
"$inc": {
|
| 1025 |
+
"categories.$.click_count": 1
|
| 1026 |
+
}
|
| 1027 |
+
}
|
| 1028 |
+
)
|
| 1029 |
+
|
| 1030 |
+
# --------------------------------------------------
|
| 1031 |
+
# 3. IF CATEGORY DOES NOT EXIST → PUSH NEW
|
| 1032 |
+
# --------------------------------------------------
|
| 1033 |
+
if update_result.matched_count == 0:
|
| 1034 |
+
_media_clicks_col.update_one(
|
| 1035 |
+
{"userId": user_oid},
|
| 1036 |
{
|
| 1037 |
"$set": {
|
| 1038 |
+
"updatedAt": now
|
|
|
|
| 1039 |
},
|
| 1040 |
+
"$push": {
|
| 1041 |
+
"categories": {
|
| 1042 |
+
"categoryId": category_oid,
|
| 1043 |
+
"click_count": 1,
|
| 1044 |
+
"lastClickedAt": now
|
| 1045 |
+
}
|
| 1046 |
}
|
| 1047 |
+
},
|
| 1048 |
+
upsert=True
|
| 1049 |
)
|
| 1050 |
+
|
| 1051 |
+
logger.info(
|
| 1052 |
+
f"Media click logged for User {user_id_str} on Category {category_id_str}"
|
| 1053 |
+
)
|
| 1054 |
+
|
| 1055 |
+
except Exception as media_err:
|
| 1056 |
+
logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 1057 |
+
# swallow error – do not affect main flow
|
| 1058 |
+
pass
|
| 1059 |
+
|
| 1060 |
+
# def sync_log_media_click(user_id_str: str, category_id_str: str):
|
| 1061 |
+
# """
|
| 1062 |
+
# Synchronously logs a click event to the media_clicks collection.
|
| 1063 |
+
# This function should be run using asyncio.to_thread().
|
| 1064 |
+
# """
|
| 1065 |
+
# if _media_clicks_col is None:
|
| 1066 |
+
# return
|
| 1067 |
|
| 1068 |
+
# try:
|
| 1069 |
+
# user_oid = ObjectId(user_id_str.strip())
|
| 1070 |
+
# category_oid = ObjectId(category_id_str.strip())
|
| 1071 |
+
# now = datetime.utcnow()
|
| 1072 |
+
# logger.info(f"Attempting background write for User:{user_id_str}, Category:{category_id_str}") # <--- NEW LOG
|
| 1073 |
+
|
| 1074 |
+
# # 1. Try updating an existing category entry within the user's document
|
| 1075 |
+
# update_result = _media_clicks_col.update_one(
|
| 1076 |
+
# {
|
| 1077 |
+
# "userId": user_oid,
|
| 1078 |
+
# "categories.categoryId": category_oid
|
| 1079 |
+
# },
|
| 1080 |
+
# {
|
| 1081 |
+
# "$set": {
|
| 1082 |
+
# "updatedAt": now,
|
| 1083 |
+
# "categories.$.lastClickedAt": now
|
| 1084 |
+
# },
|
| 1085 |
+
# "$inc": {
|
| 1086 |
+
# "categories.$.click_count": 1
|
| 1087 |
+
# }
|
| 1088 |
+
# }
|
| 1089 |
+
# )
|
| 1090 |
+
|
| 1091 |
+
# # 2. If no category entry exists (matched_count == 0) -> push a new one (or create user doc)
|
| 1092 |
+
# if update_result.matched_count == 0:
|
| 1093 |
+
# _media_clicks_col.update_one(
|
| 1094 |
+
# { "userId": user_oid },
|
| 1095 |
+
# {
|
| 1096 |
+
# "$setOnInsert": { "createdAt": now,"ai_edit_complete": 0 },
|
| 1097 |
+
# "$set": { "updatedAt": now},
|
| 1098 |
+
# "$push": {
|
| 1099 |
+
# "categories": {
|
| 1100 |
+
# "categoryId": category_oid,
|
| 1101 |
+
# "click_count": 1,
|
| 1102 |
+
# "lastClickedAt": now
|
| 1103 |
+
# }
|
| 1104 |
+
# }
|
| 1105 |
+
# },
|
| 1106 |
+
# upsert=True
|
| 1107 |
+
# )
|
| 1108 |
|
| 1109 |
+
# logger.info(f"Media click logged for User {user_id_str} on Category {category_id_str}")
|
| 1110 |
|
| 1111 |
+
# except Exception as media_err:
|
| 1112 |
+
# logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 1113 |
+
# pass # Handle logging error gracefully
|
| 1114 |
|
| 1115 |
DEFAULT_FACE_MODEL = 'GFPGANv1.4.pth'
|
| 1116 |
DEFAULT_UPSCALE_MODEL = 'SRVGG, realesr-general-x4v3.pth'
|