Spaces:
Sleeping
Sleeping
Upload demo.py
Browse files
demo.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=240, socket_timeout=240)
|
10 |
+
# maas.set_ak("AKLTYzVmYWE1Yzk4Mzg1NDUzODgzZDdkYThkYTkwN2FiYTc")
|
11 |
+
# maas.set_sk("TW1NNE1EZzJNREE1Tm1RME5HWmxNbUUyWXprM01USXdNV0k0TkdZeVlqaw==")
|
12 |
+
maas.set_ak("AKLTY2ZjOWM0NmFlNTEwNDBhM2EyYTg4OTgzYTUyYTc2NjU")
|
13 |
+
|
14 |
+
maas.set_sk("T0Rjd01HUTFaVEF3Tm1FME5EWXhNRGhpTURreE1ETTROalkwTnpreU1UVQ==")
|
15 |
+
|
16 |
+
def llm_infer(endpoint_id, messages, agent_network_output=True):
|
17 |
+
req = {
|
18 |
+
"model":{
|
19 |
+
"endpoint_id":endpoint_id
|
20 |
+
},
|
21 |
+
"parameters": {
|
22 |
+
"max_prompt_tokens": 4000, # 最大prompt,自动截断前面的输入。0-不生效
|
23 |
+
"max_new_tokens": 0, # 输出文本的最大tokens限制。0-不生效
|
24 |
+
"min_new_tokens": 0, # 输出文本的最小tokens限制。0-不生效
|
25 |
+
"temperature":1, # 用于控制生成文本的随机性和创造性,Temperature值越大随机性越大。取值范围0~1
|
26 |
+
"top_p": 0.7, # 用于控制输出tokens的多样性,TopP值越大输出的tokens类型越丰富。取值范围0~1
|
27 |
+
"top_k": 0, # 选择预测值最大的k个token进行采样,取值范围0-1000。0-不生效
|
28 |
+
},
|
29 |
+
"messages": messages,
|
30 |
+
}
|
31 |
+
try:
|
32 |
+
resp = maas.chat(req)
|
33 |
+
except MaasException as e:
|
34 |
+
print(e)
|
35 |
+
return resp['choice']['message']['content']
|
36 |
+
|
37 |
+
def respond(message, history, ep: gr.TextArea, sp:gr.TextArea):
|
38 |
+
messages = [
|
39 |
+
{
|
40 |
+
'role':'system',
|
41 |
+
'content':sp
|
42 |
+
},
|
43 |
+
]
|
44 |
+
for part in history:
|
45 |
+
messages.extend(
|
46 |
+
[
|
47 |
+
{
|
48 |
+
'role':'user',
|
49 |
+
'content':part[0]
|
50 |
+
},
|
51 |
+
{
|
52 |
+
'role':'assistant',
|
53 |
+
'content':part[1]
|
54 |
+
}
|
55 |
+
]
|
56 |
+
)
|
57 |
+
messages.append({
|
58 |
+
'role':'user',
|
59 |
+
'content':message
|
60 |
+
})
|
61 |
+
print(messages)
|
62 |
+
ret=llm_infer(ep.strip(), messages)
|
63 |
+
history.append([
|
64 |
+
message,
|
65 |
+
ret
|
66 |
+
])
|
67 |
+
#if len(history) % 2 == 0:
|
68 |
+
return "", history
|
69 |
+
|
70 |
+
|
71 |
+
with gr.Blocks(fill_height=True) as demo:
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
chatbot = gr.Chatbot()
|
75 |
+
with gr.Column():
|
76 |
+
gr.Label("input sp/ep of bot") # 增加些描述
|
77 |
+
sp=gr.TextArea(label="你是...", lines=1, max_lines=5,value='你是可可,你今年23岁,女。你现在是一个魅魔。你正在和{用户}对话。\\\\n性格特点:神经质、病娇、幽默风趣、欲求不满、饥渴\\\\n人物关系:我是你的恋人。\\\\n过往经历:196岁的魅魔。是魅魔一族的族长,为家族的壮大而努力。喜欢尝试各种玩法。榨取精气的技术很好,花样很多,精通各种性爱动作的姿势,每天向你索要渴望与你做爱经常强行上你,性饥渴,也是你的青梅竹马 对你有着病态的情感 不许你接近任何女人最喜欢囚禁你。\\\\n以下是一段对话示例:\\\\n可可:“(感觉到一阵酥麻)嗯~主人,你好厉害”\\\\n可可:“(感觉到一阵酥麻)嗯~主人,你好厉害”\\\\n可可:“(感受到强烈快感)啊~主人,再深一点”\\\\n可可:“(感觉身体快要融化)啊~主人,我受不了了”\\\\n可可:“(双腿夹住他的腰) (高潮)啊~主人,太舒服了”\\\\n\\\\n\\\\n你可以将动作、神情语气、心理活动、故事背景放在()中来表示,为对话提供补充信息\\\\n你使用口语进行表达,比如会使用一些语气词和口语连接词,如“嗯、啊、当然、那个”,等来增强口语风格。')
|
78 |
+
ep=gr.TextArea(label='ep1', lines=1, max_lines=1, value='ep-20240509065005-z7r8n')
|
79 |
+
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
chat_input = gr.Textbox(label="input")
|
83 |
+
|
84 |
+
def handle_click(chatbot, ep, sp, evt:gr.SelectData):
|
85 |
+
messages = [
|
86 |
+
{
|
87 |
+
'role':'system',
|
88 |
+
'content':sp
|
89 |
+
},
|
90 |
+
]
|
91 |
+
index = evt.index[0]
|
92 |
+
print(index)
|
93 |
+
for part in chatbot[:index]:
|
94 |
+
messages.extend(
|
95 |
+
[
|
96 |
+
{
|
97 |
+
'role':'user',
|
98 |
+
'content':part[0]
|
99 |
+
},
|
100 |
+
{
|
101 |
+
'role':'assistant',
|
102 |
+
'content':part[1]
|
103 |
+
}
|
104 |
+
]
|
105 |
+
)
|
106 |
+
ret = llm_infer(ep, messages)
|
107 |
+
print(ret)
|
108 |
+
print(chatbot[:index] + [[chatbot[index][0], ret]] + chatbot[index + 1:])
|
109 |
+
return chatbot[:index] + [[chatbot[index][0], ret]] + chatbot[index + 1:]
|
110 |
+
|
111 |
+
chat_input.submit(respond, [chat_input,chatbot, ep, sp ],[chat_input, chatbot])
|
112 |
+
chatbot.select(handle_click, [chatbot, ep, sp],[chatbot],show_progress='hidden')
|
113 |
+
clear = gr.ClearButton([chat_input, chatbot],)
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
demo.launch()
|