|
from flask import Flask, request, jsonify |
|
import subprocess |
|
import tempfile |
|
import asyncio |
|
import time |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/translate_text', methods=['POST']) |
|
def translate_text(): |
|
data = request.json |
|
encoded_text = data['encoded_text'] |
|
model_path = data['model_path'] |
|
gpu_id = data.get('gpu_id', 0) |
|
|
|
start_time = time.time() |
|
|
|
with tempfile.NamedTemporaryFile('w+', delete=False) as temp_input_file, \ |
|
tempfile.NamedTemporaryFile('w+', delete=False) as temp_output_file: |
|
temp_input_file.write(encoded_text) |
|
temp_input_file.flush() |
|
|
|
result = subprocess.run( |
|
[ |
|
'onmt_translate', |
|
'-model', model_path, |
|
'-src', temp_input_file.name, |
|
'-output', temp_output_file.name, |
|
'-gpu', str(gpu_id), |
|
'-min_length', '1' |
|
], |
|
stdout=subprocess.PIPE, |
|
stderr=subprocess.PIPE |
|
) |
|
|
|
with open(temp_output_file.name, 'r') as output_file: |
|
translated_text = output_file.read().strip() |
|
|
|
error_text = result.stderr.decode('utf-8') |
|
|
|
return jsonify({'translated_text': translated_text, 'error': error_text}) |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=5003) |
|
|