-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.py
221 lines (187 loc) · 7.92 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from dotenv import load_dotenv
import os
from pdfdeal.file_tools import md_replace_imgs
from Translates.OpenAI import openai_translate
from Translates.Ollama import ollama_translate
from Translates.DeepSeek import deepseek_translate
from Translates.DeepLX import deeplx_translate
from Translates.DeepL import deepl_translate
from MD_Translate import Process_MD
from pdfdeal import Doc2X
ENV_PATH = ".env" if os.path.exists(".env") else "example.env"
load_dotenv(ENV_PATH)
# Get translator settings from environment variables
translate_use = os.getenv("TRANSLATE_USE")
threads = int(os.getenv("THREADS", 10))
# Get translator-specific settings
openai_apikey = os.getenv("openai_apikey")
openai_baseurl = os.getenv("openai_baseurl", "https://api.openai.com/v1")
openai_model = os.getenv("openai_model", "gpt-4o-mini")
ollama_baseurl = os.getenv("ollama_baseurl", "http://localhost:11434/v1")
ollama_model = os.getenv("ollama_model", "qwen2.5")
deepseek_api = os.getenv("deepseek_api")
deeplx_url = os.getenv("deeplx_url", "http://127.0.0.1:1188/translate")
deeplx_src = os.getenv("deeplx_src", "EN")
deeplx_dest = os.getenv("deeplx_dest", "ZH")
deepl_apikey = os.getenv("deepl_apikey")
deepl_dest = os.getenv("deepl_dest", "ZH")
# LLM common settings
temperature = float(os.getenv("temperature", 0.8))
system_prompt = os.getenv("system_prompt", "")
input_prompt = os.getenv("input", "")
extra_type = os.getenv("extra_type", "markdown")
llm_src = os.getenv("llm_src", "English")
llm_dest = os.getenv("llm_dest", "中文")
system_prompt = None if system_prompt == "" else system_prompt
input_prompt = None if input_prompt == "" else input_prompt
def get_translator(choice=None):
"""Get translator based on environment variable or user selection"""
if choice:
return create_translator(choice)
if translate_use:
return create_translator(translate_use)
print("Please select a translator:")
print("1. OpenAI")
print("2. Ollama")
print("3. DeepSeek")
print("4. DeepLX")
print("5. DeepL")
print("6. Google")
choice = input("Enter your choice (1-6): ")
translators = {
"1": "openai",
"2": "ollama",
"3": "deepseek",
"4": "deeplx",
"5": "deepl",
"6": "google",
}
if choice not in translators:
print("Invalid choice, using deepseek as default")
return create_translator("deepseek")
return create_translator(translators[choice])
def create_translator(name):
"""Create translator instance based on name"""
# Get translator-specific settings from environment variables
openai_apikey = os.getenv("openai_apikey")
openai_baseurl = os.getenv("openai_baseurl", "https://api.openai.com/v1")
openai_model = os.getenv("openai_model", "gpt-4o-mini")
ollama_baseurl = os.getenv("ollama_baseurl", "http://localhost:11434/v1")
ollama_model = os.getenv("ollama_model", "qwen2.5")
deepseek_api = os.getenv("deepseek_api")
deeplx_url = os.getenv("deeplx_url", "http://127.0.0.1:1188/translate")
deeplx_src = os.getenv("deeplx_src", "EN")
deeplx_dest = os.getenv("deeplx_dest", "ZH")
deepl_apikey = os.getenv("deepl_apikey")
deepl_dest = os.getenv("deepl_dest", "ZH")
# LLM common settings
temperature = float(os.getenv("temperature", 0.8))
system_prompt = os.getenv("system_prompt", "")
input_prompt = os.getenv("input", "")
extra_type = os.getenv("extra_type", "markdown")
llm_src = os.getenv("llm_src", "English")
llm_dest = os.getenv("llm_dest", "中文")
system_prompt = None if system_prompt == "" else system_prompt
input_prompt = None if input_prompt == "" else input_prompt
if name == "openai":
if not openai_apikey or openai_apikey == "sk-1234567":
print("Error: OpenAI API key not set")
raise Exception("OpenAI API key not set")
return openai_translate(
api_key=openai_apikey,
base_url=openai_baseurl,
src=llm_src,
dest=llm_dest,
model=openai_model,
tempterature=temperature,
system_prompt=system_prompt if system_prompt else None,
input_prompt=input_prompt if input_prompt else None,
extra_type=extra_type,
)
elif name == "ollama":
return ollama_translate(
base_url=ollama_baseurl,
src=llm_src,
dest=llm_dest,
model=ollama_model,
tempterature=temperature,
system_prompt=system_prompt if system_prompt else None,
input_prompt=input_prompt if input_prompt else None,
extra_type=extra_type,
)
elif name == "deepseek":
if not deepseek_api or deepseek_api == "sk-1234567":
print("Error: DeepSeek API key not set")
raise Exception("DeepSeek API key not set")
return deepseek_translate(
api_key=deepseek_api,
src=llm_src,
dest=llm_dest,
tempterature=temperature,
system_prompt=system_prompt if system_prompt else None,
input_prompt=input_prompt if input_prompt else None,
extra_type=extra_type,
)
elif name == "deeplx":
return deeplx_translate(base_url=deeplx_url, src=deeplx_src, dest=deeplx_dest)
elif name == "deepl":
if not deepl_apikey or deepl_apikey == "":
print("Error: DeepL API key not set")
raise Exception("DeepL API key not set")
return deepl_translate(api_key=deepl_apikey, dest=deepl_dest)
elif name == "google":
from Translates.Google import google_translate
return google_translate(
src=os.getenv("google_src", "en"), dest=os.getenv("google_dest", "zh-cn")
)
else:
print(f"Unknown translator: {name}")
raise Exception(f"Unknown translator: {name}")
def main():
# Get translator
translator = get_translator()
if os.getenv("SKIP_TEST", "false").lower() != "true":
print("Testing translator...")
test = translator("Hello, how are you?", "", "")
if test == "Hello, how are you?":
print("Translator test failed, please check your settings")
raise Exception("Translator test failed")
print(f"Translator test successful: {test}")
# Get file path from user
file_path = input("Please enter the path to your markdown or PDF file: ")
if not os.path.exists(file_path):
print(f"Error: File {file_path} does not exist")
raise FileNotFoundError(f"File {file_path} does not exist")
if not file_path.endswith(".md") and not file_path.endswith(".pdf"):
print("Error: File must be a markdown file (.md) or a PDF file (.pdf)")
raise ValueError("File must be a markdown file (.md) or a PDF file (.pdf)")
if file_path.endswith(".pdf"):
print("Converting PDF to markdown using Doc2X...")
if (
os.getenv("DOC2X_APIKEY") == "sk-1234567"
or os.getenv("DOC2X_APIKEY") == ""
or os.getenv("DOC2X_APIKEY") is None
):
print("Error: Please set your DOC2X_APIKEY")
raise Exception("Please set your DOC2X_APIKEY")
client = Doc2X(debug=True)
md_text, _, flag = client.pdf2file(
pdf_file=file_path,
output_format="text",
)
if flag:
print("Error: PDF to markdown conversion failed")
raise Exception("PDF to markdown conversion failed")
output_md_path = os.path.join(
"Output", ".".join(os.path.basename(file_path).split(".")[:-1]) + ".md"
)
os.makedirs("Output", exist_ok=True)
with open(output_md_path, "w") as f:
f.write(md_text[0])
file_path = output_md_path
print("Starting to download images(If have)...")
md_replace_imgs(mdfile=file_path, replace="local", threads=10)
# Process the file
Process_MD(md_file=file_path, translate=translator, thread=threads)
if __name__ == "__main__":
main()