Spaces:
Sleeping
Sleeping
Download protocol.py from kuldeepsinghpro/hervoice-analysis: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/kuldeepsinghpro/hervoice-analysis/resolve/main/protocol.py
- Command line
-
hf download hf://spaces/kuldeepsinghpro/hervoice-analysis/protocol.py
-
curl -L -o protocol.py https://huggingface.co/spaces/kuldeepsinghpro/hervoice-analysis/resolve/main/protocol.py
1.03 kB
| """Input validation; this module has no ML dependencies.""" | |
| import base64 | |
| import hmac | |
| import json | |
| def authorize(key, expected): | |
| if not expected or not isinstance(key, str) or not hmac.compare_digest(key, expected): | |
| raise ValueError("Unauthorized") | |
| def payload(raw): | |
| if not isinstance(raw, str) or len(raw) > 180 * 1024 * 1024: | |
| raise ValueError("Invalid request size") | |
| value = json.loads(raw) | |
| if not isinstance(value, dict): | |
| raise ValueError("Expected object") | |
| return value | |
| def media(encoded, limit=10 * 1024 * 1024): | |
| if not isinstance(encoded, str) or len(encoded) > 4 * ((limit + 2) // 3): | |
| raise ValueError("Invalid media size") | |
| raw = base64.b64decode(encoded, validate=True) | |
| if not raw or len(raw) > limit: | |
| raise ValueError("Invalid media size") | |
| return raw | |
| def images(items, limit): | |
| if not isinstance(items, list) or not 1 <= len(items) <= limit: | |
| raise ValueError("Invalid image count") | |
| return [media(item) for item in items] | |