-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
96 lines (75 loc) · 2.78 KB
/
cli.py
File metadata and controls
96 lines (75 loc) · 2.78 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import os
import sys
import click
from speechpro.cloud.speech import recognition
from speechpro.cloud.speech import synthesis
recognitionClient = recognition.RecognitionClient(
os.environ['SPEECHPRO_USERNAME'],
os.environ['SPEECHPRO_DOMAIN_ID'],
os.environ['SPEECHPRO_PASSWORD']
)
synthesisClient = synthesis.SynthesisClient(
os.environ['SPEECHPRO_USERNAME'],
os.environ['SPEECHPRO_DOMAIN_ID'],
os.environ['SPEECHPRO_PASSWORD']
)
def recognize(language, model, response_type, filename, audio_channel_count=1):
with open(filename, "rb") as f:
content = f.read()
config = {
'language': language,
'model': model,
'encoding': recognition.enums.AudioEncoding.WAV,
'response_type': response_type,
'audio_channel_count': audio_channel_count
}
return recognitionClient.recognize(config, content)
@click.group()
def cli():
pass
@cli.command()
@click.option('--language', default=recognition.enums.Language.RU)
@click.option('--model', default=recognition.enums.Model.GENERAL)
@click.option('--filename', type=str,)
def recognize_plain_text(language, model, filename):
result = recognize(language, model, recognition.enums.ResponseType.PLAIN_TEXT, filename)
print(result.text)
return 0
@cli.command()
@click.option('--language', default=recognition.enums.Language.RU)
@click.option('--model', default=recognition.enums.Model.GENERAL)
@click.option('--filename', type=str,)
def recognize_word_list(language, model, filename):
result = recognize(language, model, recognition.enums.ResponseType.WORD_LIST, filename)
for w in result:
print(w.word)
return 0
@cli.command()
@click.option('--language', default=recognition.enums.Language.RU)
@click.option('--model', default=recognition.enums.Model.GENERAL)
@click.option('--filename', type=str,)
@click.option('--audio_channel_count', type=int,)
def recognize_multichannel(language, model, filename, audio_channel_count):
result = recognize(language, model, recognition.enums.ResponseType.MULTICHANNEL, filename, audio_channel_count)
for ch in result:
print(f'Channel {ch.channel_id}')
for w in ch.result:
print(w.word)
print('\n')
return 0
@cli.command()
@click.option('--voice', default=synthesis.enums.Voice.JULIA)
@click.option('--profile', default=synthesis.enums.PlaybackProfile.SPEAKER)
@click.option('--output', type=str)
@click.option('--input', type=str)
def synthesize(voice, profile, output, input):
try:
with open(input) as f:
text = f.read()
except:
text = input
audio = synthesisClient.synthesize(voice, profile, text)
with open(output, 'wb') as outputfile:
outputfile.write(audio)
if __name__ == "__main__":
sys.exit(cli()) # pragma: no cover