Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,6 +32,10 @@ import shutil
|
|
| 32 |
from utils.dataops import auto_split_upscale
|
| 33 |
from typing import List, Optional
|
| 34 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Writable directories (Spaces often allow /tmp). Override via env if needed.
|
| 36 |
facex_misc.ROOT_DIR = os.environ["FACEXLIB_CACHE_DIR"]
|
| 37 |
OUTPUT_DIR = os.environ.get("OUTPUT_DIR", "/tmp/face_upscale/output")
|
|
@@ -80,12 +84,23 @@ try:
|
|
| 80 |
_mongo_uri = os.getenv("MONGODB_URI")
|
| 81 |
_mongo_db = os.getenv("MONGODB_DB", "face_upscale")
|
| 82 |
_mongo_col = os.getenv("MONGODB_COLLECTION", "submits")
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception:
|
|
|
|
| 87 |
_mongo_client = None
|
| 88 |
_mongo_collection = None
|
|
|
|
|
|
|
| 89 |
|
| 90 |
input_images_limit = 5
|
| 91 |
|
|
@@ -851,6 +866,60 @@ class Upscale:
|
|
| 851 |
is_success, im_buf_arr = cv2.imencode(extension, image)
|
| 852 |
if is_success:
|
| 853 |
im_buf_arr.tofile(save_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
|
| 855 |
class Timer:
|
| 856 |
def __init__(self):
|
|
@@ -888,6 +957,8 @@ class Timer:
|
|
| 888 |
self.start_time = time.perf_counter()
|
| 889 |
self.checkpoints = [("Start", self.start_time)]
|
| 890 |
|
|
|
|
|
|
|
| 891 |
if __name__ == "__main__":
|
| 892 |
if torch.cuda.is_available():
|
| 893 |
torch.cuda.set_per_process_memory_fraction(0.900, device='cuda:0')
|
|
@@ -1011,6 +1082,10 @@ async def submit(
|
|
| 1011 |
face_detection_threshold: float = Form(DEFAULT_FACE_DET_THRESHOLD),
|
| 1012 |
face_detection_only_center: bool = Form(DEFAULT_ONLY_CENTER),
|
| 1013 |
with_model_name: bool = Form(DEFAULT_WITH_MODEL_NAME),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1014 |
_: bool = Depends(_verify_bearer_token)
|
| 1015 |
):
|
| 1016 |
start_time = datetime.utcnow() # Start timer
|
|
@@ -1043,6 +1118,26 @@ async def submit(
|
|
| 1043 |
save_as_png=True,
|
| 1044 |
progress=progress_stub,
|
| 1045 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1046 |
|
| 1047 |
# --- Filter only final outputs ---
|
| 1048 |
final_images = [
|
|
|
|
| 32 |
from utils.dataops import auto_split_upscale
|
| 33 |
from typing import List, Optional
|
| 34 |
from datetime import datetime
|
| 35 |
+
|
| 36 |
+
import asyncio
|
| 37 |
+
from bson.objectid import ObjectId
|
| 38 |
+
# -------------------
|
| 39 |
# Writable directories (Spaces often allow /tmp). Override via env if needed.
|
| 40 |
facex_misc.ROOT_DIR = os.environ["FACEXLIB_CACHE_DIR"]
|
| 41 |
OUTPUT_DIR = os.environ.get("OUTPUT_DIR", "/tmp/face_upscale/output")
|
|
|
|
| 84 |
_mongo_uri = os.getenv("MONGODB_URI")
|
| 85 |
_mongo_db = os.getenv("MONGODB_DB", "face_upscale")
|
| 86 |
_mongo_col = os.getenv("MONGODB_COLLECTION", "submits")
|
| 87 |
+
_admin_mongo_client = None # NEW client for media clicks
|
| 88 |
+
_media_clicks_col = None # NEW collection for media clicks
|
| 89 |
+
# 2. Media Clicks Connection (using MONGODB_ADMIN_URI, as requested)
|
| 90 |
+
_admin_mongo_uri = os.getenv("MONGODB_ADMIN_URI")
|
| 91 |
+
_admin_mongo_db = os.getenv("MONGODB_ADMIN_DB", "face_upscale_admin")
|
| 92 |
+
_media_clicks_col_name = "media_clicks"
|
| 93 |
+
|
| 94 |
+
if _admin_mongo_uri:
|
| 95 |
+
_admin_mongo_client = MongoClient(_admin_mongo_uri, connect=False)
|
| 96 |
+
_media_clicks_col = _admin_mongo_client[_admin_mongo_db][_media_clicks_col_name]
|
| 97 |
+
|
| 98 |
except Exception:
|
| 99 |
+
# If any MongoDB connection fails, ensure all related variables are None
|
| 100 |
_mongo_client = None
|
| 101 |
_mongo_collection = None
|
| 102 |
+
_admin_mongo_client = None
|
| 103 |
+
_media_clicks_col = None
|
| 104 |
|
| 105 |
input_images_limit = 5
|
| 106 |
|
|
|
|
| 866 |
is_success, im_buf_arr = cv2.imencode(extension, image)
|
| 867 |
if is_success:
|
| 868 |
im_buf_arr.tofile(save_path)
|
| 869 |
+
|
| 870 |
+
def sync_log_media_click(user_id_str: str, category_id_str: str):
|
| 871 |
+
"""
|
| 872 |
+
Synchronously logs a click event to the media_clicks collection.
|
| 873 |
+
This function should be run using asyncio.to_thread().
|
| 874 |
+
"""
|
| 875 |
+
if _media_clicks_col is None:
|
| 876 |
+
return
|
| 877 |
+
|
| 878 |
+
try:
|
| 879 |
+
user_oid = ObjectId(user_id_str.strip())
|
| 880 |
+
category_oid = ObjectId(category_id_str.strip())
|
| 881 |
+
now = datetime.utcnow()
|
| 882 |
+
|
| 883 |
+
# 1. Try updating an existing category entry within the user's document
|
| 884 |
+
update_result = _media_clicks_col.update_one(
|
| 885 |
+
{
|
| 886 |
+
"userId": user_oid,
|
| 887 |
+
"categories.categoryId": category_oid
|
| 888 |
+
},
|
| 889 |
+
{
|
| 890 |
+
"$set": {
|
| 891 |
+
"updatedAt": now,
|
| 892 |
+
"categories.$.lastClickedAt": now
|
| 893 |
+
},
|
| 894 |
+
"$inc": {
|
| 895 |
+
"categories.$.click_count": 1
|
| 896 |
+
}
|
| 897 |
+
}
|
| 898 |
+
)
|
| 899 |
+
|
| 900 |
+
# 2. If no category entry exists (matched_count == 0) -> push a new one (or create user doc)
|
| 901 |
+
if update_result.matched_count == 0:
|
| 902 |
+
_media_clicks_col.update_one(
|
| 903 |
+
{ "userId": user_oid },
|
| 904 |
+
{
|
| 905 |
+
"$setOnInsert": { "createdAt": now },
|
| 906 |
+
"$set": { "updatedAt": now },
|
| 907 |
+
"$push": {
|
| 908 |
+
"categories": {
|
| 909 |
+
"categoryId": category_oid,
|
| 910 |
+
"click_count": 1,
|
| 911 |
+
"lastClickedAt": now
|
| 912 |
+
}
|
| 913 |
+
}
|
| 914 |
+
},
|
| 915 |
+
upsert=True
|
| 916 |
+
)
|
| 917 |
+
|
| 918 |
+
# logger.info(f"Media click logged for User {user_id_str} on Category {category_id_str}")
|
| 919 |
+
|
| 920 |
+
except Exception as media_err:
|
| 921 |
+
# logger.error(f"MEDIA_CLICK LOGGING ERROR: {media_err}")
|
| 922 |
+
pass # Handle logging error gracefully
|
| 923 |
|
| 924 |
class Timer:
|
| 925 |
def __init__(self):
|
|
|
|
| 957 |
self.start_time = time.perf_counter()
|
| 958 |
self.checkpoints = [("Start", self.start_time)]
|
| 959 |
|
| 960 |
+
|
| 961 |
+
|
| 962 |
if __name__ == "__main__":
|
| 963 |
if torch.cuda.is_available():
|
| 964 |
torch.cuda.set_per_process_memory_fraction(0.900, device='cuda:0')
|
|
|
|
| 1082 |
face_detection_threshold: float = Form(DEFAULT_FACE_DET_THRESHOLD),
|
| 1083 |
face_detection_only_center: bool = Form(DEFAULT_ONLY_CENTER),
|
| 1084 |
with_model_name: bool = Form(DEFAULT_WITH_MODEL_NAME),
|
| 1085 |
+
# --- NEW OPTIONAL FIELDS ---
|
| 1086 |
+
category_id: Optional[str] = Form(None),
|
| 1087 |
+
user_id: Optional[str] = Form(None),
|
| 1088 |
+
# ---------------------------
|
| 1089 |
_: bool = Depends(_verify_bearer_token)
|
| 1090 |
):
|
| 1091 |
start_time = datetime.utcnow() # Start timer
|
|
|
|
| 1118 |
save_as_png=True,
|
| 1119 |
progress=progress_stub,
|
| 1120 |
)
|
| 1121 |
+
# -----------------------------------------------------------------
|
| 1122 |
+
# NEW: MEDIA CLICK LOGGING (AFTER SUCCESSFUL CORE PROCESSING)
|
| 1123 |
+
# -----------------------------------------------------------------
|
| 1124 |
+
# Only log if both optional fields are provided and look like valid ObjectIds
|
| 1125 |
+
if user_id and category_id:
|
| 1126 |
+
try:
|
| 1127 |
+
# Validate the format before passing to the synchronous function
|
| 1128 |
+
ObjectId(user_id.strip())
|
| 1129 |
+
ObjectId(category_id.strip())
|
| 1130 |
+
|
| 1131 |
+
# Run the blocking log operation in a separate thread safely
|
| 1132 |
+
asyncio.to_thread(
|
| 1133 |
+
sync_log_media_click,
|
| 1134 |
+
user_id,
|
| 1135 |
+
category_id
|
| 1136 |
+
)
|
| 1137 |
+
except Exception as oid_err:
|
| 1138 |
+
# Log a warning if the IDs were provided but invalid
|
| 1139 |
+
# logger.warning(f"Skipping media click log due to invalid ID format: {oid_err}")
|
| 1140 |
+
pass
|
| 1141 |
|
| 1142 |
# --- Filter only final outputs ---
|
| 1143 |
final_images = [
|