AI Learning – QianWen Model and Automatic Article Translation

I have tried several large language models before, including chatGLM2, Baichuan2, and Llama. Recently, I also saw a powerful Alibaba model called QianWen (Qwen). The experimental data on the QianWen website shows that its performance is generally better than other open-source models.

The translation comparison using chatGLM2 feels like the level of a junior high school student, while Qwen’s translation is closer to general writing standards. Of course, there is a difference in model size, so this comparison may not be fair. But it can still serve as a reference for everyone. The results can be viewed at: “chatGLM2 translation article” and “Qwen translation article“.

But just playing around with a large language model seems a bit cliche. In this article, we will use Qwen to automatically translate and publish WordPress articles, truly turning AI into productivity.

Basic environment installation

Some basic environmental settings, such as Anaconda and shared scripts, have already been written in the article “Common Operations” at this link. Please take a look first to ensure that all commands can be executed correctly.

Create a Conda environment

Due to the different dependencies of each project, an environment will be set up for each case here.

Download project and model

Download the quantized version model of Qwen-14B-Chat-Int4 directly here, with a file size of about 9GB and taking up around 13GB VRAM in actual operation.

Execute WEB Demo

Edit the file named “web_demo.py” and find the following line. Replace the original model path with “Qwen-14B-Chat-Int4”.

Then, run the following command.

After that, you can open http://IP:8000 in your browser to try out the web chat interface. By the way, I asked a question that has been quite popular recently… People are different, and one grain of rice can raise hundreds of people. With the same data, similar models will be generated.

WordPress article translation script

To call Qwen’s dialogue function, I modified the example script cli_demo.py. In fact, most key functions of large language models are quite simple, and many programs are focused on optimizing user interfaces. Here is my translation program.

The descriptions of functions:

  • has_chinese(): This method checks whether there is Chinese or not. If there is no Chinese, it means that there is nothing to do.
  • is_failed():  The function is used to determine whether the translation has failed. Due to the randomness of AI translation, such as answering irrelevant questions or deleting important parts, it is necessary to first judge and decide whether to adopt its output result. This function is closely related to the model you use. If different LLM models are adopted, the error type may be different.
  • The translate_retry() function processes the translation line by line and calls this function if it does not find some specified HTML tags. The “retry” means that after each translation, it uses the is_failed() function to check, if the failure occurs more than 20 times, it will output the original text.
  • translate_tag(): Translate the content of the specified tag for translation. Currently used for headings (h) and lists (ui).
  • “load_model_tokenizer()” is a function that loads a model tokenizer obtained from a CLI example.
  • translate(): Calls the model to perform translation.

The operation process can be briefly described as follows:

  • First load the model and then read in the file to be translated line by line.
  • If there is a “pre” tag found, output it directly because WordPress uses it to output the original format of the mark. To avoid translating the content in between, all the content in between should be outputted as is.
  • If there is a discovery of ul tag, output directly.
  • “If encountering a li tag, the content in the middle will be translated.”
  • for h3~h5,they are processed like li
  • for others, just translate.

Translation

A typical execution instruction is as follows:

Here, ai_translate_new.py refers to the above program, and article.txt represents the original content of the WordPress article. This content can be seen in the WordPress compiler and saved as article.txt.

Due to the fact that my WordPress is an older version and I have no intention of upgrading to a newer one, the tags that appear frequently in my articles are very simple. The newer versions of WordPress seem to have more user-friendly live editing tools, but these tools often add many tags, which can cause AI translation to be prone to errors. If you do not need to translate into English, using the new tool is fine, or you will need to handle exceptions for your translation program more.

If there is a failure during the translation process, the failed sentence will also be displayed.
The final content will be redirected to an “all.html” file for inspection.

The translation cannot be completed because the message contains HTML tags that cannot be translated. The original message and the translated message will be displayed. Since I set a rule in is_failed() that does not allow links to disappear, it was judged as a failure. After translation, you can check for any HTML abnormalities using a tool called tidy.

Through the number of errors in this tool, we can roughly understand whether there are major errors. If so, it will list the line numbers of the errors. At this time, we can optimize the automation program by modifying the rules of is_failed() in ai_translate_new.py, or we can simply manually correct them.

After the basic check with tidy, you can directly open this all.html file to see its content and whether there are any obvious errors.

If everything seems to be okay, you can manually paste this content into WordPress to create a new article.

WordPress automatically posts

Once we master how to automatically translate, we can then let WordPress automatically post articles. WordPress has a set of CLI tools that can be used to read article lists and content, but this is only applicable for self-hosted WordPress with a server. If you use some WordPress without CLI, it may not apply, but you can still do so by translating and then posting on the web.

WP CLI

The command-line installation method for WordPress is as follows, which is actually just downloading the file and placing it in the /bin directory.

 

WP CLI Remote

It is clear that the hosting of WordPress and the AI host must be on different machines, so we need to obtain data from WordPress remotely for translation. This aspect is also explained on the official WordPress website here, simply put, you need to create a ~/.wp-cli/config.yml file and fill in the name and account information of the host.

The @prod in front is the name, host, and location represented by this group of accounts. When issuing WP Cli commands, you can use @prod to represent it, and its connection method to the host uses SSH. Those who use GCP to deploy WordPress can refer to this article for information on how to achieve passwordless remote login.

Auto-generated post

Finally, the automatic translation post I implemented was achieved through a script that specifies the ID to be translated and completes the task. The script first retrieves the article content, then translates it, and posts it if there is no error. The script for translating is as follows:

This is actually calling ai_translate_new.py to do the translation and then calling tidy to do a check.

The script for creating the post is as follows:

This part mainly uses the post title, content, and publishing features of WP CLI to automate tasks. In each step, failure scenarios are considered to provide feedback to users.

Translation tips for automatic translation:

In order to make AI translation more prepared, I found that it is best to simplify the formatting used. For example, in WordPress, only a few tags such as pre, h3-h5, ui, and ol are used. Perhaps the current AI translation is not very perfect, but through some small tricks, it can still work for you.

Conclusion

Using open-source AI large language models can indeed save a lot of time and not rely on paid services, but the knowledge required is indeed considerable. I have always felt that users need to master certain CLI technology skills so that they can turn technology into their hands. If only through others’ APIs or GUIs, your balls are actually held in others’ hands. APIs can change at any time, and GUIs do not have automation capabilities, which means these abilities cannot be mastered by you.

Someone said “AI will not replace you, but people who know how to use AI will.” I hope everyone can master the skills of using these large models more flexibly.

 

Leave a Reply(Name請以user_開頭,否則會被判定會垃圾息)

請輸入答案 − 1 = 3