Tool A simple translator using a pre-trained AI model and PyQt.

wsnlndr

Newbie
May 20, 2023
77
404
Here I have been playing around with ChatGPT to get information about how to use a model to translate at home, without having to resort to online translators.

What I have discovered, and it is not surprising, is that to use an AI model and make it work quickly, you need to have powerful hardware resources.

In this case, the tool is an interface to translate text from English to Spanish.
You can always use an appropriate model to translate into another source and destination language or even use a large model that contains many possibilities in a single model.

The ideal is to use Cuda cores of the GPU to speed up the process since the CPU is not usually the most optimal for this task, but if the graphics card you have is not powerful, the process will not be fast to translate a simple line of text , but if you have more than one graphics card the process can be significantly accelerated with the appropriate code adjustments.

You can get the models for free at
The interface is as simple as possible using PyQt6 on GNU/Linux.
It would be ideal to automate the translation process to apply it to batch files.

Source code:
You don't have permission to view the spoiler content. Log in or register now.


Captura desde 2024-06-19 07-45-18.png
 

wsnlndr

Newbie
May 20, 2023
77
404
To use a translator like this for Renpy or other type projects, it may be interesting to implement the use of rules to avoid unpleasant surprises in the processing of the text that is translated and also improve the interface by giving more configuration options.

Example of filter lines of text by rules:
Python:
def apply_rules(words, rules):
    total_words = 0
    total_characters = 0

    # Count the total number of words and characters before applying the rules
    for rule in rules:
        if rule == "Count Words":
            total_words = len(" ".join(words).split())
        if rule == "Count Characters":
            total_characters = len("".join(words))

    if total_words > 0:
        print(f'Total number of words: {total_words}')
    
    if total_characters > 0:
        print(f'Total number of characters: {total_characters}')

    processed_words = []

    for word in words:
        for rule in rules:
            if rule.startswith('Remove Spaces'):
                word = word.replace(' ', '')
            elif rule.startswith('Convert to Lowercase'):
                word = word.lower()
            elif rule.startswith('Convert to Uppercase'):
                word = word.upper()
            elif rule.startswith('Remove Punctuation'):
                if '(' in rule and ')' in rule:
                    parameter = rule[rule.index('(') + 1:rule.index(')')]
                    word = remove_punctuation(word, parameter)
            elif rule.startswith('Remove Final Period'):
                word = word.rstrip('.')
            elif rule.startswith('Remove Empty Lines'):
                if not word.strip():
                    word = ''
            elif rule.startswith('Remove Numbers'):
                word = ''.join(filter(lambda x: not x.isdigit(), word))
            elif rule.startswith('Replace Characters'):
                if '(' in rule and ')' in rule:
                    parameters = rule[rule.index('(') + 1:rule.index(')')].split(',')
                    if len(parameters) == 2:
                        word = word.replace(parameters[0].strip(), parameters[1].strip())
            elif rule.startswith('Remove Short Words'):
                if '(' in rule and ')' in rule:
                    length = int(rule[rule.index('(') + 1:rule.index(')')])
                    if len(word) < length:
                        word = ''
            elif rule.startswith('Remove Long Words'):
                if '(' in rule and ')' in rule:
                    length = int(rule[rule.index('(') + 1:rule.index(')')])
                    if len(word) > length:
                        word = ''
            elif rule.startswith('Remove Specific Words'):
                if '(' in rule and ')' in rule:
                    specific_words = rule[rule.index('(') + 1:rule.index(')')].split(',')
                    if word in specific_words:
                        word = ''
            elif rule.startswith('Keep Only Letters'):
                word = ''.join(filter(lambda x: x.isalpha(), word))
            elif rule.startswith('Reverse Text'):
                word = word[::-1]
            elif rule.startswith('Sort Alphabetically'):
                word = ''.join(sorted(word))
            elif rule.startswith('Add Prefix'):
                if '(' in rule and ')' in rule:
                    prefix = rule[rule.index('(') + 1:rule.index(')')]
                    word = f'{prefix}{word}'
            elif rule.startswith('Add Suffix'):
                if '(' in rule and ')' in rule:
                    suffix = rule[rule.index('(') + 1:rule.index(')')]
                    word = f'{word}{suffix}'
            elif rule.startswith('Remove Words with Special Characters'):
                word = ''.join(filter(lambda x: x.isalnum(), word))
            elif rule.startswith('Capitalize Words'):
                word = word.capitalize()

        if word:
            processed_words.append(word)
 
Last edited: