Added Python Script to run argos translate

This commit is contained in:
Falko Victor Habel 2024-03-13 18:58:13 +01:00
parent b9982d191f
commit 5a41ee0cdd
5 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import json
from datetime import datetime, timezone
def create_respone(translated, from_code, to_code, error):
# Creating a dictionary that matches the structure of your JSON
data = {
"from": from_code,
"to": to_code,
"created_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f%z"),
"response": translated,
"error": error
}
json_string = json.dumps(data, indent=4)
# Printing the JSON string to the console
print(json_string)

32
scripts/python/main.py Normal file
View File

@ -0,0 +1,32 @@
import sys
from translate import translate
from package_mangement.downlaod_languages import install_language
from package_mangement.check_packages import check_languages_installed
from create_response import create_respone
def main():
message = sys.argv[1]
from_lang = sys.argv[2]
to_lang = sys.argv[3]
# Check if languages are installed
is_installed = check_languages_installed(from_lang, to_lang)
if not is_installed:
# Attempt to install the languages
installed = install_language(from_lang, to_lang)
if installed != 0:
# Installation failed
error = "Error: Installation failed"
create_respone("None", from_lang, to_lang, error)
return # Exit the function early
# Languages are installed or installation succeeded
translated = translate(message, from_lang, to_lang)
if translated:
create_respone(translated, from_lang, to_lang, error = "None")
else:
create_respone("None", from_lang, to_lang, error = "Could not translate it.")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,28 @@
import csv
def check_languages_installed(from_code, to_code):
"""
Check if the language codes match the given criteria in a predefined .csv file.
Parameters:
- from_code (str): The source language code to check.
- to_code (str): The target language code to check.
Outputs:
- Prints True if a matching row is found, else prints False.
"""
try:
# Assuming the CSV file is named 'languages.csv' and located in the same directory
with open('installed_packages_info.csv', mode='r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
# Check if the current row matches the criteria
if len(row) >= 2 and row[0] == from_code and row[1] == to_code:
return True
# If the loop completes without finding a match, print False
return False
except FileNotFoundError:
return 1
except Exception:
return 1

View File

@ -0,0 +1,18 @@
import argostranslate.translate
import argostranslate.package
def install_language(from_code, to_code):
try:
argostranslate.package.update_package_index()
available_packages = argostranslate.package.get_available_packages()
available_package = list(
filter(
lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
)
)[0]
download_path = available_package.download()
argostranslate.package.install_from_path(download_path)
return 0
except Exception:
return 1

View File

@ -0,0 +1,8 @@
import argostranslate.package
import argostranslate.translate
def translate(message, from_lang, to_lang):
translation = argostranslate.translate.translate(message, from_lang, to_lang)
return translation