-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.py
110 lines (90 loc) · 4.41 KB
/
server.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
from flask import Flask, send_file, request, make_response
import codecs
import subprocess
import time
import random
import Shakkala as sh
from keras.models import load_model
from shakkelha.shakkelha import *
from shakkelha.optimizer import *
import pyarabic.number
an = pyarabic.number.ArNumbers()
shakkelha_mod = load_model('models/shakkelha.h5')
shakkelha_mod._make_predict_function()
app = Flask(__name__)
sh = sh.Shakkala("/root/Shakkala/", version=3)
model, graph = sh.get_model()
def num_to_word(text):
text = re.sub('([\\d]+)', lambda x:' ' + an.int2str(x.group(0)) + ' ', text)
return text
@app.route('/mishkal/synth/<mode>/<text>')
def mishkal(mode, text):
try:
rand = str(random.randint(0,99999))
exit_codes = []
text = num_to_word(text)
p1 = subprocess.Popen(["python", "/root/mishkal/bin/mishkal-console.py", text], stdout=open("/tts/stage1_" + rand, 'w'), stderr=open("/tts/p1err", 'w'))
exit_codes.append(p1.wait())
p2 = subprocess.Popen(["python3", "/root/filter.py", "/tts/stage1_" + rand, "/tts/stage2_" + rand], stderr=open("/tts/p2err", 'w'))
exit_codes.append(p2.wait())
p3 = subprocess.Popen(["text2wave", "-eval", "(voice_ara_norm_ziad_hts)", "-o", "/tts/out_" + rand + ".wav"], stdin=open("/tts/stage2_" + rand, 'r'), stderr=open("/tts/p3err", 'w'))
exit_codes.append(p3.wait())
if mode == 'file':
return send_file('/tts/out_' + rand + '.wav', attachment_filename='out.wav', mimetype='audio/wav')
if mode == 'url':
resp = make_response('{"url": "out_' + rand + '.wav"}', 200)
resp.mimetype = 'application/json'
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
except Exception as e:
return str(e) + str(exit_codes)
@app.route('/shakkala/synth/<mode>/<text>')
def shakkala(mode, text):
try:
rand = str(random.randint(0,99999))
exit_codes = []
text = num_to_word(text)
input_int = sh.prepare_input(text)
with graph.as_default():
logits = model.predict(input_int)[0]
predicted_harakat = sh.logits_to_text(logits)
final_output = sh.get_final_text(text, predicted_harakat)
with open('/tts/stage1_' + rand, 'w') as f:
f.write(final_output)
p1 = subprocess.Popen(["python3", "/root/filter.py", "/tts/stage1_" + rand, "/tts/stage2_" + rand], stderr=open("/tts/p2err", 'w'))
exit_codes.append(p1.wait())
p2 = subprocess.Popen(["text2wave", "-eval", "(voice_ara_norm_ziad_hts)", "-o", "/tts/out_" + rand + ".wav"], stdin=open("/tts/stage2_" + rand, 'r'), stderr=open("/tts/p3err", 'w'))
exit_codes.append(p2.wait())
if mode == 'file':
return send_file('/tts/out_' + rand + '.wav', attachment_filename='out.wav', mimetype='audio/wav')
if mode == 'url':
resp = make_response('{"url": "out_' + rand + '.wav"}', 200)
resp.mimetype = 'application/json'
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
except Exception as e:
return str(e) + str(exit_codes)
@app.route('/shakkelha/synth/<mode>/<text>')
def shakkelha(mode, text):
try:
rand = str(random.randint(0,9999999999))
exit_codes = []
final_output = predict(num_to_word(text), shakkelha_mod)
with open('/tts/stage1_' + rand, 'w') as f:
f.write(final_output)
p1 = subprocess.Popen(["python3", "/root/filter.py", "/tts/stage1_" + rand, "/tts/stage2_" + rand], stderr=open("/tts/p2err", 'w'))
exit_codes.append(p1.wait())
p2 = subprocess.Popen(["text2wave", "-eval", "(voice_ara_norm_ziad_hts)", "-o", "/tts/out_" + rand + ".wav"],
stdin=open("/tts/stage2_" + rand, 'r'), stderr=open("/tts/p3err", 'w'))
exit_codes.append(p2.wait())
if mode == 'file':
return send_file('/tts/out_' + rand + '.wav', attachment_filename='out.wav', mimetype='audio/wav')
if mode == 'url':
resp = make_response('{"url": "out_' + rand + '.wav"}', 200)
resp.mimetype = 'application/json'
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
except Exception as e:
return str(e) + str(exit_codes)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)