Skip to content

Commit 80d0b27

Browse files
HYLcoolyxdyc
andauthored
Update translator of OP doc building. (#554)
* * use translator from alibaba to translate the doc, which is faster and more stable * * update all beta to stable * Typo Fix * Typo Fix * * fix typo --------- Co-authored-by: Daoyuan Chen <67475544+yxdyc@users.noreply.github.com>
1 parent 0815c29 commit 80d0b27

File tree

4 files changed

+165
-170
lines changed

4 files changed

+165
-170
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ repos:
4141
language: python
4242
require_serial: true
4343
additional_dependencies:
44-
- googletrans==4.0.2
44+
- translators==5.9.3
4545

4646
exclude: |
4747
(?x)^(

.pre-commit-hooks/build_op_doc.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import ast
2-
import asyncio
32
import json
43
import os
54
import re
65
from typing import Any, List
76

8-
from googletrans import Translator
7+
import translators as ts
98

109
DOC_PATH = 'docs/Operators.md'
1110

@@ -152,7 +151,7 @@ def analyze_resource_tag(code):
152151
def analyze_model_tags(code):
153152
"""
154153
Analyze the model tag for the given code content string. SHOULD be one of
155-
the "Modal Tags" in `tagging_mappings.json`. It makes the choice by finding
154+
the "Model Tags" in `tagging_mappings.json`. It makes the choice by finding
156155
the `model_type` arg in `prepare_model` method invocation.
157156
"""
158157
pattern = r'model_type=[\'|\"](.*?)[\'|\"]'
@@ -431,20 +430,16 @@ def generate_op_table_section(op_type, op_record_list):
431430
return '\n\n'.join(doc)
432431

433432

434-
async def translate_text(text, dest='zh'):
435-
async with Translator() as translator:
436-
res = await translator.translate(text, src='en', dest=dest)
437-
return res
438-
439-
440433
def get_op_desc_in_en_zh_batched(descs):
441-
zhs = asyncio.run(translate_text(descs, dest='zh'))
442-
return [desc + ' ' + zh.text for desc, zh in zip(descs, zhs)]
443-
444-
445-
def get_op_desc_in_en_zh(desc):
446-
zh = asyncio.run(translate_text(desc, dest='zh')).text
447-
return desc + ' ' + zh
434+
separator = '\n'
435+
batch = separator.join(descs)
436+
res = ts.translate_text(batch,
437+
translator='alibaba',
438+
from_language='en',
439+
to_language='zh')
440+
zhs = res.split(separator)
441+
assert len(zhs) == len(descs)
442+
return [desc + ' ' + zh.strip() for desc, zh in zip(descs, zhs)]
448443

449444

450445
def parse_op_record_from_current_doc():

.pre-commit-hooks/tag_mappings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"desc": "stable version OP. Based on the beta version, OP optimizations related to DJ (e.g. model management, batched processing, OP fusion, ...) are added to this OP. 表示 stable 版本算子。基于 beta 版本,完善了DJ相关的算子优化项(如模型管理,批处理,算子融合等)。"
4646
}
4747
},
48-
"Modal Tags": {
48+
"Model Tags": {
4949
"api": {
5050
"icon": "🔗API",
5151
"desc": "equipped with API-based models. (e.g. ChatGPT, GPT-4o). 支持基于 API 调用模型(如 ChatGPT,GPT-4o)。"

0 commit comments

Comments
 (0)