当前位置: 首页 > news >正文

AI办公自动化:用通义千问批量翻译长篇英语TXT文档

在deepseek中输入提示词:

你是一个Python编程专家,现在要完成一个编写基于qwen-turbo模型API和dashscope库的程序脚本,具体步骤如下:

打开文件夹:F:\AI自媒体内容\待翻译;

获取里面所有TXT文档;

读取TXT文档内容;

将每个TXT文档的内容作为输入,并在调用API时附加提示语“翻译成中文”,API Key为:XXX,model为qwen-turbo;

接收API返回的结果,并将其保存到在同一文件夹中,文件标题名为原txt文件标题名加上“翻译”,文档格式为txt文档;

注意:

每一步都要打印相关的信息;

根据API的限流和请求要求,合理安排任务的发送频率,避免触发API的速率限制;

要有错误处理和调试信息,这有助于找出问题所在;

请求的输入长度范围应当在[1, 6000]之间,如果超长,需要对TXT内容分拆成多份,使单个输入内容不超过6000个字符,然后再一个个发送至API,接收API返回的结果,将之前分拆的内容按顺序整合在一起。;

在读取文件时跳过那些以"翻译.txt"结尾的文件,避免递归地处理同一个文件夹下的所有文件,包括已经生成的翻译文件;

在文件的开始处添加以下导入语句:from http import HTTPStatus;

在将某个部分的结果拼接到翻译结果列表时,可能会出现预期的字符串类型与实际的类型不匹配,确保在拼接翻译结果列表时,列表中的每个元素都是字符串类型;

API 返回是JSON 对象,其中包含了文本内容,需要对 JSON 对象进行解析,以提取出 text 字段中的文本内容。

qwen-turbo模型API的使用方法,请参照下面这个例子:

from http import HTTPStatus

import dashscope

def sample_sync_call():

prompt_text = '用萝卜、土豆、茄子做饭,给我个菜谱。'

resp = dashscope.Generation.call(

model='qwen-turbo',

prompt=prompt_text

)

# The response status_code is HTTPStatus.OK indicate success,

# otherwise indicate request is failed, you can get error code

# and message from code and message.

if resp.status_code == HTTPStatus.OK:

print(resp.output) # The output text

print(resp.usage) # The usage information

else:

print(resp.code) # The error code.

print(resp.message) # The error message.

sample_sync_call()

Deepseek生成的源代码:

from http import HTTPStatus

import os

import dashscope

import json

# 设置API Key

dashscope.api_key = 'XXX'

def translate_text(text):

prompt_text = f"翻译成中文: {text}"

resp = dashscope.Generation.call(

model='qwen-turbo',

prompt=prompt_text

)

if resp.status_code == HTTPStatus.OK:

return resp.output['text']

else:

print(f"Error: {resp.code} - {resp.message}")

return None

def process_file(file_path):

with open(file_path, 'r', encoding='utf-8') as file:

content = file.read()

if len(content) > 6000:

# Split content into chunks

chunks = [content[i:i+6000] for i in range(0, len(content), 6000)]

translated_chunks = []

for chunk in chunks:

translated_chunk = translate_text(chunk)

if translated_chunk:

translated_chunks.append(translated_chunk)

else:

print(f"Failed to translate chunk from file: {file_path}")

return

translated_content = ''.join(translated_chunks)

else:

translated_content = translate_text(content)

if translated_content:

translated_file_path = file_path.rsplit('.', 1)[0] + '翻译.txt'

with open(translated_file_path, 'w', encoding='utf-8') as translated_file:

translated_file.write(translated_content)

print(f"Translated file saved: {translated_file_path}")

else:

print(f"Failed to translate file: {file_path}")

def main():

folder_path = 'F:\\AI自媒体内容\\待翻译'

for filename in os.listdir(folder_path):

if filename.endswith('.txt') and not filename.endswith('翻译.txt'):

file_path = os.path.join(folder_path, filename)

print(f"Processing file: {file_path}")

process_file(file_path)

if __name__ == "__main__":

main()

http://www.lryc.cn/news/376578.html

相关文章:

  • 一键解压,无限可能——BetterZip,您的Mac必备神器!
  • 【数学】什么是最大似然估计?如何求解最大似然估计
  • 跟张良均老师学大数据人工智能|企业项目试岗实训开营
  • Pentest Muse:一款专为网络安全人员设计的AI助手
  • 10 SpringBoot 静态资源访问
  • Unity 之通过自定义协议从浏览器启动本地应用程序
  • Python抓取天气信息
  • 【超越拟合:深度学习中的过拟合与欠拟合应对策略】
  • 【Orange Pi 5与Linux内核编程】-理解Linux内核中的container_of宏
  • 003.Linux SSH协议工具
  • web前端组织分析:深入剖析其结构、功能与未来趋势
  • GitCode热门开源项目推荐:Spider网络爬虫框架
  • 实现一个二叉树的前序遍历、中序遍历和后序遍历方法。
  • 串扰(二)
  • 零基础入门学用Arduino 第四部分(三)
  • Mp3文件结构全解析(一)
  • ES 8.14 Java 代码调用,增加knnSearch 和 混合检索 mixSearch
  • 被腰斩的颍川郡守赵广汉
  • 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] 目录管理器(200分) - 三语言AC题解(Python/Java/Cpp)
  • 关于自学\跳槽\转行做网络安全行业的一些建议
  • 计算机网络(1) OSI七层模型与TCP/IP四层模型
  • 认识QML
  • llama-factory微调chatglm3
  • 大文件上传实现
  • 为何Proteus用户争相拥抱SmartEDA?揭秘背后的强大吸引力!
  • 万界星空科技QMS质量管理介绍
  • 神经网络 torch.nn---nn.LSTM()
  • Web前端JSP软件:深度解析与探索之旅
  • 人生的乐趣,在于对真知的追求
  • IPython大揭秘:神奇技巧让你掌握无敌编程力量!