Siyuan0730 commited on
Commit
34a0cb1
·
1 Parent(s): 3ced0e6
Files changed (1) hide show
  1. app.py +33 -31
app.py CHANGED
@@ -205,7 +205,7 @@ def decorate_user_question(user_question, retrieved_chunks_for_user):
205
  '''
206
  return decorated_prompt
207
 
208
- def initialize_app(added_files, num_lessons, language):
209
  temp_file_paths = []
210
  with st.spinner('Processing file...'):
211
  for added_file in added_files:
@@ -214,26 +214,30 @@ def initialize_app(added_files, num_lessons, language):
214
  tmp_path = tmp.name
215
  temp_file_paths.append(tmp_path)
216
  st.success('Processing file...Done')
 
217
 
 
218
  with st.spinner('Constructing vector database from provided materials...'):
219
  embeddings_df, faiss_index = constructVDB(temp_file_paths)
220
  st.success("Constructing vector database from provided materials...Done")
221
-
 
 
222
  with st.spinner('Generating Course Outline...'):
223
  course_outline_list = courseOutlineGenerating(temp_file_paths, num_lessons, language)
224
  st.success("Generating Course Outline...Done")
225
-
226
- outline_presenting = st.empty()
227
  course_outline_string = ''
228
  lessons_count = 0
229
  for outline in course_outline_list:
230
  lessons_count += 1
231
  course_outline_string += f"{lessons_count}." + outline[0]
232
  course_outline_string += '\n\n' + outline[1] + '\n\n'
233
- with outline_presenting.expander("Check the course outline", expanded=False):
234
  st.write(course_outline_string)
 
 
235
 
236
- content_presenting = st.empty()
237
  count_generating_content = 0
238
  course_content_list = []
239
  for lesson in course_outline_list:
@@ -243,26 +247,23 @@ def initialize_app(added_files, num_lessons, language):
243
  courseContent = generateCourse(lesson, retrievedChunksList, language)
244
  course_content_list.append(courseContent)
245
  st.success(f"Writing content for lesson {count_generating_content}...Done")
246
- with content_presenting.expander(f"Learn the lesson {count_generating_content} ", expanded=False):
247
  st.markdown(courseContent)
248
 
249
- outline_presenting.empty()
250
- content_presenting.empty()
251
-
252
- return embeddings_df, faiss_index, course_outline_list, course_content_list
253
 
254
  def regenerate_outline(course_outline_list):
255
- #try:
256
- course_outline_string = ''
257
- lessons_count = 0
258
- for outline in course_outline_list:
259
- lessons_count += 1
260
- course_outline_string += f"{lessons_count}." + outline[0]
261
- course_outline_string += '\n\n' + outline[1] + '\n\n'
262
- with st.expander("Check the course outline", expanded=False):
263
- st.write(course_outline_string)
264
- #except Exception:
265
- # pass
266
 
267
  def regenerate_content(course_content_list):
268
  try:
@@ -302,24 +303,25 @@ def app():
302
  st.session_state.messages = []
303
 
304
  if btn:
305
- st.session_state.embeddings_df,
306
- st.session_state.faiss_index,
307
- st.session_state.course_outline_list,
308
- st.session_state.course_content_list = initialize_app(added_files, num_lessons, language)
309
- #embeddings_df, faiss_index, course_outline_list = initialize_app(added_files, num_lessons, language)
310
 
311
  col1, col2 = st.columns([0.6,0.4])
312
 
313
  # 这里还可以设置col1 和 col2 不同时出现 - 需要拆分initialize_app()函数
314
- if st.session_state.course_content_list != 1:
315
 
316
- user_question = st.chat_input("Enter your questions when learning...")
317
 
 
 
318
  with col1:
319
  #把课程大纲打印出来
320
  regenerate_outline(st.session_state.course_outline_list)
321
  #把课程内容打印出来
322
- #regenerate_content(st.session_state.course_content_list)
323
 
324
  with col2:
325
  st.caption(''':blue[AI Assistant]: Ask this TA any questions related to this course and get direct answers. :sunglasses:''')
@@ -354,7 +356,7 @@ def app():
354
  message_placeholder.markdown(full_response + "▌")
355
  message_placeholder.markdown(full_response)
356
  st.session_state.messages.append({"role": "assistant", "content": [full_response,1]})
357
-
358
 
359
 
360
  if __name__ == "__main__":
 
205
  '''
