Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,48 @@
|
|
1 |
# -*- encoding: utf-8 -*-
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
"
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
|
|
|
|
28 |
messages = [
|
|
|
|
|
|
|
|
|
29 |
]
|
30 |
for part in history:
|
31 |
messages.extend(
|
@@ -45,35 +62,61 @@ def respond(message, history, character_name, character_info, user_name, user_in
|
|
45 |
'content':message
|
46 |
})
|
47 |
print(messages)
|
48 |
-
ret=llm_infer(
|
49 |
history.append([
|
50 |
message,
|
51 |
ret
|
52 |
])
|
53 |
#if len(history) % 2 == 0:
|
54 |
return "", history
|
55 |
-
CSS ="""
|
56 |
-
.contain { display: flex; flex-direction: column; }
|
57 |
-
#component-0 { height: 100%; }
|
58 |
-
#chatbot { flex-grow: 1; }
|
59 |
-
"""
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
chatbot.
|
|
|
66 |
with gr.Column():
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
user_info=gr.TextArea(label='用户信息', lines=1, max_lines=3, value='明明是雷雷的哥哥,是个初中生')
|
71 |
|
72 |
|
73 |
with gr.Row():
|
74 |
chat_input = gr.Textbox(label="input")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
chat_input.submit(respond, [chat_input,chatbot,
|
|
|
77 |
clear = gr.ClearButton([chat_input, chatbot],)
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
1 |
# -*- encoding: utf-8 -*-
|
2 |
import gradio as gr
|
3 |
+
import random
|
4 |
+
import time
|
5 |
+
from concurrent.futures import ThreadPoolExecutor
|
6 |
+
import asyncio
|
7 |
+
from functools import partial
|
8 |
+
from volcengine.maas import MaasService, MaasException, ChatRole
|
9 |
+
maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing', connection_timeout=24000, socket_timeout=24000)
|
10 |
+
maas.set_ak("AKLTY2ZjOWM0NmFlNTEwNDBhM2EyYTg4OTgzYTUyYTc2NjU")
|
11 |
|
12 |
+
maas.set_sk("T0Rjd01HUTFaVEF3Tm1FME5EWXhNRGhpTURreE1ETTROalkwTnpreU1UVQ==")
|
13 |
+
|
14 |
+
def llm_infer(endpoint_id, messages, agent_network_output=True):
|
15 |
+
req = {
|
16 |
+
"model":{
|
17 |
+
"endpoint_id":endpoint_id
|
18 |
+
},
|
19 |
+
"parameters": {
|
20 |
+
"max_prompt_tokens": 3800, # 最大prompt,自动截断前面的输入。0-不生效
|
21 |
+
"max_new_tokens": 0, # 输出文本的最大tokens限制。0-不生效
|
22 |
+
"min_new_tokens": 0, # 输出文本的最小tokens限制。0-不生效
|
23 |
+
"temperature":1, # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大。取值范围0~1
|
24 |
+
"top_p": 0.7, # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富。取值范围0~1
|
25 |
+
"top_k": 0, # 选择预测值最大的k个token进行采样,取值范围0-1000。0-不生效
|
26 |
+
},
|
27 |
+
"messages": messages,
|
28 |
+
}
|
29 |
+
while True:
|
30 |
+
try:
|
31 |
+
resp = maas.chat(req)
|
32 |
+
break
|
33 |
+
except MaasException as e:
|
34 |
+
|
35 |
+
print(e)
|
36 |
+
continue
|
37 |
|
38 |
+
return resp['choice']['message']['content']
|
39 |
+
|
40 |
+
def respond(message, history, ep: gr.TextArea, sp:gr.TextArea):
|
41 |
messages = [
|
42 |
+
{
|
43 |
+
'role':'system',
|
44 |
+
'content':sp
|
45 |
+
},
|
46 |
]
|
47 |
for part in history:
|
48 |
messages.extend(
|
|
|
62 |
'content':message
|
63 |
})
|
64 |
print(messages)
|
65 |
+
ret=llm_infer(ep.strip(), messages)
|
66 |
history.append([
|
67 |
message,
|
68 |
ret
|
69 |
])
|
70 |
#if len(history) % 2 == 0:
|
71 |
return "", history
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
|
74 |
+
with gr.Blocks(fill_height=True).queue(default_concurrency_limit=10) as demo:
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column():
|
77 |
+
chatbot = gr.Chatbot()
|
78 |
+
chatbot.height = 500
|
79 |
with gr.Column():
|
80 |
+
gr.Label("input sp/ep of bot") # 增加些描述
|
81 |
+
sp=gr.TextArea(label="你是...", lines=1, max_lines=5,value='')
|
82 |
+
ep=gr.TextArea(label='ep1', lines=1, max_lines=1, value='')
|
|
|
83 |
|
84 |
|
85 |
with gr.Row():
|
86 |
chat_input = gr.Textbox(label="input")
|
87 |
+
|
88 |
+
def handle_click(chatbot, ep, sp, evt:gr.SelectData):
|
89 |
+
messages = [
|
90 |
+
{
|
91 |
+
'role':'system',
|
92 |
+
'content':sp
|
93 |
+
},
|
94 |
+
]
|
95 |
+
index = evt.index[0]
|
96 |
+
print(index)
|
97 |
+
for part in chatbot[:index]:
|
98 |
+
messages.extend(
|
99 |
+
[
|
100 |
+
{
|
101 |
+
'role':'user',
|
102 |
+
'content':part[0]
|
103 |
+
},
|
104 |
+
{
|
105 |
+
'role':'assistant',
|
106 |
+
'content':part[1]
|
107 |
+
}
|
108 |
+
]
|
109 |
+
)
|
110 |
+
ret = llm_infer(ep, messages)
|
111 |
+
print(ret)
|
112 |
+
print(chatbot[:index] + [[chatbot[index][0], ret]] + chatbot[index + 1:])
|
113 |
+
return chatbot[:index] + [[chatbot[index][0], ret]] + chatbot[index + 1:]
|
114 |
|
115 |
+
chat_input.submit(respond, [chat_input,chatbot, ep, sp ],[chat_input, chatbot])
|
116 |
+
#chatbot.select(handle_click, [chatbot, ep, sp],[chatbot],show_progress='hidden')
|
117 |
clear = gr.ClearButton([chat_input, chatbot],)
|
118 |
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
demo.launch(share=True)
|