simondby commited on
Commit
540467e
·
1 Parent(s): 1937c6b

update code block

Browse files
Files changed (2) hide show
  1. app.py +35 -33
  2. styles.css +8 -2
app.py CHANGED
@@ -16,6 +16,8 @@ user_key = "" # 在这里输入你的 API 密钥
16
  # password = os.environ.get('pass')
17
 
18
  API_URL = "https://api.openai.com/v1/chat/completions"
 
 
19
  HISTORY_DIR = "history"
20
  TEMPLATES_DIR = "templates"
21
 
@@ -25,9 +27,7 @@ def gen_sys_prompt():
25
  tz = pytz.timezone('Asia/Shanghai')
26
  now = datetime.datetime.now(tz)
27
  timestamp = now.strftime("%Y年%m月%d日 %H:%M:%S")
28
- sys_prompt = f"""现在是{timestamp},因此有些答案可能已经过时。
29
- 你的目的是如实解答用户问题,对于不知道的问题,你需要回复你不知道。
30
- 表格的效果展示直接以HTML格式输出而非markdown格式。"""
31
  return sys_prompt
32
 
33
  # get timestamp
@@ -147,42 +147,44 @@ if dockerflag:
147
  def parse_text(text):
148
  lines = text.split("\n")
149
  lines = [line for line in lines if line != ""]
150
- count = 0
151
- firstline = False
 
152
  for i, line in enumerate(lines):
 
 
153
  if "```" in line:
154
  count += 1
155
  items = line.split('`')
156
- if count % 2 == 1:
157
  lines[i] = f"<pre><code class='language-{items[-1]}'>"
158
- firstline = True
159
- else:
160
  lines[i] = f'</code></pre>'
161
- else:
162
- if i > 0:
163
- if count % 2 == 1:
164
- line = line.replace("&", "&amp;")
165
- # line = line.replace("\"", "`\"`")
166
- # line = line.replace("\'", "`\'`")
167
- line = line.replace("<", "&lt;")
168
- line = line.replace(">", "&gt;")
169
- line = line.replace(" ", "&nbsp;")
170
- line = line.replace("*", "&ast;")
171
- line = line.replace("_", "&lowbar;")
172
- line = line.replace("#", "&#35;")
173
- line = line.replace("-", "&#45;")
174
- line = line.replace(".", "&#46;")
175
- line = line.replace("!", "&#33;")
176
- line = line.replace("(", "&#40;")
177
- line = line.replace(")", "&#41;")
178
- lines[i] = line + "\n"
179
- text = "".join(lines)[:-1]
 
 
180
  return text
181
 
182
 
183
- # 事件:发送消息
184
- # def sent_message(inputs, top_p, temperature, max_tokens, user_key, session_id, chat_id, chatbot=[], history=[], system_prompt=gen_sys_prompt, regenerate=False, summary=False):
185
-
186
 
187
  # 请求API
188
  def openai_request(inputs, top_p, temperature, max_tokens, user_key, session_id, chat_id, chatbot=[], history=[], system_prompt=gen_sys_prompt, regenerate=False, summary=False): # repetition_penalty, top_k