206
  return decorated_prompt
207
 
208
+ def initialize_file(added_files):
209
  temp_file_paths = []
210
  with st.spinner('Processing file...'):
211
  for added_file in added_files:
 
214
  tmp_path = tmp.name
215
  temp_file_paths.append(tmp_path)
216
  st.success('Processing file...Done')
217
+ return temp_file_paths
218
 
219
+ def initialize_vdb(temp_file_paths):
220
  with st.spinner('Constructing vector database from provided materials...'):
221
  embeddings_df, faiss_index = constructVDB(temp_file_paths)
222
  st.success("Constructing vector database from provided materials...Done")
223
+ return embeddings_df, faiss_index
224
+
225
+ def initialize_outline(temp_file_paths, num_lessons, language):
226
  with st.spinner('Generating Course Outline...'):
227
  course_outline_list = courseOutlineGenerating(temp_file_paths, num_lessons, language)
228
  st.success("Generating Course Outline...Done")
 
 
229
  course_outline_string = ''
230
  lessons_count = 0
231
  for outline in course_outline_list:
232
  lessons_count += 1
233
  course_outline_string += f"{lessons_count}." + outline[0]
234
  course_outline_string += '\n\n' + outline[1] + '\n\n'
235
+ with st.expander("Check the course outline", expanded=False):
236
  st.write(course_outline_string)
237
+
238
+ return course_outline_list
239
 
240
+ def initialize_content(course_outline_list, embeddings_df, faiss_index, language):
241
  count_generating_content = 0
242
  course_content_list = []
243
  for lesson in course_outline_list:
 
247
  courseContent = generateCourse(lesson, retrievedChunksList, language)
248
  course_content_list.append(courseContent)
249
  st.success(f"Writing content for lesson {count_generating_content}...Done")
250
+ with st.expander(f"Learn the lesson {count_generating_content} ", expanded=False):
251
  st.markdown(courseContent)
252
 
253
+ return course_content_list
 
 
 
254
 
255
  def regenerate_outline(course_outline_list):
256
+ try:
257
+ course_outline_string = ''
258
+ lessons_count = 0
259
+ for outline in course_outline_list:
260
+ lessons_count += 1
261
+ course_outline_string += f"{lessons_count}." + outline[0]
262
+ course_outline_string += '\n\n' + outline[1] + '\n\n'
263
+ with st.expander("Check the course outline", expanded=False):
264
+ st.write(course_outline_string)
265
+ except Exception:
266
+ pass
267
 
268
  def regenerate_content(course_content_list):
269
  try:
 
303
  st.session_state.messages = []
304
 
305
  if btn:
306
+ #initialize app
307
+ temp_file_paths = initialize_file(added_files)
308
+ st.session_state.embeddings_df, st.session_state.faiss_index = initialize_vdb(temp_file_paths)
309
+ st.session_state.course_outline_list = initialize_outline(temp_file_paths, num_lessons, language)
310
+ st.session_state.course_content_list = initialize_content(st.session_state.course_outline_list, st.session_state.embeddings_df, st.session_state.faiss_index, language)
311
 
312
  col1, col2 = st.columns([0.6,0.4])
313
 
314
  # 这里还可以设置col1 和 col2 不同时出现 - 需要拆分initialize_app()函数
 
315
 
316
+ user_question = st.chat_input("Enter your questions when learning...")
317
 
318
+ if st.session_state.course_content_list != 1:
319
+
320
  with col1:
321
  #把课程大纲打印出来
322
  regenerate_outline(st.session_state.course_outline_list)
323
  #把课程内容打印出来
324
+ regenerate_content(st.session_state.course_content_list)
325
 
326
  with col2:
327
  st.caption(''':blue[AI Assistant]: Ask this TA any questions related to this course and get direct answers. :sunglasses:''')
 
356
  message_placeholder.markdown(full_response + "▌")
357
  message_placeholder.markdown(full_response)
358
  st.session_state.messages.append({"role": "assistant", "content": [full_response,1]})
359
+
360
 
361
 
362
  if __name__ == "__main__":