Skip to content

Commit

Permalink
Added show other translations option
Browse files Browse the repository at this point in the history
  • Loading branch information
illegalsolutions committed Oct 10, 2023
1 parent 99da518 commit 1844f5b
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__)

Expand All @@ -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)

0 comments on commit 1844f5b

Please sign in to comment.