File size: 3,145 Bytes
07b13d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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