gradio_demo / app.py
peiji's picture
Update app.py
69e485b verified
raw
history blame contribute delete
No virus
3.92 kB
# -*- encoding: utf-8 -*-
import gradio as gr
import random
import time
from concurrent.futures import ThreadPoolExecutor
import asyncio
from functools import partial
from volcengine.maas import MaasService, MaasException, ChatRole
maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing', connection_timeout=24000, socket_timeout=24000)
maas.set_ak("AKLTY2ZjOWM0NmFlNTEwNDBhM2EyYTg4OTgzYTUyYTc2NjU")
maas.set_sk("T0Rjd01HUTFaVEF3Tm1FME5EWXhNRGhpTURreE1ETTROalkwTnpreU1UVQ==")
def llm_infer(endpoint_id, messages, agent_network_output=True):
req = {
"model":{
"endpoint_id":endpoint_id
},
"parameters": {
"max_prompt_tokens": 3800, # 最大prompt,自动截断前面的输入。0-不生效
"max_new_tokens": 0, # 输出文本的最大tokens限制。0-不生效
"min_new_tokens": 0, # 输出文本的最小tokens限制。0-不生效
"temperature":1, # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大。取值范围0~1
"top_p": 0.7, # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富。取值范围0~1
"top_k": 0, # 选择预测值最大的k个token进行采样,取值范围0-1000。0-不生效
},
"messages": messages,
}
while True:
try:
resp = maas.chat(req)
break
except MaasException as e:
print(e)
continue
return resp['choice']['message']['content']
def respond(message, history, ep: gr.TextArea, sp:gr.TextArea):
messages = [
{
'role':'system',
'content':sp
},
]
for part in history:
messages.extend(
[
{
'role':'user',
'content':part[0]
},
{
'role':'assistant',
'content':part[1]
}
]
)
messages.append({
'role':'user',
'content':message
})
print(messages)
ret=llm_infer(ep.strip(), messages)
history.append([
message,
ret
])
#if len(history) % 2 == 0:
return "", history
with gr.Blocks(fill_height=True).queue(default_concurrency_limit=10) as demo:
with gr.Row():
with gr.Column():
chatbot = gr.Chatbot()
chatbot.height = 500
with gr.Column():
gr.Label("input sp/ep of bot") # 增加些描述
sp=gr.TextArea(label="你是...", lines=1, max_lines=5,value='')
ep=gr.TextArea(label='ep1', lines=1, max_lines=1, value='')
with gr.Row():
chat_input = gr.Textbox(label="input")
def handle_click(chatbot, ep, sp, evt:gr.SelectData):
messages = [
{
'role':'system',
'content':sp
},
]
index = evt.index[0]
print(index)
for part in chatbot[:index]:
messages.extend(
[
{
'role':'user',
'content':part[0]
},
{
'role':'assistant',
'content':part[1]
}
]
)
ret = llm_infer(ep, messages)
print(ret)
print(chatbot[:index] + [[chatbot[index][0], ret]] + chatbot[index + 1:])
return chatbot[:index] + [[chatbot[index][0], ret]] + chatbot[index + 1:]
chat_input.submit(respond, [chat_input,chatbot, ep, sp ],[chat_input, chatbot])
#chatbot.select(handle_click, [chatbot, ep, sp],[chatbot],show_progress='hidden')
clear = gr.ClearButton([chat_input, chatbot],)
demo.launch(share=True)