File size: 2,185 Bytes
6d1acb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
import json
import math

import pytest

from config import MAX_CHARS
from requests_schema import InputError, validate


def valid(**updates):
    values = dict(text="Bonjour !", picker=json.dumps({"voice":"Gaspard","language":"fr_fr"}),
                  source="Preset", seed=42, randomize=False, normalize=True,
                  temperature=.75, top_p=.69, top_k=80, repetition_penalty=2.4,
                  rate=0, voice_names={"Gaspard","Savannah"}, clip_present=False, consent=False)
    values.update(updates)
    return validate(**values)


def test_explicit_language_is_preserved():
    result = valid(picker=json.dumps({"voice":"Savannah","language":"fr_fr"}))
    assert result.voice == "Savannah" and result.language == "fr_fr"


@pytest.mark.parametrize("language", ["hi", "te", "fr", "", "<script>"])
def test_no_silent_english_fallback(language):
    with pytest.raises(InputError):
        valid(picker={"voice":"Gaspard","language":language})


@pytest.mark.parametrize("text", ["", " \n ", "x"*(MAX_CHARS+1), None])
def test_text_rejected_before_inference(text):
    with pytest.raises(InputError):
        valid(text=text)


@pytest.mark.parametrize("field,value", [("temperature",math.nan),("top_k",2.5),("seed",-1),
                                         ("seed",True),("rate",31),("top_p",math.inf)])
def test_numeric_input_does_not_silently_clamp(field,value):
    with pytest.raises(InputError):
        valid(**{field:value})


def test_reference_mode_requires_clip_and_permission():
    with pytest.raises(InputError): valid(source="Voice clip")
    with pytest.raises(InputError): valid(source="Voice clip",clip_present=True)
    assert valid(source="Voice clip",clip_present=True,consent=True).source == "Voice clip"


def test_fixed_seed_and_natural_rate():
    result=valid()
    assert result.seed==42 and result.rate is None


def test_malformed_picker():
    with pytest.raises(InputError): valid(picker="{bad json")


@pytest.mark.parametrize("field", ["voice", "language"])
def test_picker_fields_require_strings(field):
    value={"voice":"Gaspard", "language":"fr_fr"}
    value[field]=[]
    with pytest.raises(InputError): valid(picker=value)