-
Notifications
You must be signed in to change notification settings - Fork 2
/
seamlessm4tv2_api_s.py
99 lines (81 loc) · 4.14 KB
/
seamlessm4tv2_api_s.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
# text_translate_api.py
from model_initializer import initialize_model
from flask import Flask, request, jsonify
import sounddevice as sd
from flask_cors import CORS # Import CORS from flask_cors
model, processor, device = initialize_model()
app = Flask(__name__)
CORS(app) # Enable CORS for all routes in your Flask app
# Serve static files from the "static" folder
@app.route('/')
def index():
return app.send_static_file('index_s.html')
@app.route('/t2t', methods=['POST'])
def t2t():
input_text = request.json.get('inputText', '')
srcLang = request.json.get('srcLang', '')
tgtLang = request.json.get('tgtLang', '')
print(f"t2t input_text: {input_text}, srcLang: {srcLang}, tgtLang: {tgtLang}")
# process input
text_inputs = processor(text=f"{input_text}", src_lang=srcLang, return_tensors="pt").to(device)
# generate translation
output_tokens = model.generate(**text_inputs, tgt_lang=tgtLang, generate_speech=False)
translated_text_from_text = processor.decode(output_tokens[0].tolist()[0], skip_special_tokens=True)
return jsonify({'processedText': translated_text_from_text})
@app.route('/t2s', methods=['POST'])
def t2s():
input_text = request.json.get('inputText', '')
srcLang = request.json.get('srcLang', '')
tgtLang = request.json.get('tgtLang', '')
print(f"t2t input_text: {input_text}, srcLang: {srcLang}, tgtLang: {tgtLang}")
# process input
text_inputs = processor(text=f"{input_text}", src_lang=srcLang, return_tensors="pt").to(device)
# generate translation audio
audio_array_from_text = model.generate(**text_inputs, tgt_lang=tgtLang)[0].cpu().numpy().squeeze()
# Set the sample rate
sample_rate = model.config.sampling_rate
print(f"sample_rate: {sample_rate}")
return jsonify({'audioData': audio_array_from_text.tolist(), 'sample_rate': sample_rate})
@app.route('/s2t', methods=['POST'])
def s2t():
audio_sample = request.json.get('audioSample', '')
sample_rate = request.json.get('sampleRate', '')
srcLang = request.json.get('srcLang', '')
tgtLang = request.json.get('tgtLang', '')
print(f"s2t sample_rate: {sample_rate} srcLang: {srcLang}, tgtLang: {tgtLang}")
# sd.play(audio_sample, sample_rate)
# sd.wait() # Wait for playback to finish
audio_inputs = processor(audios=audio_sample, sampling_rate=sample_rate, return_tensors="pt").to(device)
print(f"s2t after audio_inputs")
output_tokens = model.generate(**audio_inputs, tgt_lang=tgtLang, generate_speech=False)
print(f"s2t after output_tokens")
translated_text_from_audio = processor.decode(output_tokens[0].tolist()[0], skip_special_tokens=True)
print(f"s2t after translated_text_from_audio: {translated_text_from_audio}")
return jsonify({'processedText': translated_text_from_audio})
@app.route('/s2s', methods=['POST'])
def s2s():
audio_sample = request.json.get('audioSample', '')
sample_rate = request.json.get('sampleRate', '')
srcLang = request.json.get('srcLang', '')
tgtLang = request.json.get('tgtLang', '')
print(f"s2s sample_rate: {sample_rate} srcLang: {srcLang}, tgtLang: {tgtLang}")
# sd.play(audio_sample, sample_rate)
# sd.wait() # Wait for playback to finish
audio_inputs = processor(audios=audio_sample, sampling_rate=sample_rate, return_tensors="pt").to(device)
print(f"s22 after audio_inputs")
audio_array_from_audio = model.generate(**audio_inputs, tgt_lang=tgtLang)[0].cpu().numpy().squeeze()
# Set the sample rate
sample_rate = model.config.sampling_rate
print(f"sample_rate: {sample_rate}")
return jsonify({'audioData': audio_array_from_audio.tolist(), 'sample_rate': sample_rate})
import os
if __name__ == '__main__':
# app.run(debug=True)
# app.run(debug=True, host='0.0.0.0', port=5000)
cert_path = os.path.abspath('cert.pem')
key_path = os.path.abspath('key.pem')
# Use the generated SSL certificate and key
# app.run(host='0.0.0.0', port=5000, ssl_context=('cert.pem', 'key.pem'), debug=True)
print(f"Using SSL certificate: {cert_path}")
print(f"Using SSL key: {key_path}")
app.run(host='0.0.0.0', port=5502, ssl_context=(cert_path, key_path), debug=False)