TranslateSpace / app.py
YangHao520's picture
Update app.py
dfa23c5
import gradio as gr
import os
import tempfile
import time
import openpyxl
import os
import openai
import os
import json
import gradio as gr
import tempfile
import shutil
def generate_file(file_obj):
global tmpdir
tranSystem = "你是一个高超的翻译官,你熟悉各国的语言习惯以及如何完美在各个语言间翻译,稳定可靠是你的座右铭,你的任务是将我们给你的文本,在理解文本意思之后,在不改变其原意的情况下,将其翻译成对应语言。"
prompt = """
翻译成连贯流畅的中文,模板:输入,多行文本,输出:[{"oriText":"第一行文本","tranText":"第一行文本的翻译"},{"oriText":"第二行文本","tranText":"第二行文本的翻译"}],下面是你要处理的文本:
"""
gptVersion = 'gpt-3.5-turbo'
temperature = 0.6
openai.api_key = openaiKey
print('临时文件夹地址:{}'.format(tmpdir))
print('上传文件的地址:{}'.format(file_obj.name)) # 输出上传后的文件在gradio中保存的绝对地址
#获取到上传后的文件的绝对路径后,其余的操作就和平常一致了
# 将文件复制到临时目录中
shutil.copy(file_obj.name, tmpdir)
# 获取上传Gradio的文件名称
FileName=os.path.basename(file_obj.name)
# 获取拷贝在临时目录的新的文件地址
NewfilePath=os.path.join(tmpdir,FileName)
print(NewfilePath)
book=openpyxl.load_workbook(NewfilePath)
sheet=book.active
max_row=sheet.max_row
max_col=sheet.max_column
translateSet=[]
minLines=10
headList=[]
for j in range(1,max_col+1):
if sheet.cell(1, j).value is not None and j==1:
headList.append(sheet.cell(1,j).value)
oriData=''
for i in range(2,max_row+1):
if i%minLines!=0:
if sheet.cell(i,j).value is not None:
oriData+=sheet.cell(i,j).value.replace('\n',' ')+'\n'
elif i%minLines==0 or i==max_row:
if sheet.cell(i,j).value is not None:
oriData+=sheet.cell(i,j).value.replace('\n',' ')+'\n'
params = tranSystem, prompt+oriData, gptVersion, temperature
systemPrompt, ques, answer = ChatV2(params)
print(answer)
jsondata = json.loads(answer)
print(jsondata)
translateSet.extend(jsondata)
print(translateSet)
print(len(translateSet))
print(headList)
transDist={}
for item in translateSet:
transDist[item['oriText']]=item['tranText']
# 在本地电脑打开一个新的文件,并且将上传文件内容写入到新文件
outputPath = os.path.join(tmpdir, "New" + FileName)
wb=openpyxl.Workbook
ws=wb.active
ws.append('')
# 打开复制到新路径后的文件
with open(NewfilePath, 'rb') as file_obj:
#在本地电脑打开一个新的文件,并且将上传文件内容写入到新文件
outputPath=os.path.join(tmpdir,"New"+FileName)
with open(outputPath,'wb') as w:
w.write(file_obj.read())
# 返回新文件的的地址(注意这里)
return outputPath
def app():
global tmpdir
with tempfile.TemporaryDirectory(dir='.') as tmpdir:
# 定义输入和输出
inputs = gr.components.File(label="上传文件")
outputs = gr.components.File(label="下载文件")
# 创建 Gradio 应用程序g
app = gr.Interface(fn=generate_file, inputs=inputs, outputs=outputs, title="文件上传、并生成可下载文件demo",
description="上传任何文件都可以,只要大小别超过你电脑的内存即可"
)
# 启动应用程序
app.launch(share=True)
def ChatV2(params):
systemPrompt,ques,gptVersion,temperature=params
completion = openai.ChatCompletion.create(
# model="gpt-3.5-turbo",
# model="gpt-4",
model=gptVersion,
messages=[{"role": "system", "content": systemPrompt}, {"role": "user", "content": ques}],
temperature=temperature)
return systemPrompt, ques, completion['choices'][0]['message']['content']
def main():
tranSystem="你是一个高超的翻译官,你熟悉各国的语言习惯以及如何完美在各个语言间翻译,稳定可靠是你的座右铭,你的任务是将我们给你的文本,在理解文本意思之后,在不改变其原意的情况下,将其翻译成对应语言。"
prompt="""
翻译成连贯流畅的中文,模板:输入,多行文本,输出:[{"oriText":"第一行文本","tranText":"第一行文本的翻译"},{"oriText":"第二行文本","tranText":"第二行文本的翻译"}],下面是你要处理的文本:
あなたに合わせる\n今から帰ります\n終わった
"""
gptVersion='gpt-3.5-turbo'
temperature=0.6
openai.api_key=openaiKey
params=tranSystem,prompt,gptVersion,temperature
systemPrompt, ques,answer=ChatV2(params)
print(answer)
jsondata=json.loads(answer)
print(jsondata)
if __name__=="__main__":
#main()
app()
"""根据你的需求
我现在要回家了
结束了
很晚了,抱歉!
什么时候结束?
有点困了!
被锁住了
什么时候回来?
"""