You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
garage-AI/app/functions.py

62 lines
1.6 KiB
Python

import argostranslate.package
import argostranslate.translate
def install_language_package(from_code, to_code):
argostranslate.package.update_package_index()
available_packages = argostranslate.package.get_available_packages()
# Install package for translation from 'from_code' to 'to_code'
package_to_install = next(
filter(
lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
)
)
argostranslate.package.install_from_path(package_to_install.download())
# Install package for translation from 'to_code' to 'from_code'
package_to_install_reverse = next(
filter(
lambda x: x.from_code == to_code and x.to_code == from_code, available_packages
)
)
argostranslate.package.install_from_path(package_to_install_reverse.download())
def translate(text, from_code, to_code):
translated_text = argostranslate.translate.translate(text, from_code, to_code)
return translated_text
from_code = "en"
to_code = "fr"
# Install language packages for translation in both directions
install_language_package(from_code, to_code)
# FONCTIONS
# Fonctions trad
def fr_to_en(to_trad):
translatedText = translate(to_trad, "fr", "en")
return translatedText
def en_to_fr(to_trad):
translatedText = translate(to_trad, "en", "fr")
return translatedText
# Fonctions split et join
def split_str(text):
texts = []
try:
texts = text.split("```")
except:
pass
return texts
def join_str(texts):
if len(texts) != 0:
text = "```".join(texts)
return text