YangHao520 commited on
Commit
373ffd5
·
1 Parent(s): 69dd6c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -36
app.py CHANGED
@@ -11,6 +11,7 @@ import datetime
11
  from DataFormat import DataFormat
12
  from DataFormat import GetTokenforStr
13
  import uploadData
 
14
 
15
  def ChatV2(params):
16
 
@@ -20,7 +21,9 @@ def ChatV2(params):
20
  # model="gpt-4",
21
  model=gptVersion,
22
  messages=[{"role": "system", "content": systemPrompt}, {"role": "user", "content": ques}],
23
- temperature=temperature)
 
 
24
 
25
  return systemPrompt, ques, completion['choices'][0]['message']['content']
26
  def ChatV2_estimate(params):
@@ -29,42 +32,62 @@ def ChatV2_estimate(params):
29
  maxNum=4
30
  for i in range(maxNum):
31
  try:
 
 
 
 
 
32
  completion = openai.ChatCompletion.create(
33
  # model="gpt-3.5-turbo",
34
- # model="gpt-4",
35
  model=gptVersion,
36
- messages=[{"role": "system", "content": systemPrompt}, {"role": "user", "content":prompt+ ques}],
37
- temperature=temperature)
38
-
 
 
39
  FineTunecompletion = openai.ChatCompletion.create(
40
  # model="gpt-3.5-turbo",
41
  # model="gpt-4",
42
  model=inetuneGptVersion,
43
- messages=[{"role": "system", "content": systemPrompt}, {"role": "user", "content": prompt+ques}],
44
- temperature=temperature)
 
 
45
  except Exception as e:
46
  print(e)
47
  print('第{}次错误,错误文本:{}'.format(i+1,ques))
48
  time.sleep(6*(i+1)) #如果请求失败则过10s重新请求
49
  if i==maxNum-1:
50
- return ''
51
-
 
 
52
  extData=[]
53
  for Checkbox in Checkboxgourd:
54
  if Checkbox == 'GPT生成结果翻译成中文':
55
- systemTran='你是一个对中文和日语非常了解的语言大师'
56
- prompt_tran='在不改变原意的情况下,请将给你的文本翻译成中文,下面是你要翻译的文本:'
57
- transCn = openai.ChatCompletion.create(
58
- # model="gpt-3.5-turbo",
59
- # model="gpt-4",
60
- model=gptVersion,
61
- messages=[{"role": "system", "content": systemTran}, {"role": "user", "content": prompt_tran+ques}],
62
- temperature=temperature)
63
- transCnText=transCn['choices'][0]['message']['content']
64
- extData.append(transCnText)
65
 
66
- if Checkbox == 'GPT生成结果翻译成中文':
67
  systemTran='你是一个对中文和日语非常了解的语言大师'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  prompt_tran='在不改变原意的情况下,请将给你的文本翻译成中文,下面是你要翻译的文本:'