@@ -319,9 +321,9 @@ def openai_request(inputs, top_p, temperature, max_tokens, user_key, session_id,
319
  chat_id=chat_id,
320
  api_key=api_key,
321
  round=chat_round,
322
- system=system_prompt.replace("'","\'").replace('"','\"'),
323
- user=inputs.replace("'","\'").replace('"','\"'),
324
- assistant=history_write,
325
  messages='',
326
  payload=paras,
327
  username=logged_in_user
 
16
  # password = os.environ.get('pass')
17
 
18
  API_URL = "https://api.openai.com/v1/chat/completions"
19
+ API_URL = "https://openai.hex.im/v1/chat/completions"
20
+ API_URL = "https://yuns.deno.dev/v1/chat/completions"
21
  HISTORY_DIR = "history"
22
  TEMPLATES_DIR = "templates"
23
 
 
27
  tz = pytz.timezone('Asia/Shanghai')
28
  now = datetime.datetime.now(tz)
29
  timestamp = now.strftime("%Y年%m月%d日 %H:%M:%S")
30
+ sys_prompt = f"你的目的是如实解答用户问题,对于不知道的问题,你需要回复你不知道。现在是{timestamp},有些信息可能已经过时。表格的效果展示直接以HTML格式输出而非markdown格式。"
 
 
31
  return sys_prompt
32
 
33
  # get timestamp
 
147
  def parse_text(text):
148
  lines = text.split("\n")
149
  lines = [line for line in lines if line != ""]
150
+ count = 0 # locate the start & end of the code block
151
+ in_code_block = False # track if we're currently in a code block
152
+
153
  for i, line in enumerate(lines):
154
+
155
+ # code block indicator
156
  if "```" in line:
157
  count += 1
158
  items = line.split('`')
159
+ if count % 2 == 1: # start
160
  lines[i] = f"<pre><code class='language-{items[-1]}'>"
161
+ in_code_block = True
162
+ else: # end
163
  lines[i] = f'</code></pre>'
164
+ in_code_block = False
165
+
166
+ # normal text
167
+ elif in_code_block:
168
+ line = line.replace("&", "&amp;")
169
+ # line = line.replace("\"", "`\"`")
170
+ # line = line.replace("\'", "`\'`")
171
+ line = line.replace("<", "&lt;")
172
+ line = line.replace(">", "&gt;")
173
+ line = line.replace(" ", "&nbsp;")
174
+ line = line.replace("*", "&ast;")
175
+ line = line.replace("_", "&lowbar;")
176
+ line = line.replace("#", "&#35;")
177
+ line = line.replace("-", "&#45;")
178
+ line = line.replace(".", "&#46;")
179
+ line = line.replace("!", "&#33;")
180
+ line = line.replace("(", "&#40;")
181
+ line = line.replace(")", "&#41;")
182
+ lines[i] = line + "<br>"
183
+
184
+ text = "".join(lines)
185
  return text
186
 
187
 
 
 
 
188
 
189
  # 请求API
190
  def openai_request(inputs, top_p, temperature, max_tokens, user_key, session_id, chat_id, chatbot=[], history=[], system_prompt=gen_sys_prompt, regenerate=False, summary=False): # repetition_penalty, top_k
 
321
  chat_id=chat_id,
322
  api_key=api_key,
323
  round=chat_round,
324
+ system=system_prompt.replace("'","\'").replace('"','\"').replace('\n',''),
325
+ user=inputs.replace("'","\'").replace('"','\"').replace('\n',''),
326
+ assistant=history_write.replace('\n',''),
327
  messages='',
328
  payload=paras,
329
  username=logged_in_user
styles.css CHANGED
@@ -10,11 +10,14 @@ footer {visibility: hidden;}
10
  /* 表格 */
11
  table {
12
  margin: 1em 0;
13
- border-collapse: collapse;
 
 
14
  empty-cells: show;
 
15
  }
16
  td,th {
17
- border: 1.2px solid #697070 !important;
18
  padding: 0.2em;
19
  }
20
  thead {
@@ -23,6 +26,9 @@ thead {
23
  thead th {
24
  padding: .5em .2em;
25
  }
 
 
 
26
  /* 行内代码 */
27
  code {
28
  display: inline;
 
10
  /* 表格 */
11
  table {
12
  margin: 1em 0;
13
+ border-collapse: separate;
14
+ border-spacing: 0px 0px;
15
+ border: 1px solid #333;
16
  empty-cells: show;
17
+ width: 100%;
18
  }
19
  td,th {
20
+ border: 1px solid #697070 !important;
21
  padding: 0.2em;
22
  }
23
  thead {
 
26
  thead th {
27
  padding: .5em .2em;
28
  }
29
+ tbody td {
30
+ text-indent: .5em;
31
+ }
32
  /* 行内代码 */
33
  code {
34
  display: inline;