From 77ef8df43fe2455f703a8b95d8164c4b39975bf3 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Thu, 14 Mar 2024 22:20:08 +0100 Subject: [PATCH] improved folder structure and much faster code --- go.mod | 3 + scripts/main.go | 103 ------------------ scripts/python/create_response.py | 18 --- scripts/python/main.py | 31 ------ .../package_mangement/check_packages.py | 18 --- .../package_mangement/downlaod_languages.py | 32 ------ .../package_mangement/note_all_packages.py | 22 ---- scripts/python/translate.py | 8 -- 8 files changed, 3 insertions(+), 232 deletions(-) create mode 100644 go.mod delete mode 100644 scripts/main.go delete mode 100644 scripts/python/create_response.py delete mode 100644 scripts/python/main.py delete mode 100644 scripts/python/package_mangement/check_packages.py delete mode 100644 scripts/python/package_mangement/downlaod_languages.py delete mode 100644 scripts/python/package_mangement/note_all_packages.py delete mode 100644 scripts/python/translate.py diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b9fb246 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module GO_server_w_ARGOS_TRANSLATE + +go 1.22.1 diff --git a/scripts/main.go b/scripts/main.go deleted file mode 100644 index 9c761a2..0000000 --- a/scripts/main.go +++ /dev/null @@ -1,103 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "encoding/json" - "fmt" - "io" - "log" - "net/http" - "os" - "os/exec" -) - -type Message struct { - Message string `json:"message"` - From string `json:"from"` - To string `json:"to"` -} - -func getAllPackages() { - TurnOn := exec.Command("python", "scripts/python/package_mangement/note_all_packages.py") - // Create a buffer to capture the standard output. - var out bytes.Buffer - TurnOn.Stdout = &out - - // Execute the command. - err := TurnOn.Run() - if err != nil { - log.Fatal(err) - } - - // Retrieve and print the standard output from the command. - fmt.Println("Output:", out.String()) -} -func handleRequest(w http.ResponseWriter, r *http.Request) { - // Read the request body - body, err := io.ReadAll(r.Body) - if err != nil { - http.Error(w, "Error reading request body: "+err.Error(), http.StatusInternalServerError) - return - } - defer r.Body.Close() - - // Unmarshal the JSON data - var msg Message - err = json.Unmarshal(body, &msg) - if err != nil { - http.Error(w, "Error unmarshalling JSON: "+err.Error(), http.StatusBadRequest) - return - } - - // Check if From and To fields are not longer than 2 letters - if len(msg.From) > 2 || len(msg.To) > 2 { - http.Error(w, "From and To fields should not be longer than 2 letters.", http.StatusBadRequest) - return - } - - // Start the Python command in the background - cmd := exec.Command("python", "scripts/python/main.py", msg.Message, msg.From, msg.To) - output, err := cmd.StdoutPipe() - if err != nil { - http.Error(w, "Error starting Python command: "+err.Error(), http.StatusInternalServerError) - return - } - err = cmd.Start() - if err != nil { - http.Error(w, "Error starting Python command: "+err.Error(), http.StatusInternalServerError) - return - } - - // Read the output of the Python command and send it to the client - scanner := bufio.NewScanner(output) - for scanner.Scan() { - fmt.Fprintln(w, scanner.Text()) - } - - // Wait for the command to complete - err = cmd.Wait() - if err != nil { - http.Error(w, "Error waiting for Python command: "+err.Error(), http.StatusInternalServerError) - return - } -} - -func main() { - getAllPackages() - // Define the HTTP handler function - http.HandleFunc("/api", handleRequest) - - // Get the port number from the environment variable or use a default value - port := "11435" - if p := os.Getenv("PORT"); p != "" { - port = p - } - - // Start the HTTP server - fmt.Printf("Listening on :%s...\n", port) - err := http.ListenAndServe(":"+port, nil) - if err != nil { - panic(err) - } -} diff --git a/scripts/python/create_response.py b/scripts/python/create_response.py deleted file mode 100644 index de3b2ff..0000000 --- a/scripts/python/create_response.py +++ /dev/null @@ -1,18 +0,0 @@ -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) - - diff --git a/scripts/python/main.py b/scripts/python/main.py deleted file mode 100644 index 356226b..0000000 --- a/scripts/python/main.py +++ /dev/null @@ -1,31 +0,0 @@ -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 = f"No available package for translating from {from_lang} to {to_lang}" - 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() diff --git a/scripts/python/package_mangement/check_packages.py b/scripts/python/package_mangement/check_packages.py deleted file mode 100644 index 79557bb..0000000 --- a/scripts/python/package_mangement/check_packages.py +++ /dev/null @@ -1,18 +0,0 @@ -import csv - -def check_languages_installed(from_code, to_code): - try: - # Assuming the CSV file is named 'languages.csv' and located in the same directory - with open('data/installed_packages.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 - return False - - except FileNotFoundError: - return 1 - except Exception: - return 1 - \ No newline at end of file diff --git a/scripts/python/package_mangement/downlaod_languages.py b/scripts/python/package_mangement/downlaod_languages.py deleted file mode 100644 index 9400f9e..0000000 --- a/scripts/python/package_mangement/downlaod_languages.py +++ /dev/null @@ -1,32 +0,0 @@ -import argostranslate.translate -import argostranslate.package -from package_mangement.note_all_packages import write_installed_packages_to_csv - - -def install_language(from_code, to_code): - try: - argostranslate.package.update_package_index() - available_packages = argostranslate.package.get_available_packages() - # Convert the filter result into a list - filtered_packages = list( - filter( - lambda x: x.from_code == from_code and x.to_code == to_code, available_packages - ) - ) - # Check if the filtered list is empty - if not filtered_packages: - print(f"No available package for translating from {from_code} to {to_code}") - return 1 - - # If we have at least one package, proceed to download and install - available_package = filtered_packages[0] - download_path = available_package.download() - argostranslate.package.install_from_path(download_path) - - # After successful installation, append package details to the CSV file - write_installed_packages_to_csv() - - return 0 - except Exception as e: - print(f"An error occurred: {e}") - return e \ No newline at end of file diff --git a/scripts/python/package_mangement/note_all_packages.py b/scripts/python/package_mangement/note_all_packages.py deleted file mode 100644 index 5f6a22e..0000000 --- a/scripts/python/package_mangement/note_all_packages.py +++ /dev/null @@ -1,22 +0,0 @@ -import argostranslate.package -import csv - -# Function to write installed package details into a CSV file -def write_installed_packages_to_csv(filename='data/installed_packages.csv'): - # Fetch all installed packages - installed_packages = argostranslate.package.get_installed_packages() - # Open/Create a CSV file to write into - with open(filename, mode='w', newline='', encoding='utf-8') as file: - writer = csv.writer(file) - - # Write package details row by row - for package in installed_packages: - from_language = package.from_code - to_language = package.to_code - package_name = package.from_name - - writer.writerow([from_language, to_language, package_name]) - -if __name__ == "__main__": - write_installed_packages_to_csv() - \ No newline at end of file diff --git a/scripts/python/translate.py b/scripts/python/translate.py deleted file mode 100644 index 526234b..0000000 --- a/scripts/python/translate.py +++ /dev/null @@ -1,8 +0,0 @@ -import argostranslate.package -import argostranslate.translate - -def translate(message, from_lang, to_lang): - translation = argostranslate.translate.translate(message, from_lang, to_lang) - return translation - -