maxmon commited on
Commit
284628d
·
1 Parent(s): 6ebd5a9

feat: 初始化

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. LICENSE +21 -0
  3. app.py +59 -0
  4. src/proxy.py +20 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ data/
2
+ *.pyc
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 LLMLab
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio
2
+ from src.proxy import chat
3
+
4
+ # 左边是介绍、聊天记录,右边是对话框
5
+
6
+ dd_prompt = '''下面是一个数字分身{name}的性格、介绍:
7
+ {desc}
8
+ 下面是数字分身和别人的对话:
9
+ {chat}
10
+ 请针对上文,扮演这个数字分身,和我聊天,接下来你会和我说:'''
11
+
12
+ # 哆啦A梦
13
+
14
+ # 哆啦A梦,有各种各样的道具,喜欢帮助别人,喜欢吃铜锣烧
15
+
16
+ # 哆啦A梦:大雄,你怎么又闯祸了!!!
17
+
18
+ def dd_chat(dd_name, dd_desc, dd_example_chat, msg, his):
19
+ dd_soul = dd_prompt.format(name=dd_name, desc=dd_desc, chat=dd_example_chat)
20
+ _his = [[dd_soul, ''], *his]
21
+ _res = chat(msg, _his)
22
+ his.append([msg, _res])
23
+ return '', his
24
+
25
+ # 用一个小故事来测试性格
26
+
27
+ # 人物介绍
28
+
29
+ # 聊天记录
30
+
31
+ # 基于性格、聊天记录来对话
32
+
33
+ import gradio as gr
34
+ import os
35
+ import random
36
+ import time
37
+
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("Start typing below and then click **Run** to see the output.")
40
+ with gr.Row():
41
+ with gr.Column():
42
+ dd_name = gr.Textbox(placeholder="昵称", lines=1)
43
+ dd_desc = gr.Textbox(placeholder="介绍一下你想做的分身,比如性格、爱好、职业、梦想、年龄等等", lines=5)
44
+ dd_example_chat = gr.Textbox(placeholder="输入分身语录,或和别人的对话", lines=10)
45
+
46
+ with gr.Column():
47
+ chatbot = gr.Chatbot(lines=10, placeholder="和分身聊天", live=True)
48
+ msg = gr.Textbox()
49
+ with gr.Row():
50
+ chat_btn = gr.Button("Chat")
51
+ clear = gr.Button("Clear")
52
+ chat_btn.click(dd_chat, [dd_name, dd_desc, dd_example_chat, msg, chatbot], [msg, chatbot])
53
+ msg.submit(dd_chat, [dd_name, dd_desc, dd_example_chat, msg, chatbot], [msg, chatbot])
54
+ gr.Examples(inputs=[dd_name, dd_desc, dd_example_chat], examples=[['哆啦A梦', '哆啦A梦,有各种各样的道具,喜欢帮助别人,喜欢吃铜锣烧', '哆啦A梦:大雄,你怎么又闯祸了!!!']])
55
+ # btn = gr.Button("Run")
56
+ # btn.click(fn=update, inputs=inp, outputs=out)
57
+
58
+ if __name__ == '__main__':
59
+ demo.launch()
src/proxy.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ def chat(input, history=[]):
4
+ headers = {
5
+ "Authorization": "Bearer sk-TmUhOQKWsJ5t43QVoGBblSw3GFOMZwZhpGFlCGX7jxwedsdN"
6
+ }
7
+ _history = []
8
+ for item in history:
9
+ _history.append({"role": "user", "content": item[0]})
10
+ _history.append({"role": "assistant", "content": item[1]})
11
+ j = {
12
+ "model": "gpt-3.5-turbo",
13
+ "messages": [*_history, {"role": "user", "content": input}],
14
+ "temperature": 0.7
15
+ }
16
+ result = requests.post('https://api.aiproxy.io/v1/chat/completions', json=j, headers=headers)
17
+ return result.json()['choices'][0]['message']['content']
18
+
19
+ if __name__ == '__main__':
20
+ print(chat('你好'))