華強(qiáng)北 做網(wǎng)站海外推廣營銷平臺(tái)
在deepseek中輸入提示詞:
你是一個(gè)Python編程專家,現(xiàn)在要完成一個(gè)編寫基于qwen-turbo模型API和dashscope庫的程序腳本,具體步驟如下:
打開文件夾:F:\AI自媒體內(nèi)容\待翻譯;
獲取里面所有TXT文檔;
讀取TXT文檔內(nèi)容;
將每個(gè)TXT文檔的內(nèi)容作為輸入,并在調(diào)用API時(shí)附加提示語“翻譯成中文”,API Key為:XXX,model為qwen-turbo;
接收API返回的結(jié)果,并將其保存到在同一文件夾中,文件標(biāo)題名為原txt文件標(biāo)題名加上“翻譯”,文檔格式為txt文檔;
注意:
每一步都要打印相關(guān)的信息;
根據(jù)API的限流和請(qǐng)求要求,合理安排任務(wù)的發(fā)送頻率,避免觸發(fā)API的速率限制;
要有錯(cuò)誤處理和調(diào)試信息,這有助于找出問題所在;
請(qǐng)求的輸入長度范圍應(yīng)當(dāng)在[1, 6000]之間,如果超長,需要對(duì)TXT內(nèi)容分拆成多份,使單個(gè)輸入內(nèi)容不超過6000個(gè)字符,然后再一個(gè)個(gè)發(fā)送至API,接收API返回的結(jié)果,將之前分拆的內(nèi)容按順序整合在一起。;
在讀取文件時(shí)跳過那些以"翻譯.txt"結(jié)尾的文件,避免遞歸地處理同一個(gè)文件夾下的所有文件,包括已經(jīng)生成的翻譯文件;
在文件的開始處添加以下導(dǎo)入語句:from http import HTTPStatus;
在將某個(gè)部分的結(jié)果拼接到翻譯結(jié)果列表時(shí),可能會(huì)出現(xiàn)預(yù)期的字符串類型與實(shí)際的類型不匹配,確保在拼接翻譯結(jié)果列表時(shí),列表中的每個(gè)元素都是字符串類型;
API 返回是JSON 對(duì)象,其中包含了文本內(nèi)容,需要對(duì) JSON 對(duì)象進(jìn)行解析,以提取出 text 字段中的文本內(nèi)容。
qwen-turbo模型API的使用方法,請(qǐng)參照下面這個(gè)例子:
from http import HTTPStatus
import dashscope
def sample_sync_call():
prompt_text = '用蘿卜、土豆、茄子做飯,給我個(gè)菜譜。'
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
# 設(shè)置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自媒體內(nèi)容\\待翻譯'
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()