youngtsai commited on
Commit
2b5e430
1 Parent(s): 79fc5f8

def get_questions(video_id, df_string):

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -571,29 +571,38 @@ def generate_questions(df_string):
571
  return questions
572
 
573
  def get_questions(video_id, df_string):
574
- if not video_id:
 
 
 
 
 
 
 
 
 
575
  questions = generate_questions(df_string)
 
 
 
576
  else:
577
- # 去 g drive 確認是有有 video_id_questions.json
578
- print("===get_questions===")
579
- service = init_drive_service()
580
- parent_folder_id = '1GgI4YVs0KckwStVQkLa1NZ8IpaEMurkL'
581
- folder_id = create_folder_if_not_exists(service, video_id, parent_folder_id)
582
- file_name = f'{video_id}_questions.json'
583
-
584
- # 检查檔案是否存在
585
- exists, file_id = check_file_exists(service, folder_id, file_name)
586
- if not exists:
587
- questions = generate_questions(df_string)
588
- questions_text = json.dumps(questions, ensure_ascii=False, indent=2)
589
- upload_content_directly(service, file_name, folder_id, questions_text)
590
- print("questions已上傳到Google Drive")
591
- else:
592
- # 逐字稿已存在,下载逐字稿内容
593
- print("questions已存在于Google Drive中")
594
- questions_text = download_file_as_string(service, file_id)
595
- questions = json.loads(questions_text)
596
 
 
 
597
  q1 = questions[0] if len(questions) > 0 else ""
598
  q2 = questions[1] if len(questions) > 1 else ""
599
  q3 = questions[2] if len(questions) > 2 else ""
@@ -604,6 +613,7 @@ def get_questions(video_id, df_string):
604
  print("=====get_questions=====")
605
  return q1, q2, q3
606
 
 
607
  def send_question(question, df_string_output, chat_history):
608
  # 当问题按钮被点击时调用此函数
609
  return respond(question, df_string_output, chat_history)
@@ -753,7 +763,7 @@ with gr.Blocks() as demo:
753
  btn_2.click(respond, inputs=[btn_2, df_string_output, chatbot], outputs=[msg, chatbot])
754
  btn_3.click(respond, inputs=[btn_3, df_string_output, chatbot], outputs=[msg, chatbot])
755
 
756
- btn_create_question.click(get_questions, inputs = ["", df_string_output], outputs = [btn_1, btn_2, btn_3])
757
 
758
  # file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
759
  file_upload.change(process_file, inputs=file_upload, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
 
571
  return questions
572
 
573
  def get_questions(video_id, df_string):
574
+ # g drive 確認是有有 video_id_questions.json
575
+ print("===get_questions===")
576
+ service = init_drive_service()
577
+ parent_folder_id = '1GgI4YVs0KckwStVQkLa1NZ8IpaEMurkL'
578
+ folder_id = create_folder_if_not_exists(service, video_id, parent_folder_id)
579
+ file_name = f'{video_id}_questions.json'
580
+
581
+ # 检查檔案是否存在
582
+ exists, file_id = check_file_exists(service, folder_id, file_name)
583
+ if not exists:
584
  questions = generate_questions(df_string)
585
+ questions_text = json.dumps(questions, ensure_ascii=False, indent=2)
586
+ upload_content_directly(service, file_name, folder_id, questions_text)
587
+ print("questions已上傳到Google Drive")
588
  else:
589
+ # 逐字稿已存在,下载逐字稿内容
590
+ print("questions已存在于Google Drive中")
591
+ questions_text = download_file_as_string(service, file_id)
592
+ questions = json.loads(questions_text)
593
+
594
+ q1 = questions[0] if len(questions) > 0 else ""
595
+ q2 = questions[1] if len(questions) > 1 else ""
596
+ q3 = questions[2] if len(questions) > 2 else ""
597
+ print("=====get_questions=====")
598
+ print(f"q1: {q1}")
599
+ print(f"q2: {q2}")
600
+ print(f"q3: {q3}")
601
+ print("=====get_questions=====")
602
+ return q1, q2, q3
 
 
 
 
 
603
 
604
+ def change_questions(df_string):
605
+ questions = generate_questions(df_string)
606
  q1 = questions[0] if len(questions) > 0 else ""
607
  q2 = questions[1] if len(questions) > 1 else ""
608
  q3 = questions[2] if len(questions) > 2 else ""
 
613
  print("=====get_questions=====")
614
  return q1, q2, q3
615
 
616
+
617
  def send_question(question, df_string_output, chat_history):
618
  # 当问题按钮被点击时调用此函数
619
  return respond(question, df_string_output, chat_history)
 
763
  btn_2.click(respond, inputs=[btn_2, df_string_output, chatbot], outputs=[msg, chatbot])
764
  btn_3.click(respond, inputs=[btn_3, df_string_output, chatbot], outputs=[msg, chatbot])
765
 
766
+ btn_create_question.click(change_questions, inputs = [df_string_output], outputs = [btn_1, btn_2, btn_3])
767
 
768
  # file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
769
  file_upload.change(process_file, inputs=file_upload, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])