Siyuan0730 commited on
Commit
2be4d0b
·
1 Parent(s): c1726fc

改进session

Browse files
Files changed (1) hide show
  1. app.py +64 -58
app.py CHANGED
@@ -262,21 +262,27 @@ def initialize_app(added_files, num_lessons, language):
262
  return embeddings_df, faiss_index, course_outline_list, course_content_list
263
 
264
  def regenerate_outline(course_outline_list):
265
- course_outline_string = ''
266
- lessons_count = 0
267
- for outline in course_outline_list:
268
- lessons_count += 1
269
- course_outline_string += f"{lessons_count}." + outline[0]
270
- course_outline_string += '\n\n' + outline[1] + '\n\n'
271
- with st.expander("Check the course outline", expanded=False):
272
- st.write(course_outline_string)
 
 
 
273
 
274
  def regenerate_content(course_content_list):
275
- count_generating_content = 0
276
- for content in course_content_list:
277
- count_generating_content += 1
278
- with st.expander(f"Learn the lesson {count_generating_content} ", expanded=False):
279
- st.markdown(content)
 
 
 
280
 
281
  def app():
282
  st.title("OmniTutor v0.0.2")
@@ -284,7 +290,7 @@ def app():
284
  with st.sidebar:
285
  st.image("https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231021212525.png")
286
  added_files = st.file_uploader('Upload .md file', type=['.md'], accept_multiple_files=True)
287
- num_lessons = st.slider('How many lessons do you want this course to have?', min_value=3, max_value=14, value=5, step=1)
288
  language = 'English'
289
  Chinese = st.checkbox('Output in Chinese')
290
  if Chinese:
@@ -292,7 +298,6 @@ def app():
292
  btn = st.button('submit')
293
 
294
  if btn:
295
-
296
  if "embeddings_df" and "faiss_index" and "course_outline_list" and "course_content_list" not in st.session_state:
297
  st.session_state.embeddings_df,
298
  st.session_state.faiss_index,
@@ -300,56 +305,57 @@ def app():
300
  st.session_state.course_content_list = initialize_app(added_files, num_lessons, language)
301
  #embeddings_df, faiss_index, course_outline_list = initialize_app(added_files, num_lessons, language)
302
 
303
- col1, col2 = st.columns([0.6,0.4])
304
-
 
 
305
  with col1:
306
  #把课程大纲打印出来
307
  regenerate_outline(st.session_state.course_outline_list)
308
  #把课程内容打印出来
309
  regenerate_content(st.session_state.course_content_list)
310
-
311
- with col2:
312
-
313
- st.caption(''':blue[AI Assistant]: Ask this TA any questions related to this course and get direct answers. :sunglasses:''')
314
- # Set a default model
315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316
  with st.chat_message("assistant"):
317
- st.write("Hello👋, how can I help you today? 😄")
318
-
319
- if "openai_model" not in st.session_state:
320
- st.session_state["openai_model"] = "gpt-3.5-turbo"
321
- # Initialize chat history
322
- if "messages" not in st.session_state:
323
- st.session_state.messages = []
324
-
325
- # Display chat messages from history on app rerun - 这部分不用session,利用好rerun
326
- for message in st.session_state.messages:
327
- with st.chat_message(message["role"]):
328
- st.markdown(message["content"][0])
329
-
330
- user_question = st.chat_input("Enter your questions when learning...")
331
-
332
- #这里的session.state就是保存了这个对话会话的一些基本信息和设置
333
- if user_question:
334
- retrieved_chunks_for_user = searchVDB(user_question, st.session_state.embeddings_df, st.session_state.faiss_index)
335
- #retrieved_chunks_for_user = []
336
- prompt = decorate_user_question(user_question, retrieved_chunks_for_user)
337
- st.session_state.messages.append({"role": "user", "content": [user_question, prompt]})
338
- with st.chat_message("user"):
339
- st.markdown(user_question)
340
- # Display assistant response in chat message container
341
- with st.chat_message("assistant"):
342
- message_placeholder = st.empty()
343
- full_response = ""
344
- for response in openai.ChatCompletion.create(
345
- model=st.session_state["openai_model"],
346
- messages=[{"role": m["role"], "content": m["content"][1]} for m in st.session_state.messages],
347
- stream=True,
348
- ):
349
- full_response += response.choices[0].delta.get("content", "")
350
- message_placeholder.markdown(full_response + "▌")
351
- message_placeholder.markdown(full_response)
352
- st.session_state.messages.append({"role": "assistant", "content": full_response})
353
 
