Spaces:
Sleeping
Sleeping
| import re | |
| from fastapi import Request | |
| def get_ip(request: Request) -> str: | |
| forwarded = request.headers.get("X-Forwarded-For") | |
| if forwarded: | |
| return forwarded.split(",")[0].strip() | |
| return request.client.host if request.client else "unknown" | |
| def is_default_key(key: str, default: str) -> bool: | |
| if not key or not default: | |
| return False | |
| return key.strip() == default.strip() | |
| def get_cloudinary_creds(url: str) -> dict: | |
| if not url or not url.startswith("cloudinary://"): | |
| return {} | |
| try: | |
| creds = url.replace("cloudinary://", "") | |
| auth, cloud_name = creds.split("@") | |
| api_key, api_secret = auth.split(":") | |
| return { | |
| "cloud_name": cloud_name, | |
| "api_key": api_key, | |
| "api_secret": api_secret | |
| } | |
| except ValueError: | |
| return {} | |
| def sanitize_filename(filename: str) -> str: | |
| if not filename: | |
| return "unnamed_file" | |
| return re.sub(r'[^a-zA-Z0-9_\-\.]', '_', filename) | |
| def standardize_category_name(name: str) -> str: | |
| if not name: | |
| return "uncategorized" | |
| return re.sub(r'[^a-zA-Z0-9_\-]', '_', name.lower()) | |
| def to_list(vector) -> list[float]: | |
| try: | |
| return [float(x) for x in vector] | |
| except TypeError: | |
| return [] | |
| def url_to_public_id(url: str) -> str: | |
| if not url: | |
| return "" | |
| try: | |
| parts = url.split("/upload/") | |
| if len(parts) > 1: | |
| path = parts[1].split("/", 1)[-1] | |
| return path.rsplit(".", 1)[0] | |
| return "" | |
| except Exception: | |
| return "" | |
| def cld_thumb_url(url: str) -> str: | |
| if not url: | |
| return "" | |
| return url.replace("/upload/", "/upload/c_thumb,w_200,g_face/") | |
| def face_ui_score(raw_score: float) -> float: | |
| return min(100.0, max(0.0, round(raw_score * 100, 2))) |