/
app_noprofile.py
executable file
·203 lines (160 loc) · 5.36 KB
/
app_noprofile.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
import io
import re
import json
import argparse
import subprocess
import logging
import time
import shlex
import tempfile
import threading
import wave
import atexit
import base64
import asyncio
from uuid import uuid4
from pathlib import Path
from typing import Optional, Dict, Any, Tuple, BinaryIO, List
import pydash
from quart import (
Quart,
request,
render_template,
send_from_directory,
flash,
send_file,
jsonify,
)
# -----------------------------------------------------------------------------
profile_path: Optional[Path] = None
download_dir: Optional[Path] = None
# Quart application
app = Quart("voice2json", template_folder=Path("templates").absolute())
app.secret_key = str(uuid4())
logger = logging.getLogger("app_noprofile")
loop = asyncio.get_event_loop()
# -----------------------------------------------------------------------------
class Info:
def __init__(
self,
description: str,
version: str,
accuracy_closed: int,
speed_closed: int,
accuracy_open: int,
speed_open: int,
):
self.version = version
self.description = description
self.accuracy_closed = accuracy_closed
self.speed_closed = speed_closed
self.accuracy_open = accuracy_open
self.speed_open = speed_open
def download_path(self, name, download_dir):
return download_dir / f"{name}_v{self.version}.tar.gz"
PROFILES = {
"German (Deutsch)": {
"de-de": {
"de_kaldi-zamia": Info("Kaldi TDNN Zamia", 1.0, 5, 3, 5, 3),
"de_pocketsphinx-cmu": Info("Pocketsphinx CMU", 1.0, 5, 29, 5, 5),
}
},
"English": {
"en-us": {"en-us_pocketsphinx-cmu": Info("Pocketsphinx CMU", 1.0, 3, 36, 0, 6)}
},
}
# -----------------------------------------------------------------------------
@app.route("/", methods=["GET", "POST"])
async def index():
if request.method == "POST":
form = await request.form
for key in form:
if key.startswith("download-"):
profile_name = key.split("-", maxsplit=1)[1]
await flash(f"Downloading {profile_name}", "info")
break
elif key.startswith("install-"):
profile_name = key.split("-", maxsplit=1)[1]
await install_profile(profile_name)
await flash(f"Installed {profile_name}", "success")
break
return await render_template(
"index_noprofile.html",
profile_path=profile_path,
download_dir=download_dir,
profiles=PROFILES,
sorted=sorted,
range=range,
)
# @app.route("/download")
# async def download():
# return await render_template(
# "download_noprofile.html",
# profile_path=profile_path,
# profiles=PROFILES,
# sorted=sorted,
# range=range,
# )
# -----------------------------------------------------------------------------
# Static Routes
# -----------------------------------------------------------------------------
@app.route("/css/<path:filename>", methods=["GET"])
def css(filename):
return send_from_directory("css", filename)
@app.route("/js/<path:filename>", methods=["GET"])
def js(filename):
return send_from_directory("js", filename)
@app.route("/img/<path:filename>", methods=["GET"])
def img(filename):
return send_from_directory("img", filename)
@app.errorhandler(Exception)
def handle_error(err) -> Tuple[str, int]:
logger.exception(err)
return (str(err), 500)
# -----------------------------------------------------------------------------
async def install_profile(name):
profile_dir = profile_path
if str(profile_dir).endswith(".yml"):
profile_dir = profile_path.parent
info = None
for lang_profiles in PROFILES.values():
for locale_profiles in lang_profiles.values():
info = locale_profiles.get(name)
if info is not None:
break
if info is not None:
break
assert info is not None, f"No profile named {name}"
tar_gz_path = info.download_path(name, download_dir)
command = ["tar", "-C", str(profile_dir), "-xvf", str(tar_gz_path)]
logger.debug(command)
profile_dir.mkdir(parents=True, exist_ok=True)
await asyncio.create_subprocess_exec(
*command
)
# -----------------------------------------------------------------------------
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true", help="Enable DEBUG mode")
parser.add_argument(
"--http-port", type=int, default=5000, help="Web server port (default: 5000)"
)
parser.add_argument(
"--http-host", default="127.0.0.1", help="Web server host (default: 127.0.0.1)"
)
parser.add_argument("--profile", required=True, help="Path to voice2json profile")
parser.add_argument("--cache", required=True, help="Path to download cache")
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
logger.debug(args)
profile_path = Path(args.profile)
download_dir = Path(args.cache)
# Start web server
try:
app.run(port=args.http_port, host=args.http_host, debug=args.debug)
except KeyboardInterrupt:
pass