354
 
355
 
 
262
  return embeddings_df, faiss_index, course_outline_list, course_content_list
263
 
264
  def regenerate_outline(course_outline_list):
265
+ try:
266
+ course_outline_string = ''
267
+ lessons_count = 0
268
+ for outline in course_outline_list:
269
+ lessons_count += 1
270
+ course_outline_string += f"{lessons_count}." + outline[0]
271
+ course_outline_string += '\n\n' + outline[1] + '\n\n'
272
+ with st.expander("Check the course outline", expanded=False):
273
+ st.write(course_outline_string)
274
+ except Exception:
275
+ pass
276
 
277
  def regenerate_content(course_content_list):
278
+ try:
279
+ count_generating_content = 0
280
+ for content in course_content_list:
281
+ count_generating_content += 1
282
+ with st.expander(f"Learn the lesson {count_generating_content} ", expanded=False):
283
+ st.markdown(content)
284
+ except Exception:
285
+ pass
286
 
287
  def app():
288
  st.title("OmniTutor v0.0.2")
 
290
  with st.sidebar:
291
  st.image("https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231021212525.png")
292
  added_files = st.file_uploader('Upload .md file', type=['.md'], accept_multiple_files=True)
293
+ num_lessons = st.slider('How many lessons do you want this course to have?', min_value=3, max_value=15, value=5, step=1)
294
  language = 'English'
295
  Chinese = st.checkbox('Output in Chinese')
296
  if Chinese:
 
298
  btn = st.button('submit')
299
 
300
  if btn:
 
301
  if "embeddings_df" and "faiss_index" and "course_outline_list" and "course_content_list" not in st.session_state:
302
  st.session_state.embeddings_df,
303
  st.session_state.faiss_index,
 
305
  st.session_state.course_content_list = initialize_app(added_files, num_lessons, language)
306
  #embeddings_df, faiss_index, course_outline_list = initialize_app(added_files, num_lessons, language)
307
 
308
+ col1, col2 = st.columns([0.6,0.4])
309
+
310
+ # 这里还可以设置col1 和 col2 不同时出现 - 需要拆分initialize_app()函数
311
+ if st.session_state.course_content_list:
312
  with col1:
313
  #把课程大纲打印出来
314
  regenerate_outline(st.session_state.course_outline_list)
315
  #把课程内容打印出来
316
  regenerate_content(st.session_state.course_content_list)
 
 
 
 
 
317
 
318
+ with col2:
319
+ st.caption(''':blue[AI Assistant]: Ask this TA any questions related to this course and get direct answers. :sunglasses:''')
320
+ # Set a default model
321
+
322
+ with st.chat_message("assistant"):
323
+ st.write("Hello👋, how can I help you today? 😄")
324
+
325
+ if "openai_model" not in st.session_state:
326
+ st.session_state["openai_model"] = "gpt-3.5-turbo"
327
+ # Initialize chat history
328
+ if "messages" not in st.session_state:
329
+ st.session_state.messages = []
330
+
331
+ # Display chat messages from history on app rerun - 这部分不用session,利用好rerun
332
+ for message in st.session_state.messages:
333
+ with st.chat_message(message["role"]):
334
+ st.markdown(message["content"][0])
335
+
336
+ user_question = st.chat_input("Enter your questions when learning...")
337
+
338
+ #这里的session.state就是保存了这个对话会话的一些基本信息和设置
339
+ if user_question:
340
+ retrieved_chunks_for_user = searchVDB(user_question, st.session_state.embeddings_df, st.session_state.faiss_index)
341
+ #retrieved_chunks_for_user = []
342
+ prompt = decorate_user_question(user_question, retrieved_chunks_for_user)
343
+ st.session_state.messages.append({"role": "user", "content": [user_question, prompt]})
344
+ with st.chat_message("user"):
345
+ st.markdown(user_question)
346
+ # Display assistant response in chat message container
347
  with st.chat_message("assistant"):
348
+ message_placeholder = st.empty()
349
+ full_response = ""
350
+ for response in openai.ChatCompletion.create(
351
+ model=st.session_state["openai_model"],
352
+ messages=[{"role": m["role"], "content": m["content"][1]} for m in st.session_state.messages],
353
+ stream=True,
354
+ ):
355
+ full_response += response.choices[0].delta.get("content", "")
356
+ message_placeholder.markdown(full_response + "▌")
357
+ message_placeholder.markdown(full_response)
358
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
 
360
 
361