From 1844f5b6ea379f529d19b6883ad8e9d94b220e72 Mon Sep 17 00:00:00 2001 From: Only1337 <47225461+Only1337@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:30:23 +0300 Subject: [PATCH] Added show other translations option --- main.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/main.py b/main.py index f28958f..bc99541 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,8 @@ session = r.session() +show_other_translations = False # if you want to show other translations, set this to True (it will be slower) + def translate(text, input_lang, output_lang): # i make a request to the deepl translate without the api key # and i get the cookie from the response @@ -37,11 +39,29 @@ def translate(text, input_lang, output_lang): "id": 40890006, }, ) + # i get the translated text from the response - translated_text = response.json()["result"]["translations"][0]["beams"][0]["postprocessed_sentence"] - return translated_text + # translated_text = response.json()["result"]["translations"][0]["beams"][0]["postprocessed_sentence"] + + if show_other_translations: + translate_response = { + "text": response.json()["result"]["translations"][0]["beams"][0]["postprocessed_sentence"], + "other_translations": {} + } + + index = 0 + for i in response.json()["result"]["translations"][0]["beams"]: + if not index == 0: + translate_response["other_translations"][index-1] = i["postprocessed_sentence"] + index += 1 + + return translate_response + else: + translated_text = response.json()["result"]["translations"][0]["beams"][0]["postprocessed_sentence"] + return translated_text + -# print(translate("Bu gün nasılsın dostum?\nBu bir satır denemesidir.", "TR", "EN")) +# print(translate("""Bu gün nasılsın dostum?\nBu bir satır denemesidir.""", "TR", "EN")) app = Flask(__name__) @@ -54,10 +74,9 @@ def translate_text(): if not text.strip(): return jsonify({'success': 'false', 'message': 'Text data cannot be empty'}) - translated_text = translate(text, input_lang, output_lang) + translated_data = translate(text, input_lang, output_lang) - return jsonify({'success': 'true', 'translated': {'text': translated_text}}) - # return jsonify({'success': 'true', 'translated': {'lines': translated_lines, 'text': translated_text}}) + return jsonify({'success': 'true', 'translated': {'text': translated_data}}) if __name__ == '__main__': app.run(host='0.0.0.0', port=30)