Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -870,61 +870,7 @@ class Upscale:
|
|
| 870 |
if is_success:
|
| 871 |
im_buf_arr.tofile(save_path)
|
| 872 |
|
| 873 |
-
def sync_log_media_click(user_id_str: str, category_id_str: str):
|
| 874 |
-
"""
|
| 875 |
-
Synchronously logs a click event to the media_clicks collection.
|
| 876 |
-
This function should be run using asyncio.to_thread().
|
| 877 |
-
"""
|
| 878 |
-
if _media_clicks_col is None:
|
| 879 |
-
return
|
| 880 |
-
|
| 881 |
-
try:
|
| 882 |
-
user_oid = ObjectId(user_id_str.strip())
|
| 883 |
-
category_oid = ObjectId(category_id_str.strip())
|
| 884 |
-
now = datetime.utcnow()
|
| 885 |
-
logger.info(f"Attempting background write for User:{user_id_str}, Category:{category_id_str}") # <--- NEW LOG
|
| 886 |
|
| 887 |
-
# 1. Try updating an existing category entry within the user's document
|
| 888 |
-
update_result = _media_clicks_col.update_one(
|
| 889 |
-
{
|
| 890 |
-
"userId": user_oid,
|
| 891 |
-
"categories.categoryId": category_oid
|
| 892 |
-
},
|
| 893 |
-
{
|
| 894 |
-
"$set": {
|
| 895 |
-
"updatedAt": now,
|
| 896 |
-
"categories.$.lastClickedAt": now
|
| 897 |
-
},
|
| 898 |
-
"$inc": {
|
| 899 |
-
"categories.$.click_count": 1
|
| 900 |
-
}
|
| 901 |
-
}
|
| 902 |
-
)
|
| 903 |
-
|
| 904 |
-
# 2. If no category entry exists (matched_count == 0) -> push a new one (or create user doc)
|
| 905 |
-
if update_result.matched_count == 0:
|
| 906 |
-
_media_clicks_col.update_one(
|
| 907 |
-
{ "userId": user_oid },
|
| 908 |
-
{
|
| 909 |
-
"$setOnInsert": { "createdAt": now },
|
| 910 |
-
"$set": { "updatedAt": now },
|
| 911 |
-
"$push": {
|
| 912 |
-
"categories": {
|
| 913 |
-
"categoryId": category_oid,
|
| 914 |
-
"click_count": 1,
|
| 915 |
-
"lastClickedAt": now
|
| 916 |
-
}
|
| 917 |
-
}
|
| 918 |
-
},
|
| 919 |
-
upsert=True
|
| 920 |
-
)
|
| 921 |
-
|
| 922 |
-
logger.info(f"Media click logged for User {user_id_str} on Category {category_id_str}")
|
| 923 |
-
|
| 924 |
-
except Exception as media_err:
|
| 925 |
-
logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 926 |
-
pass # Handle logging error gracefully
|
| 927 |
-
|
| 928 |
class Timer:
|
| 929 |
def __init__(self):
|
| 930 |
self.start_time = time.perf_counter()
|
|
@@ -992,6 +938,60 @@ def _verify_bearer_token(credentials: Optional[HTTPAuthorizationCredentials] = D
|
|
| 992 |
if not _api_bearer_token or token != _api_bearer_token:
|
| 993 |
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
| 994 |
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 995 |
|
| 996 |
DEFAULT_FACE_MODEL = 'GFPGANv1.4.pth'
|
| 997 |
DEFAULT_UPSCALE_MODEL = 'SRVGG, realesr-general-x4v3.pth'
|
|
|
|
| 870 |
if is_success:
|
| 871 |
im_buf_arr.tofile(save_path)
|
| 872 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
class Timer:
|
| 875 |
def __init__(self):
|
| 876 |
self.start_time = time.perf_counter()
|
|
|
|
| 938 |
if not _api_bearer_token or token != _api_bearer_token:
|
| 939 |
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token")
|
| 940 |
return True
|
| 941 |
+
def sync_log_media_click(user_id_str: str, category_id_str: str):
|
| 942 |
+
"""
|
| 943 |
+
Synchronously logs a click event to the media_clicks collection.
|
| 944 |
+
This function should be run using asyncio.to_thread().
|
| 945 |
+
"""
|
| 946 |
+
if _media_clicks_col is None:
|
| 947 |
+
return
|
| 948 |
+
|
| 949 |
+
try:
|
| 950 |
+
user_oid = ObjectId(user_id_str.strip())
|
| 951 |
+
category_oid = ObjectId(category_id_str.strip())
|
| 952 |
+
now = datetime.utcnow()
|
| 953 |
+
logger.info(f"Attempting background write for User:{user_id_str}, Category:{category_id_str}") # <--- NEW LOG
|
| 954 |
+
|
| 955 |
+
# 1. Try updating an existing category entry within the user's document
|
| 956 |
+
update_result = _media_clicks_col.update_one(
|
| 957 |
+
{
|
| 958 |
+
"userId": user_oid,
|
| 959 |
+
"categories.categoryId": category_oid
|
| 960 |
+
},
|
| 961 |
+
{
|
| 962 |
+
"$set": {
|
| 963 |
+
"updatedAt": now,
|
| 964 |
+
"categories.$.lastClickedAt": now
|
| 965 |
+
},
|
| 966 |
+
"$inc": {
|
| 967 |
+
"categories.$.click_count": 1
|
| 968 |
+
}
|
| 969 |
+
}
|
| 970 |
+
)
|
| 971 |
+
|
| 972 |
+
# 2. If no category entry exists (matched_count == 0) -> push a new one (or create user doc)
|
| 973 |
+
if update_result.matched_count == 0:
|
| 974 |
+
_media_clicks_col.update_one(
|
| 975 |
+
{ "userId": user_oid },
|
| 976 |
+
{
|
| 977 |
+
"$setOnInsert": { "createdAt": now },
|
| 978 |
+
"$set": { "updatedAt": now },
|
| 979 |
+
"$push": {
|
| 980 |
+
"categories": {
|
| 981 |
+
"categoryId": category_oid,
|
| 982 |
+
"click_count": 1,
|
| 983 |
+
"lastClickedAt": now
|
| 984 |
+
}
|
| 985 |
+
}
|
| 986 |
+
},
|
| 987 |
+
upsert=True
|
| 988 |
+
)
|
| 989 |
+
|
| 990 |
+
logger.info(f"Media click logged for User {user_id_str} on Category {category_id_str}")
|
| 991 |
+
|
| 992 |
+
except Exception as media_err:
|
| 993 |
+
logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 994 |
+
pass # Handle logging error gracefully
|
| 995 |
|
| 996 |
DEFAULT_FACE_MODEL = 'GFPGANv1.4.pth'
|
| 997 |
DEFAULT_UPSCALE_MODEL = 'SRVGG, realesr-general-x4v3.pth'
|