Download core/general/validation.py from owensong/Inflect-Mini-v3: direct link, hf CLI and curl.
- Browser
- Download file 3.15 kB
-
https://huggingface.co/owensong/Inflect-Mini-v3/resolve/main/core/general/validation.py
- Command line
-
hf download hf://owensong/Inflect-Mini-v3/core/general/validation.py
-
curl -L -o validation.py https://huggingface.co/owensong/Inflect-Mini-v3/resolve/main/core/general/validation.py
3.15 kB
| import json | |
| from pathlib import Path | |
| VOICES = ('hope', 'sophia', 'cassidy', 'james', 'jasper', 'mark') | |
| REQUEST_SCHEMA = 'inflect-sixvoice-private-request-v1' | |
| def require(ok, message): | |
| if not ok: | |
| raise ValueError(message) | |
| def read_json(path): | |
| def unique(pairs): | |
| value = {} | |
| for key, item in pairs: | |
| require(key not in value, 'Duplicate JSON key') | |
| value[key] = item | |
| return value | |
| return json.loads(Path(path).read_text(encoding='utf-8'), object_pairs_hook=unique, | |
| parse_constant=lambda value: (_ for _ in ()).throw(ValueError('Nonfinite JSON'))) | |
| def validate_request(request, manifest_hash): | |
| require(isinstance(request, dict) and set(request) == { | |
| 'schema', 'manifest_sha256', 'voice', 'seed', 'original_text', 'spoken_text', | |
| 'numeric_edits', 'chunks', 'frontend_runtime'}, 'Invalid request fields') | |
| require(request['schema'] == REQUEST_SCHEMA and | |
| request['manifest_sha256'] == manifest_hash, 'Request identity differs') | |
| require(type(request['voice']) is str and request['voice'] in VOICES, 'Unknown voice') | |
| require(type(request['seed']) is int and 0 <= request['seed'] < 2**32 - 100, | |
| 'Seed must leave room for chunk increments') | |
| require(all(type(request[k]) is str and 0 < len(request[k]) <= 20000 | |
| for k in ('original_text', 'spoken_text')), 'Invalid text') | |
| require(isinstance(request['numeric_edits'], list) and | |
| isinstance(request['frontend_runtime'], dict), 'Invalid frontend trace') | |
| chunks = request['chunks'] | |
| require(isinstance(chunks, list) and 1 <= len(chunks) <= 100, 'Invalid chunks') | |
| for chunk in chunks: | |
| require(isinstance(chunk, dict) and set(chunk) == { | |
| 'text', 'phones', 'article_rows', 'frontend_edits'}, 'Invalid chunk fields') | |
| require(type(chunk['text']) is str and bool(chunk['text'].strip()) and | |
| type(chunk['phones']) is str and 1 <= len(chunk['phones']) <= 510, | |
| 'Invalid chunk text/phones') | |
| require(isinstance(chunk['article_rows'], list) and | |
| isinstance(chunk['frontend_edits'], dict), 'Invalid chunk trace') | |
| for row in chunk['article_rows']: | |
| require(isinstance(row, dict) and set(row) == {'index', 'donors', 'text', 'phone'}, | |
| 'Invalid article row') | |
| index, donors = row['index'], row['donors'] | |
| require(type(index) is int and 1 <= index <= len(chunk['phones']) and | |
| row['text'] == 'a' and row['phone'] in ('\u0250', '\u0259') and | |
| chunk['phones'][index-1] == row['phone'], 'Invalid article target') | |
| require(isinstance(donors, list) and bool(donors) and | |
| len(set(donors)) == len(donors) and all( | |
| type(i) is int and index < i <= len(chunk['phones']) and | |
| chunk['phones'][i-1] == ' ' for i in donors), 'Invalid article donors') | |
| require(''.join(c['text'] for c in chunks) == request['spoken_text'], | |
| 'Chunks do not preserve spoken text') | |
| return request | |