-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
165 lines (151 loc) · 5.62 KB
/
main.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from apscheduler.schedulers.background import BackgroundScheduler
from pymongo import UpdateOne
from typing import List
from loguru import logger
import datetime
from utils.translate import translate_chunk, Translation, Platform, Mode
from utils.database import init_engine, database
from utils.config import CornConfig, TranslateConfig
translate_config = TranslateConfig.load()
CHUNK_SIZE = translate_config.chunk_size
def translate_mutil_texts(
objs: List[Translation],
target_language="Simplified Chinese",
chunk_size=CHUNK_SIZE,
mode: Mode = Mode.MAIN,
):
failed_objs: List[Translation] = []
result = []
total_tokens = 0
chunks = [
objs[start : start + chunk_size] for start in range(0, len(objs), chunk_size)
]
logger.debug(f"Split {len(objs)} texts into {len(chunks)} chunks")
for chunk in chunks:
translated_objs, tokens_used = translate_chunk(chunk, target_language, mode)
total_tokens += tokens_used
if translated_objs:
result.extend(translated_objs)
update_database(translated_objs)
logger.debug(
f"Translated {len(translated_objs)} texts, used tokens: {tokens_used}, total tokens used: {total_tokens}"
)
else:
logger.debug(f"Failed to translate chunk, used tokens: {tokens_used}")
logger.debug(f"Switching to backup model")
failed_objs.extend(chunk)
if failed_objs:
failed_objs = []
chunks = [
failed_objs[start : start + chunk_size]
for start in range(0, len(failed_objs), chunk_size)
]
logger.debug(f"Split {len(failed_objs)} texts into {len(chunks)} chunks")
for chunk in chunks:
translated_objs, tokens_used = translate_chunk(
chunk, target_language, Mode.DOWNGRADE
)
total_tokens += tokens_used
if translated_objs:
result.extend(translated_objs)
update_database(translated_objs)
logger.debug(
f"Translated {len(translated_objs)} texts with backup model, used tokens: {tokens_used}, total tokens used: {total_tokens}"
)
else:
failed_objs.extend(chunk)
logger.debug(
f"Failed to translate chunk with backup model, used tokens: {tokens_used}"
)
if failed_objs:
logger.error(f"Failed to translate {len(failed_objs)} texts")
logger.debug([f"{obj.model_dump_json()}\n" for obj in failed_objs])
logger.debug(f"Total tokens used: {total_tokens}")
return result
def query_database() -> List[Translation]:
mr_result = database.get_collection("modrinth_projects").find(
{"description": {"$ne": None}, "translated_description": None}
)
cf_result = database.get_collection("curseforge_mods").find(
{"summary": {"$ne": None}, "translated_summary": None}
)
result = [
Translation(
platform=Platform.MODRINTH,
id=project["_id"],
original_text=project["description"],
)
for project in mr_result
] + [
Translation(
platform=Platform.CURSEFORGE,
id=project["_id"],
original_text=project["summary"],
)
for project in cf_result
]
logger.debug(f"Found {len(result)} items to translate.")
return result
def update_database(translations: List[Translation]):
mr_collection = database.get_collection("modrinth_projects")
cf_collection = database.get_collection("curseforge_mods")
mr_result: List[Translation] = []
cf_result: List[Translation] = []
for translation in translations:
if translation.platform == Platform.MODRINTH:
mr_result.append(translation)
else:
cf_result.append(translation)
mr_operations = [
UpdateOne(
{"_id": translation.id},
{"$set": {"translated_description": translation.translated_text}},
)
for translation in mr_result
]
cf_operations = [
UpdateOne(
{"_id": translation.id},
{"$set": {"translated_summary": translation.translated_text}},
)
for translation in cf_result
]
if mr_operations:
mr_collection.bulk_write(mr_operations)
if cf_operations:
cf_collection.bulk_write(cf_operations)
logger.info(
f"Updated {len(mr_result)} modrinth projects and {len(cf_result)} curseforge mods."
)
def check_translations():
translations = query_database()
if translations:
translations = translate_mutil_texts(translations)
# update_database(translations)
logger.info(f"Translated {len(translations)} objs.")
logger.info(f"下次执行时间: {translate_job.next_run_time}")
if __name__ == "__main__":
corn_config = CornConfig.load()
scheduler = BackgroundScheduler()
translate_job = scheduler.add_job(
check_translations,
"cron",
day=corn_config.day,
hour=corn_config.hour,
minute=corn_config.minute,
second=corn_config.second,
timezone="Asia/Shanghai",
# 10s 后开始执行首次任务
next_run_time=datetime.datetime.now() + datetime.timedelta(seconds=10)
)
# 启动调度器
scheduler.start()
logger.info("Scheduler started, waiting for 10s.")
logger.info(f"下次执行时间: {translate_job.next_run_time}")
# 保持主线程运行
try:
while True:
pass
except (KeyboardInterrupt, SystemExit):
logger.info("Shutting down...")
scheduler.shutdown()