Submit suggestions to a LibreTranslate server from Python
import ltsuggest

# Define the URL of the LibreTranslate suggest endpoint
url = "http://localhost:5000/"
# url = "https://libretranslate.com/"

# Define your input parameters
q = "Hello world!"  # Original text
s = "¡Hola mundo!"  # Suggested translation
source = "en"       # Language of original text
target = "es"       # Language of suggested translation

# Call the suggest_translation function
response = ltsuggest.suggest_translation(url, q, s, source, target)

print("Status code: " + str(response.status_code))
print(response.json())
demo.py
argosopentech/LTSuggest
# Written by ChatGPT-4

import requests

def suggest_translation(url, q, s, source, target):
    # Build the JSON payload
    payload = {
        "q": q,
        "s": s,
        "source": source,
        "target": target
    }

    # Add trailing slash to URL if not present
    if not url.endswith("/"):
        url += "/"    
    
    # Make the request
    response = requests.post(f"{url}suggest", json=payload)

    return response

ltsuggest.py
argosopentech/LTSuggest
import csv
import ltsuggest

# Define the URL of the LibreTranslate suggest endpoint
url = "http://localhost:5000"

# Define the CSV file path
csv_file = "suggestions.csv"

# Open the CSV file and read suggestions
with open(csv_file, newline='', encoding='utf-8') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        q = row['Original']  # Original text
        s = row['Suggested']  # Suggested translation
        source = row['Source']  # Language of original text
        target = row['Target']  # Language of suggested translation

        # Call the suggest_translation function
        response = ltsuggest.suggest_translation(url, q, s, source, target)

        print(f"Translation for '{q}' from {source} to {target}:")
        print("Status code: " + str(response.status_code))
        print(response.json())

uploadcsv.py
argosopentech/LTSuggest