Spaces:
Sleeping
Sleeping
Download api/utils/user.py from mafzaal/Quick-Understand: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/spaces/mafzaal/Quick-Understand/resolve/main/api/utils/user.py
- Command line
-
hf download hf://spaces/mafzaal/Quick-Understand/api/utils/user.py
-
curl -L -o user.py https://huggingface.co/spaces/mafzaal/Quick-Understand/resolve/main/api/utils/user.py
1.08 kB
| import time | |
| import uuid | |
| from fastapi import Request, Response | |
| from .prompts import user_prompts, DEFAULT_SYSTEM_TEMPLATE, DEFAULT_USER_TEMPLATE | |
| def get_or_create_user_id(request: Request, response: Response) -> str: | |
| """ | |
| Get or create a user ID without using cookies to support HuggingFace deployments | |
| Args: | |
| request: FastAPI request object | |
| response: FastAPI response object | |
| Returns: | |
| User ID (either existing or newly created) | |
| """ | |
| # Try to get user ID from header first | |
| user_id = request.headers.get("X-User-ID") | |
| # Then try to get from query parameter | |
| if not user_id: | |
| user_id = request.query_params.get("user_id") | |
| # If no user ID exists, create a new one | |
| if not user_id: | |
| user_id = str(uuid.uuid4()) | |
| # Initialize with default prompts | |
| if user_id not in user_prompts: | |
| user_prompts[user_id] = { | |
| "system_template": DEFAULT_SYSTEM_TEMPLATE, | |
| "user_template": DEFAULT_USER_TEMPLATE | |
| } | |
| return user_id |