-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
96 lines (79 loc) · 3.73 KB
/
test_api.py
File metadata and controls
96 lines (79 loc) · 3.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
# Quick API Test Script
# Run: python3 test_api.py
import json, urllib.request, sys
API_URL = "https://api.deepseek.com/v1/chat/completions"
# ⚠️ 请替换为你的真实 API Key
API_KEY = "YOUR_DEEPSEEK_API_KEY_HERE"
LANG_MAP = {
"中文": "Chinese", "英文": "English", "日文": "Japanese",
"韩文": "Korean", "法文": "French", "德文": "German",
"西班牙文": "Spanish", "俄文": "Russian", "葡萄牙文": "Portuguese",
"阿拉伯文": "Arabic"
}
def test_translate(source_lang_cn, target_lang_cn, text):
print(f"\n{'='*60}")
print(f"测试: {source_lang_cn} → {target_lang_cn}")
print(f"原文: {text}")
target_en = LANG_MAP.get(target_lang_cn, target_lang_cn)
source_en = LANG_MAP.get(source_lang_cn, source_lang_cn)
source_desc = f"from {source_en} ({source_lang_cn})"
system_prompt = f"""You are a professional web page translator. Translate the following content {source_desc} into {target_en} ({target_lang_cn}).
Rules:
- Only translate text content. Do NOT modify code, URLs, HTML tags, CSS class names, or any markup.
- If a segment looks like code, return it AS-IS without translation.
- Preserve technical terms accurately.
- Translation should be natural and fluent in {target_en}.
- The input may contain multiple segments separated by: <!-- DEEPLSEEK_TRANSLATOR_SEGMENT_BOUNDARY -->
- You MUST keep this separator EXACTLY as-is between segments in your output.
- Translate each segment independently.
- Return ONLY the translated text. No explanations, notes, or extra words."""
payload = {
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": text}
],
"temperature": 0.3,
"max_tokens": 8192
}
req = urllib.request.Request(
API_URL,
data=json.dumps(payload).encode(),
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
},
method="POST"
)
try:
with urllib.request.urlopen(req, timeout=60) as resp:
data = json.loads(resp.read())
result = data["choices"][0]["message"]["content"]
print(f"\nAPI 返回:")
print(repr(result))
print()
sep = "<!-- DEEPLSEEK_TRANSLATOR_SEGMENT_BOUNDARY -->"
parts = result.split(sep)
print(f"分段数: {len(parts)}")
for i, p in enumerate(parts):
print(f" 段{i}: {p.strip()[:100]}")
return True
except urllib.error.HTTPError as e:
print(f"HTTP错误 {e.code}: {e.read().decode()[:200]}")
return False
except Exception as e:
print(f"错误: {e}")
return False
if __name__ == "__main__":
if API_KEY == "YOUR_DEEPSEEK_API_KEY_HERE":
print("❌ 请先设置你的 API Key!")
print("编辑此文件,将 API_KEY 替换为你的 DeepSeek API Key")
sys.exit(1)
# 测试 1: 日语→中文
test_translate("日文", "中文", "これは日本語のテスト文章です。<!-- DEEPLSEEK_TRANSLATOR_SEGMENT_BOUNDARY -->\n私は毎日コーヒーを飲みます。")
# 测试 2: 英文→中文(验证基本功能)
test_translate("英文", "中文", "This is a test. I drink coffee every day.<!-- DEEPLSEEK_TRANSLATOR_SEGMENT_BOUNDARY -->\nThe weather is nice today.")
# 测试 3: 法文→中文
test_translate("法文", "中文", "Bonjour le monde!<!-- DEEPLSEEK_TRANSLATOR_SEGMENT_BOUNDARY -->\nJe parle français.")
# 测试 4: 中文→日文
test_translate("中文", "日文", "你好世界!<!-- DEEPLSEEK_TRANSLATOR_SEGMENT_BOUNDARY -->\n今天天气很好。")