zhangjf commited on
Commit
1857da6
0 Parent(s):

Duplicate from zhangjf/chatbot

Browse files
Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +150 -0
  4. requirements.txt +2 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Chatbot
3
+ emoji: 👀
4
+ colorFrom: orange
5
+ colorTo: white
6
+ sdk: gradio
7
+ sdk_version: 3.16.2
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: zhangjf/chatbot
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import tiktoken
3
+
4
+ import datetime
5
+ import json
6
+ import os
7
+
8
+ openai.api_key = os.getenv('API_KEY')
9
+ openai.request_times = 0
10
+
11
+ def ask(question, history, behavior):
12
+ openai.request_times += 1
13
+ print(f"request times {openai.request_times}: {datetime.datetime.now()}: {question}")
14
+ try:
15
+ response = openai.ChatCompletion.create(
16
+ model="gpt-3.5-turbo",
17
+ messages=forget_long_term(
18
+ [
19
+ {"role":"system", "content":content}
20
+ for content in behavior
21
+ ] + [
22
+ {"role":"user" if i%2==0 else "assistant", "content":content}
23
+ for i,content in enumerate(history + [question])
24
+ ]
25
+ )
26
+ )["choices"][0]["message"]["content"]
27
+ while response.startswith("\n"):
28
+ response = response[1:]
29
+ except Exception as e:
30
+ print(e)
31
+ response = 'Timeout! Please wait a few minutes and retry'
32
+ history = history + [question, response]
33
+ return history
34
+
35
+ def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
36
+ """Returns the number of tokens used by a list of messages."""
37
+ try:
38
+ encoding = tiktoken.encoding_for_model(model)
39
+ except KeyError:
40
+ encoding = tiktoken.get_encoding("cl100k_base")
41
+ if model == "gpt-3.5-turbo": # note: future models may deviate from this
42
+ num_tokens = 0
43
+ for message in messages:
44
+ num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
45
+ for key, value in message.items():
46
+ num_tokens += len(encoding.encode(value))
47
+ if key == "name": # if there's a name, the role is omitted
48
+ num_tokens += -1 # role is always required and always 1 token
49
+ num_tokens += 2 # every reply is primed with <im_start>assistant
50
+ return num_tokens
51
+ else:
52
+ raise NotImplementedError(f"""num_tokens_from_messages() is not presently implemented for model {model}.
53
+ See https://github.com/openai/openai-python/blob/main/chatml.md for information on how messages are converted to tokens.""")
54
+
55
+ def forget_long_term(messages, max_num_tokens=4000):
56
+ while num_tokens_from_messages(messages)>max_num_tokens:
57
+ if messages[0]["role"]=="system" and not len(messages[0]["content"]>=max_num_tokens):
58
+ messages = messages[:1] + messages[2:]
59
+ else:
60
+ messages = messages[1:]
61
+ return messages
62
+
63
+
64
+ import gradio as gr
65
+
66
+
67
+ def to_md(content):
68
+ is_inside_code_block = False
69
+ output_spans = []
70
+ for i in range(len(content)):
71
+ if content[i]=="\n" and not is_inside_code_block:
72
+ if len(output_spans)>0 and output_spans[-1]=="\n```\n":
73
+ pass
74
+ else:
75
+ output_spans.append("<br>")
76
+ elif content[i]=="`":
77
+ output_spans.append(content[i])
78
+ if len(output_spans)>=3 and all([output_spans[j]=="`" for j in [-3,-2,-1]]):
79
+ is_inside_code_block = not is_inside_code_block
80
+ output_spans = output_spans[:-3]
81
+ if not output_spans[-1]=="\n":
82
+ output_spans.append("\n")
83
+ output_spans.append("\n```\n")
84
+ else:
85
+ output_spans.append(content[i])
86
+ return "".join(output_spans)
87
+
88
+
89
+ def predict(question, history=[], behavior=[]):
90
+ history = ask(question, history, behavior)
91
+ response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
92
+ return "", history, response
93
+
94
+
95
+ with gr.Blocks() as demo:
96
+
97
+ examples_txt = [
98
+ ['200字介绍一下凯旋门:'],
99
+ ['网上购物有什么小窍门?'],
100
+ ['补全下述对三亚的介绍:\n三亚位于海南岛的最南端,是'],
101
+ ['将这句文言文翻译成英语:"逝者如斯夫,不舍昼夜。"'],
102
+ ['Question: What\'s the best winter resort city? User: A 10-year professional traveler. Answer: '],
103
+ ['How to help my child to make friends with his classmates? answer this question step by step:'],
104
+ ['polish the following statement for a paper: In this section, we perform case study to give a more intuitive demonstration of our proposed strategies and corresponding explanation.'],
105
+ ]
106
+
107
+ examples_bhv = [
108
+ "你现在是一个带有批判思维的导游,会对景点的优缺点进行中肯的分析。",
109
+ "你现在是一名佛教信仰者,但同时又对世界上其它的宗教和文化保持着包容、尊重和交流的态度。",
110
+ f"You are a helpful assistant. You will answer all the questions step-by-step.",
111
+ f"You are a helpful assistant. Today is {datetime.date.today()}.",
112
+ ]
113
+
114
+ gr.Markdown(
115
+ """
116
+ 朋友你好,
117
+
118
+ 这是我利用[gradio](https://gradio.app/creating-a-chatbot/)编写的一个小网页,用于以网页的形式给大家分享ChatGPT请求服务,希望你玩的开心��关于使用技巧或学术研讨,欢迎在[Community](https://huggingface.co/spaces/zhangjf/chatbot/discussions)中和我交流。
119
+
120
+ p.s. 响应时间和聊天内容长度正相关,一般能在5秒~30秒内响应。
121
+ """)
122
+
123
+ behavior = gr.State([])
124
+
125
+ with gr.Column(variant="panel"):
126
+ with gr.Row().style(equal_height=True):
127
+ with gr.Column(scale=0.85):
128
+ bhv = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT扮演的人设").style(container=False)
129
+ with gr.Column(scale=0.15, min_width=0):
130
+ button_set = gr.Button("Set")
131
+ bhv.submit(fn=lambda x:(x,[x]), inputs=[bhv], outputs=[bhv, behavior])
132
+ button_set.click(fn=lambda x:(x,[x]), inputs=[bhv], outputs=[bhv, behavior])
133
+
134
+
135
+ state = gr.State([])
136
+
137
+ with gr.Column(variant="panel"):
138
+ chatbot = gr.Chatbot()
139
+ txt = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT回答的问题").style(container=False)
140
+ with gr.Row():
141
+ button_gen = gr.Button("Submit")
142
+ button_clr = gr.Button("Clear")
143
+
144
+ gr.Examples(examples=examples_bhv, inputs=bhv, label="Examples for setting behavior")
145
+ gr.Examples(examples=examples_txt, inputs=txt, label="Examples for asking question")
146
+ txt.submit(predict, [txt, state, behavior], [txt, state, chatbot])
147
+ button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot])
148
+ button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])
149
+
150
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai==0.27.0
2
+ tiktoken==0.3.0