Skip to content

Commit

Permalink
Merge pull request #2 from NoEdgeAI/markdown
Browse files Browse the repository at this point in the history
Fix many bug on windows
  • Loading branch information
Menghuan1918 authored Dec 22, 2024
2 parents ebd1f66 + ca05673 commit ddb8abf
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 103 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/python.app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Build Application

on:
workflow_dispatch:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest, macos-13]

steps:
- uses: actions/checkout@v4

- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: "3.12"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pyinstaller
- name: Build with PyInstaller
run: pyinstaller -w --onefile -i icon.png app.py

- name: Copy additional files
run: |
cp reference.docx dist/
cp example.env dist/
cp reference.docx dist/
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: Trans-PolyDocs-${{ matrix.os }}
path: dist/
16 changes: 8 additions & 8 deletions MD_Translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def Process_MD(
)
output_md_path = os.path.join(
output_path,
os.path.basename(md_file).split(".")[0]
".".join(os.path.basename(md_file).split(".")[:-1])
+ "_translated_"
+ time.strftime("%Y%m%d_%H%M%S")
+ ".md",
Expand All @@ -25,18 +25,18 @@ def Process_MD(
f.write(output_md)

print(f"Translated markdown saved to {output_path}")
reference_docx = os.path.abspath("reference.docx")
output_docx_path = output_md_path.replace(".md", ".docx")
print("Translating markdown to docx...")
print("Trying ranslating markdown to docx...")
try:
output_docx_path = output_md_path.replace(".md", ".docx")
reference_docx = os.path.abspath("reference.docx")
extra_args = [f"--resource-path={output_path}"]
if os.path.exists(reference_docx):
extra_args.append(f"--reference-doc={reference_docx}")
pypandoc.convert_file(
output_md_path,
"docx",
outputfile=output_docx_path,
extra_args=[
f"--resource-path={output_path}",
f"--reference-doc={reference_docx}",
],
extra_args=extra_args,
)
print(f"Translated docx saved to {output_docx_path}")
except Exception as e:
Expand Down
28 changes: 28 additions & 0 deletions Main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ def get_translator(choice=None):

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")
Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Trans-PolyDocs

🚧 目前项目还处于完善阶段,暂不可用 🚧

🚧 Project under development, not ready for use yet 🚧

[English](README_EN.md) | 中文
Expand All @@ -19,7 +20,9 @@
> [!IMPORTANT]
> 如您希望将翻译后的文档以Word形式输出,请安装`pandoc`后运行程序。
>
> Windows:在Powershell中输入`winget install --source winget --exact --id JohnMacFarlane.Pandoc`[下载安装包安装](https://pandoc.org/installing.html)
> Windows:
>
>[下载安装包安装](https://pandoc.org/installing.html)或在Powershell中输入`winget install --source winget --exact --id JohnMacFarlane.Pandoc`
>
> MacOS:终端中运行`brew install pandoc`
>
Expand Down Expand Up @@ -47,8 +50,10 @@ cp example.env .env

## 打包

使用pyinstaller进行打包。使用`pip install pyinstaller`进行安装。
使用pyinstaller进行打包。使用`pip install pyinstaller`进行安装。运行以下指令:

```bash
pyinstaller --onefile --add-data "reference.docx:." --collect-all pypandoc_binary app.py
```
pyinstaller -w --onefile -i icon.png app.py
```

并复制项目中的`reference.docx`以及`example.env`到打包出的二进制文件同一目录中即可。
84 changes: 47 additions & 37 deletions Translates/DeepSeek.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,43 +70,53 @@ def deepseek_translate(
raise ValueError("input_prompt must contain {{text}} placeholder")

def translate(text: str, prev_text: str, next_text: str) -> str:
try:
client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": input_prompt.replace("{{prev_text}}", prev_text)
.replace("{{dest}}", dest)
.replace("{{text}}", text)
.replace("{{next_text}}", next_text),
},
],
temperature=tempterature,
stream=False,
)
result = response.choices[0].message.content
retry_count = 0
max_retries = 2

while retry_count <= max_retries:
try:
client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": input_prompt.replace("{{prev_text}}", prev_text)
.replace("{{dest}}", dest)
.replace("{{text}}", text)
.replace("{{next_text}}", next_text),
},
],
temperature=tempterature,
stream=False,
)
result = response.choices[0].message.content

if extra_type == "json":
try:
return json.loads(result)["translated"]
except Exception as e:
print(f"Having trouble extracting JSON: {e}")
return result
elif extra_type == "markdown":
try:
return result.split("```")[1]
except Exception as e:
print(f"Having trouble extracting markdown: {e}")
return result
return result
except Exception as e:
print(f"Error: {e}")
return text
if extra_type == "json":
try:
return json.loads(result)["translated"]
except Exception as e:
print(f"Having trouble extracting JSON: {e}")
return result
elif extra_type == "markdown":
try:
return result[result.find("```") + 3:result.rfind("```")]
except Exception as e:
print(f"Having trouble extracting markdown: {e}")
return result
return result

except Exception as e:
retry_count += 1
if retry_count <= max_retries:
print(f"Error occurred: {e}. Retrying... (Attempt {retry_count} of {max_retries})")
continue
else:
print(f"Error after {max_retries} retries: {e}")
return text

return translate
2 changes: 1 addition & 1 deletion Translates/Ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def translate(text: str, prev_text: str, next_text: str) -> str:
return result
elif extra_type == "markdown":
try:
return result.split("```")[1]
return result[result.find("```") + 3:result.rfind("```")]
except Exception as e:
print(f"Having trouble extracting markdown: {e}")
return result
Expand Down
81 changes: 44 additions & 37 deletions Translates/OpenAI.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,43 +71,50 @@ def openai_translate(
raise ValueError("input_prompt must contain {{text}} placeholder")

def translate(text: str, prev_text: str, next_text: str) -> str:
try:
client = OpenAI(api_key=api_key, base_url=base_url)
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": input_prompt.replace("{{prev_text}}", prev_text)
.replace("{{dest}}", dest)
.replace("{{text}}", text)
.replace("{{next_text}}", next_text),
},
],
temperature=tempterature,
stream=False,
)
result = response.choices[0].message.content
retries = 2
while retries >= 0:
try:
client = OpenAI(api_key=api_key, base_url=base_url)
response = client.chat.completions.create(
model=model,
messages=[
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": input_prompt.replace("{{prev_text}}", prev_text)
.replace("{{dest}}", dest)
.replace("{{text}}", text)
.replace("{{next_text}}", next_text),
},
],
temperature=tempterature,
stream=False,
)
result = response.choices[0].message.content

if extra_type == "json":
try:
return json.loads(result)["translated"]
except Exception as e:
print(f"Having trouble extracting JSON: {e}")
return result
elif extra_type == "markdown":
try:
return result.split("```")[1]
except Exception as e:
print(f"Having trouble extracting markdown: {e}")
return result
return result
except Exception as e:
print(f"Error: {e}")
return text
if extra_type == "json":
try:
return json.loads(result)["translated"]
except Exception as e:
print(f"Having trouble extracting JSON: {e}")
return result
elif extra_type == "markdown":
try:
return result[result.find("```") + 3:result.rfind("```")]
except Exception as e:
print(f"Having trouble extracting markdown: {e}")
return result
return result
except Exception as e:
if retries > 0:
print(f"Error occurred: {e}. Retrying... ({retries} attempts left)")
retries -= 1
continue
else:
print(f"Error after all retries: {e}")
return text

return translate
Loading

0 comments on commit ddb8abf

Please sign in to comment.