Repository navigation
Expand file tree
/
Copy pathrun_fast.py
More file actions
139 lines (112 loc) · 4.73 KB
/
Copy pathrun_fast.py
File metadata and controls
139 lines (112 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
from pathlib import Path
import time
from ruamel.yaml import YAML
from deep_translator import GoogleTranslator
from concurrent.futures import ThreadPoolExecutor
yaml = YAML()
yaml.preserve_quotes = True
langs = {
"zh_Hant": "zh-TW",
"ja_JP": "ja",
"vi_VN": "vi",
"th_TH": "th",
"es_ES": "es"
}
def translate_text(text, target_lang):
if not text.strip(): return text
gt = GoogleTranslator(source='auto', target=target_lang)
lines = text.split('\n')
translated_lines = []
in_code_block = False
buffer = []
def flush_buffer():
if not buffer: return
t = '\n'.join(buffer)
for _ in range(3):
try:
res = gt.translate(t)
if res:
res = res.replace('] (', '](').replace('** ', '**').replace(' **', '**').replace('` ', '`').replace(' `', '`')
translated_lines.append(res)
break
except Exception as e:
time.sleep(1)
else:
translated_lines.append(t)
buffer.clear()
for line in lines:
if line.strip().startswith('```'):
flush_buffer()
in_code_block = not in_code_block
translated_lines.append(line)
continue
if in_code_block:
translated_lines.append(line)
continue
if line.strip().startswith('http') or (not any(c.isalpha() for c in line) and len(line) < 10):
flush_buffer()
translated_lines.append(line)
continue
buffer.append(line)
if len(buffer) >= 10:
flush_buffer()
flush_buffer()
return '\n'.join(translated_lines)
def bump_version(ver):
parts = ver.split('.')
parts[-1] = str(int(parts[-1]) + 1)
return '.'.join(parts)
plugins = [
"AgenticRAG", "AIImagePlugin", "AutoTranslate", "DifyDatasetsConnector",
"EssentialCommands", "FastGPTConnector", "GeneralParsers", "GoogleSearch",
"GroupChatSummary", "HelloPlugin", "KeywordAlert", "LangRAG", "LongTermMemory",
"QWeather", "RAGFlowConnector", "ScheNotify", "SysStatPlugin", "TavilySearch",
"URLSummary", "WebSearch"
]
repo_root = Path(__file__).resolve().parent
plugins = [str(path.parent) for category in ("Runner", "KnowledgeEngine", "misc")
for path in sorted((repo_root / category).glob("*/manifest.yaml"))
if path.parent.name in plugins]
def translate_file(plugin, l_key, l_code, readme_text):
out_file = os.path.join(plugin, "readme", f"README_{l_key}.md")
if os.path.exists(out_file):
return
print(f"Translating {plugin} README to {l_key}...", flush=True)
translated = translate_text(readme_text, l_code)
with open(out_file, 'w', encoding='utf-8') as fout:
fout.write(translated)
print(f"Done {plugin} README to {l_key}!", flush=True)
with ThreadPoolExecutor(max_workers=10) as executor:
for plugin in plugins:
manifest_path = os.path.join(plugin, "manifest.yaml")
if not os.path.exists(manifest_path):
continue
with open(manifest_path, 'r', encoding='utf-8') as f:
data = yaml.load(f)
en_desc = data['metadata'].get('description', {}).get('en_US', '')
en_label = data['metadata'].get('label', {}).get('en_US', '')
old_v = str(data['metadata'].get('version', '0.1.0'))
data['metadata']['version'] = bump_version(old_v)
for l_key, l_code in langs.items():
if l_key not in data['metadata'].get('description', {}):
if 'description' not in data['metadata']:
data['metadata']['description'] = {}
if en_desc:
data['metadata']['description'][l_key] = translate_text(en_desc, l_code)
if l_key not in data['metadata'].get('label', {}):
if 'label' not in data['metadata']:
data['metadata']['label'] = {}
if en_label:
data['metadata']['label'][l_key] = en_label
with open(manifest_path, 'w', encoding='utf-8') as f:
yaml.dump(data, f)
readme_base = os.path.join(plugin, "README.md")
readme_dir = os.path.join(plugin, "readme")
os.makedirs(readme_dir, exist_ok=True)
if os.path.exists(readme_base):
with open(readme_base, 'r', encoding='utf-8') as f:
readme_text = f.read()
for l_key, l_code in langs.items():
executor.submit(translate_file, plugin, l_key, l_code, readme_text)
print("All done!", flush=True)