Files

1 line
4.5 KiB
JSON

{"repo": "aleksi6/hello-github", "files": {"deepseek.py": {"7bbc90417d08063c0011627bbaadd9ad785f0667": {"t": 1785746431.4293466, "n": 3567, "h": "2ae7653dbc43d4e1", "c": "import json\r\nimport logging\r\nimport time\r\nimport re\r\nfrom tqdm import tqdm # 导入tqdm用于显示进度条\r\n\r\nimport openai\r\n\r\n# 使用你提供的API key,务必确保其合法有效且妥善保管,防止泄露\r\napi_key = \"sk-70ec806889b24bb0b6ca57b66b701fdd\"\r\nopenai.api_key = api_key\r\nopenai.api_base = \"https://api.deepseek.com\"\r\n\r\n# 模拟的10个英文句子列表,实际应用中可替换为真实获取的句子数据\r\nenglish_sentences = [\r\n \"This is a sample sentence 1.\",\r\n \"That's another example sentence.\",\r\n \"I'm happy today.\",\r\n \"She likes music.\",\r\n \"They are watching TV.\",\r\n \"He studies hard.\",\r\n \"We have a good time.\",\r\n \"The book is interesting.\",\r\n \"My dog is cute.\",\r\n \"It's a beautiful day.\"\r\n]\r\n\r\nresults = []\r\nlogging.basicConfig(filename='translation_errors.log', level=logging.DEBUG) # 将日志级别调整为DEBUG,方便查看详细信息\r\n\r\n# 设置请求间隔时间,单位为秒,可以根据API限制适当调整这个值\r\ninterval = 2\r\n\r\n\r\ndef extract_french_translation(text):\r\n \"\"\"\r\n 简单尝试从文本中提取可能的法语翻译内容,通过查找符合法语句子结构特点的部分\r\n (这里只是简单示例,可根据实际情况优化判断规则)\r\n \"\"\"\r\n possible_french = []\r\n lines = text.splitlines()\r\n for line in lines:\r\n if line.strip() and line[0].isupper() and any(char.isalpha() for char in line) and any(french_word in line.lower() for french_word in [\"est\", \"et\", \"le\", \"la\", \"les\", \"de\", \"du\", \"à\"]):\r\n possible_french.append(line.strip())\r\n return possible_french[-1] if possible_french else \"\"\r\n\r\n\r\ndef extract_chinese_translation(text):\r\n \"\"\"\r\n 简单尝试从文本中提取可能的中文翻译内容,通过查找包含中文常见汉字的部分\r\n (这里只是简单示例,可根据实际情况优化判断规则)\r\n \"\"\"\r\n import re\r\n chinese_pattern = re.compile(r'[\\u4e00-\\u9fff]+')\r\n matches = chinese_pattern.findall(text)\r\n return \"\".join(matches) if matches else \"\"\r\n\r\n\r\n# 使用tqdm来包裹循环,显示进度条,total参数指定总任务数(即句子的数量)\r\nfor idx, sentence in tqdm(enumerate(english_sentences, start=1), total=len(english_sentences)):\r\n try:\r\n response = openai.ChatCompletion.create(\r\n model=\"deepseek-chat\",\r\n messages=[\r\n {\"role\": \"system\", \"content\": \"You are an expert translator assistant. Your task is to accurately translate the given English sentences into Chinese and French, providing accurate and fluent translations for each sentence.\"},\r\n {\"role\": \"user\", \"content\": sentence}\r\n ],\r\n stream=False\r\n )\r\n # 检查response中是否存在error信息来判断请求是否异常\r\n if 'error' in response:\r\n raise openai.error.APIError(f\"Request failed with error: {response['error']}\")\r\n translation_text = response['choices'][0]['message']['content']\r\n try:\r\n zh_translation = extract_chinese_translation(translation_text)\r\n fr_translation = extract_french_translation(translation_text)\r\n result_dict = {\r\n \"sentence_id\": idx,\r\n \"original\": sentence,\r\n \"translations\": {\r\n \"zh\": zh_translation,\r\n \"fr\": fr_translation\r\n }\r\n }\r\n results.append(result_dict)\r\n except:\r\n logging.error(f\"Unexpected translation data format for sentence {idx}: {translation_text}. Setting empty translations.\")\r\n result_dict = {\r\n \"sentence_id\": idx,\r\n \"original\": sentence,\r\n \"translations\": {\r\n \"zh\": \"\",\r\n \"fr\": \"\"\r\n }\r\n }\r\n results.append(result_dict)\r\n except (openai.error.OpenAIError, KeyError) as e:\r\n logging.error(f\"Error translating sentence {idx}: {e}\")\r\n time.sleep(interval)\r\n\r\nprint(json.dumps(results, ensure_ascii=False, indent=4))"}}}}