zhangjf commited on
Commit
be7969f
·
1 Parent(s): be7cb49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -49
app.py CHANGED
@@ -3,14 +3,11 @@ import tiktoken
3
 
4
  import datetime
5
  import json
6
- import time
7
  import os
8
 
9
  openai.api_key = os.getenv('API_KEY')
10
  openai.request_times = 0
11
 
12
- all_dialogue = []
13
-
14
  def ask(question, history, behavior):
15
  openai.request_times += 1
16
  print(f"request times {openai.request_times}: {datetime.datetime.now()}: {question}")
@@ -37,7 +34,6 @@ def ask(question, history, behavior):
37
  print(e)
38
  response = 'Timeout! Please wait a few minutes and retry'
39
  history = history + [question, response]
40
- record_dialogue(history)
41
  return history
42
 
43
  def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
@@ -68,65 +64,54 @@ def forget_long_term(messages, max_num_tokens=4000):
68
  messages = messages[1:]
69
  return messages
70
 
71
- def record_dialogue(history):
72
- dialogue = json.dumps(history, ensure_ascii=False)
73
- for i in range(len(all_dialogue)):
74
- if dialogue[1:-1].startswith(all_dialogue[i][1:-1]):
75
- all_dialogue[i] = dialogue
76
- return
77
- all_dialogue.append(dialogue)
78
- return
79
-
80
 
81
  import gradio as gr
82
 
83
 
84
  def to_md(content):
85
  is_inside_code_block = False
86
- count_backtick = 0
87
  output_spans = []
88
  for i in range(len(content)):
89
- if content[i]=="\n":
90
- if not is_inside_code_block:
91
- output_spans.append("<br>")
92
  else:
93
- output_spans.append("\n\n")
94
  elif content[i]=="`":
95
- count_backtick += 1
96
- if count_backtick == 3:
97
- count_backtick = 0
98
- is_inside_code_block = not is_inside_code_block
99
  output_spans.append(content[i])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  else:
101
  output_spans.append(content[i])
102
  return "".join(output_spans)
103
 
104
 
105
  def predict(question, history=[], behavior=[]):
106
- if question.startswith(f"{openai.api_key}:"):
107
- return adminInstruct(question, history)
108
  history = ask(question, history, behavior)
109
  response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
110
- return "", history, response, gr.File.update(value=None, visible=False)
111
-
112
-
113
- def retry(question, history=[], behavior=[]):
114
- if len(history)<2:
115
- return "", history, [], gr.File.update(value=None, visible=False)
116
- question = history[-2]
117
- history = history[:-2]
118
- return predict(question, history, behavior)
119
-
120
-
121
- def adminInstruct(question, history):
122
- if "download all dialogue" in question:
123
- filename = f"./all_dialogue_{len(all_dialogue)}.jsonl"
124
- with open(filename, "w", encoding="utf-8") as f:
125
- for dialogue in all_dialogue:
126
- f.write(dialogue + "\n")
127
- response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
128
- return "", history, response, gr.File.update(value=filename, visible=True)
129
- return "", history, response, gr.File.update(value=None, visible=False)
130
 
131
 
132
  with gr.Blocks() as demo:
@@ -173,15 +158,12 @@ with gr.Blocks() as demo:
173
  txt = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT回答的问题").style(container=False)
174
  with gr.Row():
175
  button_gen = gr.Button("Submit")
176
- button_rtr = gr.Button("Retry")
177
  button_clr = gr.Button("Clear")
178
-
179
- downloadfile = gr.File(None, interactive=False, show_label=False, visible=False)
180
  gr.Examples(examples=examples_bhv, inputs=bhv, label="Examples for setting behavior")
181
  gr.Examples(examples=examples_txt, inputs=txt, label="Examples for asking question")
182
  txt.submit(predict, [txt, state, behavior], [txt, state, chatbot])
183
- button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot, downloadfile])
184
- button_rtr.click(fn=retry, inputs=[txt, state, behavior], outputs=[txt, state, chatbot, downloadfile])
185
  button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])
186
 
187
  demo.queue(concurrency_count=3, max_size=10)
 
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}")
 
34
  print(e)
35
  response = 'Timeout! Please wait a few minutes and retry'
36
  history = history + [question, response]
 
37
  return history
38
 
39
  def num_tokens_from_messages(messages, model="gpt-3.5-turbo"):
 
64
  messages = messages[1:]
65
  return messages
66
 
 
 
 
 
 
 
 
 
 
67
 
68
  import gradio as gr
69
 
70
 
71
  def to_md(content):
72
  is_inside_code_block = False
 
73
  output_spans = []
74
  for i in range(len(content)):
75
+ if content[i]=="\n" and not is_inside_code_block:
76
+ if len(output_spans)>0 and output_spans[-1].endswith("```"):
77
+ output_spans.append("\n")
78
  else:
79
+ output_spans.append("<br>")
80
  elif content[i]=="`":
 
 
 
 
81
  output_spans.append(content[i])
82
+ if len(output_spans)>=3 and all([output_spans[j]=="`" for j in [-3,-2,-1]]):
83
+ is_inside_code_block = not is_inside_code_block
84
+ output_spans = output_spans[:-3]
85
+ if is_inside_code_block:
86
+ if len(output_spans)==0:
87
+ output_spans.append("```")
88
+ elif output_spans[-1]=="<br>":
89
+ output_spans[-1] = "\n"
90
+ output_spans.append("```")
91
+ elif output_spans[-1].endswith("\n"):
92
+ output_spans.append("```")
93
+ else:
94
+ output_spans.append("\n```")
95
+
96
+ if i+1<len(content) and content[i+1]!="\n":
97
+ output_spans.append("\n")
98
+ else:
99
+ if output_spans[-1].endswith("\n"):
100
+ output_spans.append("```")
101
+ else:
102
+ output_spans.append("\n```")
103
+
104
+ if i+1<len(content) and content[i+1]!="\n":
105
+ output_spans.append("\n")
106
  else:
107
  output_spans.append(content[i])
108
  return "".join(output_spans)
109
 
110
 
111
  def predict(question, history=[], behavior=[]):
 
 
112
  history = ask(question, history, behavior)
113
  response = [(to_md(history[i]),to_md(history[i+1])) for i in range(0,len(history)-1,2)]
114
+ return "", history, response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
 
117
  with gr.Blocks() as demo:
 
158
  txt = gr.Textbox(show_label=False, placeholder="输入你想让ChatGPT回答的问题").style(container=False)
159
  with gr.Row():
160
  button_gen = gr.Button("Submit")
 
161
  button_clr = gr.Button("Clear")
162
+
 
163
  gr.Examples(examples=examples_bhv, inputs=bhv, label="Examples for setting behavior")
164
  gr.Examples(examples=examples_txt, inputs=txt, label="Examples for asking question")
165
  txt.submit(predict, [txt, state, behavior], [txt, state, chatbot])
166
+ button_gen.click(fn=predict, inputs=[txt, state, behavior], outputs=[txt, state, chatbot])
 
167
  button_clr.click(fn=lambda :([],[]), inputs=None, outputs=[chatbot, state])
168
 
169
  demo.queue(concurrency_count=3, max_size=10)