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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- encoding: utf-8 -*-
|
| 2 |
+
from fastapi import Path
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import random
|
| 5 |
+
import time
|
| 6 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 7 |
+
import asyncio
|
| 8 |
+
from functools import partial
|
| 9 |
+
from copy import deepcopy
|
| 10 |
+
from volcengine.maas import MaasService, MaasException, ChatRole
|
| 11 |
+
import tempfile
|
| 12 |
+
maas = MaasService('ml-maas-api-test.bytedance.net', 'cn-beijing', connection_timeout=240, socket_timeout=240)
|
| 13 |
+
# maas.set_ak("AKLTYzVmYWE1Yzk4Mzg1NDUzODgzZDdkYThkYTkwN2FiYTc")
|
| 14 |
+
# maas.set_sk("TW1NNE1EZzJNREE1Tm1RME5HWmxNbUUyWXprM01USXdNV0k0TkdZeVlqaw==")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
lines=[]
|
| 18 |
+
cnt=0
|
| 19 |
+
affirm_lines=[]
|
| 20 |
+
def llm_infer(endpoint_id, messages, agent_network_output=True):
|
| 21 |
+
req = {
|
| 22 |
+
"model":{
|
| 23 |
+
"endpoint_id":endpoint_id
|
| 24 |
+
},
|
| 25 |
+
"parameters": {
|
| 26 |
+
"max_prompt_tokens": 4000, # 最大prompt,自动截断前面的输入。0-不生效
|
| 27 |
+
"max_new_tokens": 0, # 输出文本的最大tokens限制。0-不生效
|
| 28 |
+
"min_new_tokens": 0, # 输出文本的最小tokens限制。0-不生效
|
| 29 |
+
"temperature":1, # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大。取值范围0~1
|
| 30 |
+
"top_p": 0.7, # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富。取值范围0~1
|
| 31 |
+
"top_k": 0, # 选择预测值最大的k个token进行采样,取值范围0-1000。0-不生效
|
| 32 |
+
},
|
| 33 |
+
"messages": messages,
|
| 34 |
+
}
|
| 35 |
+
print(req)
|
| 36 |
+
try:
|
| 37 |
+
resp = maas.chat(req)
|
| 38 |
+
except MaasException as e:
|
| 39 |
+
print(e)
|
| 40 |
+
print(resp['req_id'])
|
| 41 |
+
return resp['choice']['message']['content']
|
| 42 |
+
|
| 43 |
+
def make_valid_messages(lines):
|
| 44 |
+
examples = []
|
| 45 |
+
for line in lines:
|
| 46 |
+
messages=eval(line)
|
| 47 |
+
|
| 48 |
+
if isinstance(messages, list):
|
| 49 |
+
messages={'messages':messages}
|
| 50 |
+
_messages = [messages['messages'][0]]
|
| 51 |
+
|
| 52 |
+
pre_role = 'system'
|
| 53 |
+
for part in messages['messages'][1:]:
|
| 54 |
+
cur_role=part['role']
|
| 55 |
+
if cur_role == 'assistant' and (pre_role == 'assistant' or pre_role=='system'):
|
| 56 |
+
_messages.append({
|
| 57 |
+
'role':'user',
|
| 58 |
+
'content':""
|
| 59 |
+
})
|
| 60 |
+
if cur_role == 'user' and pre_role == 'user':
|
| 61 |
+
_messages.append({
|
| 62 |
+
'role':'assistant',
|
| 63 |
+
'content':""
|
| 64 |
+
})
|
| 65 |
+
_messages.append(part)
|
| 66 |
+
pre_role=cur_role
|
| 67 |
+
if pre_role == 'user':
|
| 68 |
+
_messages.append({
|
| 69 |
+
'role':'assistant',
|
| 70 |
+
'content':""
|
| 71 |
+
})
|
| 72 |
+
examples.append(
|
| 73 |
+
{
|
| 74 |
+
"messages":deepcopy(_messages)
|
| 75 |
+
}
|
| 76 |
+
)
|
| 77 |
+
return examples
|
| 78 |
+
|
| 79 |
+
def handle_click(chatbot, ep, ak, sk, evt:gr.SelectData):
|
| 80 |
+
maas.set_ak(ak)
|
| 81 |
+
|
| 82 |
+
maas.set_sk(sk)
|
| 83 |
+
global lines, cnt
|
| 84 |
+
index, j = evt.index
|
| 85 |
+
if j == 0:
|
| 86 |
+
return chatbot
|
| 87 |
+
messages = [
|
| 88 |
+
{
|
| 89 |
+
'role':'system',
|
| 90 |
+
'content':lines[cnt]['messages'][0]['content']
|
| 91 |
+
},
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
print(index)
|
| 95 |
+
for part in chatbot[:index]:
|
| 96 |
+
messages.extend(
|
| 97 |
+
[
|
| 98 |
+
{
|
| 99 |
+
'role':'user',
|
| 100 |
+
'content':part[0]
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
'role':'assistant',
|
| 104 |
+
'content':part[1]
|
| 105 |
+
}
|
| 106 |
+
]
|
| 107 |
+
)
|
| 108 |
+
messages.append({
|
| 109 |
+
'role':'user',
|
| 110 |
+
'content':chatbot[index][0]
|
| 111 |
+
})
|
| 112 |
+
ret = llm_infer(ep, messages)
|
| 113 |
+
lines[cnt]['messages'][index + 1 + 1]['content'] = ret
|
| 114 |
+
return chatbot[:index] + [[chatbot[index][0], ret]] + chatbot[index + 1:]
|
| 115 |
+
|
| 116 |
+
def select_line(cnt):
|
| 117 |
+
global lines
|
| 118 |
+
history = []
|
| 119 |
+
sp=lines[cnt]['messages'][0]['content']
|
| 120 |
+
for part in lines[cnt]['messages'][1:]:
|
| 121 |
+
if part['role'] == 'user':
|
| 122 |
+
history.append([
|
| 123 |
+
part['content']
|
| 124 |
+
])
|
| 125 |
+
else:
|
| 126 |
+
history[-1].append(
|
| 127 |
+
part['content']
|
| 128 |
+
)
|
| 129 |
+
return sp, history
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def add_cnt():
|
| 133 |
+
global cnt, lines
|
| 134 |
+
cnt += 1 if cnt != len(lines) - 1 else 0
|
| 135 |
+
sp, history = select_line(cnt)
|
| 136 |
+
return cnt, sp, history
|
| 137 |
+
|
| 138 |
+
def sub_cnt():
|
| 139 |
+
global cnt, lines
|
| 140 |
+
cnt -= 1 if cnt != 0 else 0
|
| 141 |
+
sp, history = select_line(cnt)
|
| 142 |
+
return cnt, sp, history
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def handle_file(file: gr.File):
|
| 146 |
+
global lines, cnt
|
| 147 |
+
lines=open(file).readlines()
|
| 148 |
+
lines=make_valid_messages(lines)
|
| 149 |
+
cnt=0
|
| 150 |
+
sp, history=select_line(cnt)
|
| 151 |
+
return history, gr.TextArea(value=sp, max_lines=5, lines=3, interactive=True, visible=True),gr.Dropdown(choices=list(range(len(lines))), interactive=True, visible=True)
|
| 152 |
+
def start_eval():
|
| 153 |
+
pass
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
def drop_download_select_line(dropdown_value):
|
| 157 |
+
global cnt
|
| 158 |
+
cnt = dropdown_value
|
| 159 |
+
sp, history = select_line(cnt)
|
| 160 |
+
return sp, history
|
| 161 |
+
|
| 162 |
+
def affirm_sample():
|
| 163 |
+
global cnt,affirm_lines
|
| 164 |
+
affirm_lines.append(lines[cnt])
|
| 165 |
+
|
| 166 |
+
cnt += 1 if len(lines) - 1 != cnt else 0
|
| 167 |
+
sp, history = select_line(cnt)
|
| 168 |
+
|
| 169 |
+
return cnt, sp, history
|
| 170 |
+
|
| 171 |
+
def download_file():
|
| 172 |
+
# https://www.gradio.app/docs/gradio/downloadbutton#demos
|
| 173 |
+
global affirm_lines
|
| 174 |
+
import jsonlines
|
| 175 |
+
import pathlib
|
| 176 |
+
ff = tempfile.NamedTemporaryFile()
|
| 177 |
+
ff.name="output.jsonl"
|
| 178 |
+
with jsonlines.open(ff.name, 'w') as fp:
|
| 179 |
+
for line in affirm_lines:
|
| 180 |
+
fp.write(line)
|
| 181 |
+
|
| 182 |
+
return pathlib.Path(ff.name)
|
| 183 |
+
|
| 184 |
+
visible = gr.State(False)
|
| 185 |
+
sample_choices = gr.State([])
|
| 186 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 187 |
+
with gr.Row(equal_height=True):
|
| 188 |
+
with gr.Column(scale=2):
|
| 189 |
+
chatbot = gr.Chatbot()
|
| 190 |
+
chatbot.height=620
|
| 191 |
+
affirm_btn=gr.Button(value="确认")
|
| 192 |
+
|
| 193 |
+
with gr.Column():
|
| 194 |
+
file=gr.File() # 增加些描述
|
| 195 |
+
sp=gr.TextArea(visible=False)
|
| 196 |
+
ak=gr.TextArea(label='ak', lines=1, max_lines=1)
|
| 197 |
+
sk=gr.TextArea(label='sk', lines=1, max_lines=1)
|
| 198 |
+
ep=gr.TextArea(label='ep', lines=1, max_lines=1, value='ep-20240511021029-wlhrl')
|
| 199 |
+
sample_select=gr.Dropdown(choices=[], interactive=True, visible=False)
|
| 200 |
+
with gr.Row():
|
| 201 |
+
with gr.Column():
|
| 202 |
+
prev_btn = gr.Button(value='上一条')
|
| 203 |
+
with gr.Column():
|
| 204 |
+
next_btn = gr.Button(value='下一条')
|
| 205 |
+
eval_btn=gr.Button(value="开始生成")
|
| 206 |
+
download=gr.DownloadButton("Download")
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
file.upload(handle_file, [file], [chatbot, sp, sample_select])
|
| 210 |
+
chatbot.select(handle_click, [chatbot, ep, ak, sk],[chatbot, ],show_progress='hidden')
|
| 211 |
+
|
| 212 |
+
#affirm_btn=gr.Button(value="确认")
|
| 213 |
+
prev_btn.click(sub_cnt, outputs=[sample_select, sp, chatbot])
|
| 214 |
+
next_btn.click(add_cnt, outputs=[sample_select, sp, chatbot])
|
| 215 |
+
affirm_btn.click(affirm_sample, inputs=[], outputs=[sample_select, sp, chatbot])
|
| 216 |
+
download.click(download_file, inputs=[], outputs=[download])
|
| 217 |
+
sample_select.change(drop_download_select_line, [sample_select], [sp, chatbot])
|
| 218 |
+
demo.launch()
|