Nyanfa commited on
Commit
11c55db
·
verified ·
1 Parent(s): 7fec6a7

Minor adjustments

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -82,7 +82,9 @@ def get_ai_response(messages):
82
  if chunk.choices and chunk.choices[0].delta.get("content"):
83
  content = chunk.choices[0].delta["content"]
84
  st.session_state.response += content
85
- shown_message += content.replace("\n", " \n")
 
 
86
  placeholder.markdown(shown_message)
87
 
88
  if st.session_state.prefill == st.session_state.response:
@@ -93,18 +95,22 @@ def get_ai_response(messages):
93
  st.session_state.is_streaming = False
94
  return st.session_state.response
95
 
96
- def replace_spaces_in_code_blocks(match):
97
- return match.group(0).replace(" \n", "\n")
 
 
98
 
99
  code_block_pattern = r"(```.*?```)"
100
 
101
  def display_messages():
102
  for i, message in enumerate(st.session_state.messages):
103
  with st.chat_message(message["role"]):
104
- shown_message = message["content"].replace("\n", " \n")
 
 
105
  if "```" in shown_message:
106
  # Replace " \n" with "\n" within code blocks
107
- shown_message = re.sub(code_block_pattern, replace_spaces_in_code_blocks, shown_message, flags=re.DOTALL)
108
  st.markdown(shown_message)
109
 
110
  col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
@@ -169,6 +175,8 @@ def display_messages():
169
 
170
  # Add sidebar for advanced settings
171
  with st.sidebar:
 
 
172
  # Copy Conversation History button
173
  log_text = ""
174
  for message in st.session_state.messages:
@@ -240,7 +248,8 @@ with st.sidebar:
240
  model = st.selectbox("Model", options=model_list, index=0)
241
  system_prompt = st.text_area("System prompt", height=200)
242
 
243
- st.session_state.prefill = st.text_area("Prefill", height=50, value=st.session_state.prefill, placeholder="It only works well with the Claude models.")
 
244
  save_prefill = st.toggle("Save the @prefill command input in the sidebar", value=True)
245
 
246
  temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=1.0, step=0.1)
@@ -314,7 +323,7 @@ if "retry_flag" in st.session_state and st.session_state.retry_flag == True:
314
  else:
315
  st.session_state.retry_flag = False
316
 
317
- if prompt := st.chat_input("What is up?"):
318
  used_prefill = False
319
  prefill_pattern = r"([@@](prefill|ぷれふぃる|プレフィル)\s?(.*))"
320
  prefill_match = re.search(prefill_pattern, prompt)
@@ -329,8 +338,12 @@ if prompt := st.chat_input("What is up?"):
329
  st.session_state.messages.append({"role": "user", "content": prompt})
330
  messages = st.session_state.messages.copy()
331
 
 
 
 
 
332
  with st.chat_message("user"):
333
- st.write(prompt)
334
 
335
  response = get_ai_response(messages)
336
 
 
82
  if chunk.choices and chunk.choices[0].delta.get("content"):
83
  content = chunk.choices[0].delta["content"]
84
  st.session_state.response += content
85
+ shown_message += content.replace("\n", " \n")\
86
+ .replace("<", "\\<")\
87
+ .replace(">", "\\>")
88
  placeholder.markdown(shown_message)
89
 
90
  if st.session_state.prefill == st.session_state.response:
 
95
  st.session_state.is_streaming = False
96
  return st.session_state.response
97
 
98
+ def normalize_code_block(match):
99
+ return match.group(0).replace(" \n", "\n")\
100
+ .replace("\\<", "<")\
101
+ .replace("\\>", ">")
102
 
103
  code_block_pattern = r"(```.*?```)"
104
 
105
  def display_messages():
106
  for i, message in enumerate(st.session_state.messages):
107
  with st.chat_message(message["role"]):
108
+ shown_message = message["content"].replace("\n", " \n")\
109
+ .replace("<", "\\<")\
110
+ .replace(">", "\\>")
111
  if "```" in shown_message:
112
  # Replace " \n" with "\n" within code blocks
113
+ shown_message = re.sub(code_block_pattern, normalize_code_block, shown_message, flags=re.DOTALL)
114
  st.markdown(shown_message)
115
 
116
  col1, col2, col3, col4 = st.columns([1, 1, 1, 1])
 
175
 
176
  # Add sidebar for advanced settings
177
  with st.sidebar:
178
+ st.markdown("Help (Japanese): https://rentry.org/9hgneofz")
179
+
180
  # Copy Conversation History button
181
  log_text = ""
182
  for message in st.session_state.messages:
 
248
  model = st.selectbox("Model", options=model_list, index=0)
249
  system_prompt = st.text_area("System prompt", height=200)
250
 
251
+ st.session_state.prefill = st.text_area("Prefill", height=50, value=st.session_state.prefill, placeholder="It only works well with the Claude models.",
252
+ help="You can prefill the assistant's responses. You can also directly type the @prefill command into the chat field (e.g., \"Write a novel. @prefill Sure! I'd be happy to write a novel for you.\")")
253
  save_prefill = st.toggle("Save the @prefill command input in the sidebar", value=True)
254
 
255
  temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=1.0, step=0.1)
 
323
  else:
324
  st.session_state.retry_flag = False
325
 
326
+ if prompt := st.chat_input("Enter your message here..."):
327
  used_prefill = False
328
  prefill_pattern = r"([@@](prefill|ぷれふぃる|プレフィル)\s?(.*))"
329
  prefill_match = re.search(prefill_pattern, prompt)
 
338
  st.session_state.messages.append({"role": "user", "content": prompt})
339
  messages = st.session_state.messages.copy()
340
 
341
+ shown_message = prompt.replace("\n", " \n")\
342
+ .replace("<", "\\<")\
343
+ .replace(">", "\\>")
344
+
345
  with st.chat_message("user"):
346
+ st.write(shown_message)
347
 
348
  response = get_ai_response(messages)
349