69
  transCn = openai.ChatCompletion.create(
70
  # model="gpt-3.5-turbo",
@@ -122,13 +145,15 @@ def AI_Produst(systemText,quesList,gptVersion,temperature,num,outputPath,progres
122
  return outputPath
123
 
124
  def AI_Produst_estimate(systemText,prompt,quesList,gptVersion,inetuneGptVersion,temperature,num,outputPath,Checkbox,progress):
 
 
125
  progress(0, desc="Starting...")
126
  wb=openpyxl.Workbook()
127
  ws=wb.active
128
- ws.append(["System",'User','GPT_Output','是否合格','FineTune GPT_Output','是否合格'])
129
  maxNum=min(num,len(quesList))
130
  print('最大数字'+str(maxNum))
131
- with concurrent.futures.ThreadPoolExecutor(max_workers=6) as excutor:
132
  futures=[]
133
 
134
  for i in range(maxNum):
@@ -138,19 +163,31 @@ def AI_Produst_estimate(systemText,prompt,quesList,gptVersion,inetuneGptVersion,
138
  futures.append(task)
139
  prad=tqdm(total=len(futures))
140
  for futrue in concurrent.futures.as_completed(futures):
141
- prad.update(1)
 
142
  try:
143
- systemPrompt,ques,GPTAnswer,FineTuneGPTAnswer,exdata=futrue.result()
144
- except Exception as e:
145
- print('如果本次请求异常则自动退出')
146
- ws.append([systemPrompt,ques,'',e,''])
147
- #print(systemPrompt)
148
- print(ques)
149
- if len(exdata)==2:
150
- #加入翻译
151
- ws.append([systemPrompt,ques,GPTAnswer,'',FineTuneGPTAnswer])
152
- else:
153
- ws.append([systemPrompt,ques,GPTAnswer,'',FineTuneGPTAnswer])
 
 
 
 
 
 
 
 
 
 
 
154
 
155
  prad.close()
156
 
@@ -188,6 +225,7 @@ def AIProdust_batch(systemText,prompt,inputFile,textInput_APIKEY,temperature,gpt
188
  AI_Produst(systemText,quesList,gptVersion,temperature,num,outputPath,progress)
189
  return outputPath
190
  def AIProdust_batch_estimate(systemText,prompt,inputFile,textInput_APIKEY,temperature,gptVersion,fintuneGPTVersion,num,Checkbox,progress=gr.Progress(track_tqdm=True)):
 
191
  openai.api_key=textInput_APIKEY
192
  inputFile=inputFile.name
193
  nowTime=str(datetime.datetime.now()).split('.')[0].replace(' ','_').replace(':','_')
@@ -223,9 +261,14 @@ def Lines2Excel(lines):
223
  wb.save(outputPath)
224
 
225
  return outputPath
 
 
 
 
226
  def AIProdust():
227
  global tmpdir
228
 
 
229
  GPTVersion = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k',
230
  'gpt-3.5-turbo-16k-0613']
231
 
@@ -328,14 +371,24 @@ def AIProdust():
328
  fintuneGPTVersion=gr.Textbox(label='FineTune_GPTVersion', lines=2, placeholder='...')
329
  fintuneslider = gr.components.Slider(0, 1, label="Temperature", step=None, value=0.7)
330
  fintunenum = gr.Number(label='请求的次数', value=5)
331
- Checkbox=gr.CheckboxGroup(["GPT生成结果翻译成中文", "文本语法解析"], label="GPT额外功能", info="为了提高审核速度你要增加什么?")
332
  fintuneoutPutFile = gr.components.File(label="下载文件")
333
  fintunebutton_ques = gr.Button("开始请求")
334
  fintunebutton_ques.click(AIProdust_batch_estimate,
335
  inputs=[fintunetextInput_Sys, fintunetextInput_Prompt, fintuneinput_ExcelFile, textInput_APIKEY, fintuneslider,
336
  fintunedrop,fintuneGPTVersion, fintunenum,Checkbox], outputs=fintuneoutPutFile)
 
 
337
 
338
- demo.queue().launch()
 
 
 
 
 
 
 
 
339
  if __name__=="__main__":
340
  # ChatDemo()
341
  # AIProdustDemo() #AIGC 批量生成内容并加在Excel文件
 
11
  from DataFormat import DataFormat
12
  from DataFormat import GetTokenforStr
13
  import uploadData
14
+ import json
15
 
16
  def ChatV2(params):
17
 
 
21
  # model="gpt-4",
22
  model=gptVersion,
23
  messages=[{"role": "system", "content": systemPrompt}, {"role": "user", "content": ques}],
24
+ temperature=temperature,
25
+ timeout=30
26
+ )
27
 
28
  return systemPrompt, ques, completion['choices'][0]['message']['content']
29
  def ChatV2_estimate(params):
 
32
  maxNum=4
33
  for i in range(maxNum):
34
  try:
35
+ if '{Q1}' in prompt:
36
+ gptques=prompt.replace('{Q1}',ques)
37
+ else:
38
+ gptques=prompt+ques
39
+ print(gptques)
40
  completion = openai.ChatCompletion.create(
41
  # model="gpt-3.5-turbo",
 
42
  model=gptVersion,
43
+ messages=[{"role": "system", "content": systemPrompt}, {"role": "user", "content":gptques}],
44
+ temperature=temperature,
45
+ timeout=30
46
+ )
47
+ print([{"role": "system", "content": systemPrompt}, {"role": "user", "content":gptques}])
48
  FineTunecompletion = openai.ChatCompletion.create(
49
  # model="gpt-3.5-turbo",
50
  # model="gpt-4",
51
  model=inetuneGptVersion,
52
+ messages=[{"role": "system", "content": systemPrompt}, {"role": "user", "content": gptques}],
53
+ temperature=temperature,
54
+ timeout=30
55
+ )
56
  except Exception as e:
57
  print(e)
58
  print('第{}次错误,错误文本:{}'.format(i+1,ques))
59
  time.sleep(6*(i+1)) #如果请求失败则过10s重新请求
60
  if i==maxNum-1:
61
+ return systemPrompt, ques,'',str(e),''
62
+ time.sleep(1)
63
+ gptText=completion['choices'][0]['message']['content']
64
+ gptText2=FineTunecompletion['choices'][0]['message']['content']
65
  extData=[]
66
  for Checkbox in Checkboxgourd:
67
  if Checkbox == 'GPT生成结果翻译成中文':
 
 
 
 
 
 
 
 
 
 
68
 
 
69
  systemTran='你是一个对中文和日语非常了解的语言大师'
70
+ prompt_tran='请将给你的一组文本全部翻译成连贯流畅的中文,一组文本主要有三部分待翻译:text1:文本1 text2:文本2 text3:文本3。然后你需要将其翻译并按照json格式输出:{"tran_text1":"将用户输入的文本翻译成中文的文本","tran_text2":"将gpt改写文本翻译成中文的文本","tran_text3":"将gpt改写的文本翻译成中文的文本"},下面是你要翻译的文本:"""{Q1}"""'
71
+ transques="text1:{} text2:{} text3:{}".format(ques,gptText,gptText2)
72
+ for i in range(2):
73
+ try:
74
+ transCn = openai.ChatCompletion.create(
75
+ # model="gpt-3.5-turbo",
76
+ # model="gpt-4",
77
+ model=gptVersion,
78
+ messages=[{"role": "system", "content": systemTran}, {"role": "user", "content": prompt_tran.replace('{Q1}',transques)}],
79
+ temperature=temperature,
80
+ timeout=30
81
+ )
82
+ transCnText=json.loads(transCn['choices'][0]['message']['content'])
83
+ extData.append(transCnText)
84
+ break
85
+ except Exception as e:
86
+ print('error:'+str(e))
87
+ pass
88
+
89
+ if Checkbox == 'test2':
90
+ systemTran='你是一个对中文和日语非常了解的翻译官'
91
  prompt_tran='在不改变原意的情况下,请将给你的文本翻译成中文,下面是你要翻译的文本:'
92
  transCn = openai.ChatCompletion.create(
93
  # model="gpt-3.5-turbo",
 
145
  return outputPath
146
 
147
  def AI_Produst_estimate(systemText,prompt,quesList,gptVersion,inetuneGptVersion,temperature,num,outputPath,Checkbox,progress):
148
+ global stopFlag
149
+ stopFlag=False
150
  progress(0, desc="Starting...")
151
  wb=openpyxl.Workbook()
152
  ws=wb.active
153
+ ws.append(["System",'User','GPT_Output','是否合格','FineTune GPT_Output','是否合格','Tran_User','Tran_GPT_Output','Tran_FineTune GPT_Output'])
154
  maxNum=min(num,len(quesList))
155
  print('最大数字'+str(maxNum))
156
+ with concurrent.futures.ThreadPoolExecutor(max_workers=4) as excutor:
157
  futures=[]
158
 
159
  for i in range(maxNum):
 
163
  futures.append(task)
164
  prad=tqdm(total=len(futures))
165
  for futrue in concurrent.futures.as_completed(futures):
166
+ if stopFlag:
167
+ break
168
  try:
169
+ prad.update(1)
170
+ try:
171
+ systemPrompt,ques,GPTAnswer,FineTuneGPTAnswer,exdata=futrue.result(timeout=30)
172
+ except Exception as e:
173
+ print('如果本次请求异常则自动退出')
174
+ ws.append([systemPrompt,ques,'',str(e),''])
175
+ continue
176
+ #print(systemPrompt)
177
+ print(ques)
178
+ if len(exdata)==1:
179
+ try:
180
+ translate=exdata[0]
181
+ #加入翻译
182
+ ws.append([systemPrompt,ques,GPTAnswer,'',FineTuneGPTAnswer,'',translate['tran_text1'],translate['tran_text2'],translate['tran_text3']])
183
+ except Exception as e:
184
+ ws.append([systemPrompt, ques, GPTAnswer, '', FineTuneGPTAnswer])
185
+ print('error:'+str(e))
186
+ print(exdata[0])
187
+ else:
188
+ ws.append([systemPrompt,ques,GPTAnswer,'',FineTuneGPTAnswer])
189
+ except:
190
+ time.sleep(10)
191
 
192
  prad.close()
193
 
 
225
  AI_Produst(systemText,quesList,gptVersion,temperature,num,outputPath,progress)
226
  return outputPath
227
  def AIProdust_batch_estimate(systemText,prompt,inputFile,textInput_APIKEY,temperature,gptVersion,fintuneGPTVersion,num,Checkbox,progress=gr.Progress(track_tqdm=True)):
228
+
229
  openai.api_key=textInput_APIKEY
230
  inputFile=inputFile.name
231
  nowTime=str(datetime.datetime.now()).split('.')[0].replace(' ','_').replace(':','_')
 
261
  wb.save(outputPath)
262
 
263
  return outputPath
264
+ def stopprodust():
265
+ global stopFlag
266
+ stopFlag=True
267
+ return stopFlag
268
  def AIProdust():
269
  global tmpdir
270
 
271
+
272
  GPTVersion = ['gpt-4', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k',
273
  'gpt-3.5-turbo-16k-0613']
274
 
 
371
  fintuneGPTVersion=gr.Textbox(label='FineTune_GPTVersion', lines=2, placeholder='...')
372
  fintuneslider = gr.components.Slider(0, 1, label="Temperature", step=None, value=0.7)
373
  fintunenum = gr.Number(label='请求的次数', value=5)
374
+ Checkbox=gr.CheckboxGroup(["GPT生成结果翻译成中文", "文本语法解析(暂不支持)"], label="GPT额外功能", info="为了提高审核速度你要增加什么?")
375
  fintuneoutPutFile = gr.components.File(label="下载文件")
376
  fintunebutton_ques = gr.Button("开始请求")
377
  fintunebutton_ques.click(AIProdust_batch_estimate,
378
  inputs=[fintunetextInput_Sys, fintunetextInput_Prompt, fintuneinput_ExcelFile, textInput_APIKEY, fintuneslider,
379
  fintunedrop,fintuneGPTVersion, fintunenum,Checkbox], outputs=fintuneoutPutFile)
380
+ fintunebutton_quesStop = gr.Button("终止运行")
381
+ fintunebutton_quesStop.click(stopprodust,outputs=gr.Textbox(label='状态'))
382
 
383
+ gr.Markdown("<br><br>")
384
+ gr.Markdown("### APIKey创建的微调任务状态查询'")
385
+ input_APIKey = gr.Textbox(label="Openai_APIKEY", lines=2, placeholder='...')
386
+ button_createTask = gr.Button('微调任务查询')
387
+ output_TaskSatejson = gr.Json(label='查询微调任务状态')
388
+ button_createTask.click(uploadData.GetFineTuningJobState,
389
+ inputs=[input_APIKey],
390
+ outputs=output_TaskSatejson)
391
+ demo.queue().launch(share=True)
392
  if __name__=="__main__":
393
  # ChatDemo()
394
  # AIProdustDemo() #AIGC 批量生成内容并加在Excel文件