youngtsai commited on
Commit
9cfc16e
·
1 Parent(s): 792abb3

AI card visible false

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -1092,23 +1092,27 @@ def generate_questions(df_string):
1092
 
1093
  def get_questions_answers(video_id, df_string, source="gcs"):
1094
  if source == "gcs":
1095
- print("===get_questions_answers on gcs===")
1096
- gcs_client = GCS_CLIENT
1097
- bucket_name = 'video_ai_assistant'
1098
- file_name = f'{video_id}_questions_answers.json'
1099
- blob_name = f"{video_id}/{file_name}"
1100
- # 检查檔案是否存在
1101
- is_questions_answers_exists = GCS_SERVICE.check_file_exists(bucket_name, blob_name)
1102
- if not is_questions_answers_exists:
1103
- questions_answers = generate_questions_answers(df_string)
1104
- questions_answers_text = json.dumps(questions_answers, ensure_ascii=False, indent=2)
1105
- upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, questions_answers_text)
1106
- print("questions_answers已上傳到GCS")
1107
- else:
1108
- # questions_answers已存在,下载内容
1109
- print("questions_answers已存在于GCS中")
1110
- questions_answers_text = download_blob_to_string(gcs_client, bucket_name, blob_name)
1111
- questions_answers = json.loads(questions_answers_text)
 
 
 
 
1112
 
1113
  return questions_answers
1114
 
@@ -1118,11 +1122,6 @@ def generate_questions_answers(df_string):
1118
  df_string_json = json.loads(df_string)
1119
  else:
1120
  df_string_json = df_string
1121
-
1122
- content_text = json.dumps(df_string_json, ensure_ascii=False, indent=2)
1123
- print("=====content_text=====")
1124
- print(content_text)
1125
- print("=====content_text=====")
1126
 
1127
  # JSON FORMAT: [{"question": "問題", "answer": "答案"}, ...]
1128
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,並用既有資料為本質猜測用戶可能會問的問題,使用 zh-TW"
@@ -1962,7 +1961,7 @@ def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_mo
1962
  for qa in questions_answers_json:
1963
  question = qa["question"]
1964
  answer = qa["answer"]
1965
- if user_message == question:
1966
  print("=== in questions_answers_json==")
1967
  print(f"question: {question}")
1968
  print(f"answer: {answer}")
@@ -2061,7 +2060,7 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
2061
  for qa in questions_answers_json:
2062
  question = qa["question"]
2063
  answer = qa["answer"]
2064
- if user_message == question:
2065
  print("=== in questions_answers_json==")
2066
  print(f"question: {question}")
2067
  print(f"answer: {answer}")
@@ -2539,7 +2538,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
2539
  with gr.Tab("AI小精靈"):
2540
  with gr.Accordion("選擇 AI 小精靈", open=True) as chatbot_select_accordion:
2541
  with gr.Row():
2542
- with gr.Column(scale=1, variant="panel"):
2543
  chatbot_avatar_url = "https://junyitopicimg.s3.amazonaws.com/s4byy--icon.jpe?v=20200513013523726"
2544
  chatbot_description = """Hi,我是你的AI學伴【飛特精靈】,\n
2545
  我可以陪你一起學習本次的內容,有什麼問題都可以問我喔!\n
@@ -2553,7 +2552,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
2553
  gr.Image(value=chatbot_avatar_url, height=100, width=100, show_label=False, show_download_button=False)
2554
  chatbot_open_ai_select_btn = gr.Button("👆選擇【飛特精靈】", elem_id="chatbot_btn", visible=True, variant="primary")
2555
  gr.Markdown(value=chatbot_description, visible=True)
2556
- with gr.Column(scale=1, variant="panel"):
2557
  streaming_chatbot_avatar_url = "https://storage.googleapis.com/wpassets.junyiacademy.org/1/2020/11/1-%E6%98%9F%E7%A9%BA%E9%A0%AD%E8%B2%BC-%E5%A4%AA%E7%A9%BA%E7%8B%90%E7%8B%B8%E8%B2%93-150x150.png"
2558
  streaming_chatbot_description = """Hi,我是【飛特音速】, \n
2559
  說話比較快,但有什麼問題都可以問我喔! \n
@@ -2585,7 +2584,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
2585
  bot_avatar = "https://junyitopicimg.s3.amazonaws.com/s4byy--icon.jpe?v=20200513013523726"
2586
  latex_delimiters = [{"left": "$", "right": "$", "display": False}]
2587
  chatbot_greeting = [[
2588
- None,
2589
  """Hi,我是你的AI學伴【飛特精靈】,我可以陪你一起學習本次的內容,有什麼問題都可以問我喔!
2590
  🤔 如果你不知道怎麼發問,可以點擊左下方的問題一、問題二、問題三,我會幫你生成問題!
2591
  🗣️ 也可以點擊右下方用語音輸入,我會幫你轉換成文字,厲害吧!
@@ -2632,7 +2631,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
2632
  with gr.Row("其他精靈") as chatbot_jutor:
2633
  with gr.Column():
2634
  ai_chatbot_greeting = [[
2635
- None,
2636
  """Hi,我是飛特精靈的朋友們【梨梨、麥麥、狐狸貓】,也可以陪你一起學習本次的內容,有什麼問題都可以問我喔!
2637
  🤔 如果你不知道怎麼發問,可以點擊左下方的問題一、問題二、問題三,我會幫你生成問題!
2638
  🗣️ 也可以點擊右下方用語音輸入,我會幫你轉換成文字,厲害吧!
@@ -2641,7 +2640,12 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
2641
  """,
2642
  ]]
2643
  ai_chatbot_bot_avatar = "https://storage.googleapis.com/wpassets.junyiacademy.org/1/2019/11/%E5%9B%9B%E6%A0%BC%E6%95%85%E4%BA%8B-04.jpg"
2644
- ai_name = gr.Dropdown(label="選擇 AI 助理", choices=[("梨梨","jutor"), ("麥麥","claude3"), ("狐狸貓","groq")], value="jutor")
 
 
 
 
 
2645
  ai_chatbot = gr.Chatbot(avatar_images=[user_avatar, ai_chatbot_bot_avatar], label="ai_chatbot", show_share_button=False, likeable=True, show_label=False, latex_delimiters=latex_delimiters, value=ai_chatbot_greeting)
2646
  ai_chatbot_socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=True, visible=False)
2647
  with gr.Row():
 
1092
 
1093
  def get_questions_answers(video_id, df_string, source="gcs"):
1094
  if source == "gcs":
1095
+ try:
1096
+ print("===get_questions_answers on gcs===")
1097
+ gcs_client = GCS_CLIENT
1098
+ bucket_name = 'video_ai_assistant'
1099
+ file_name = f'{video_id}_questions_answers.json'
1100
+ blob_name = f"{video_id}/{file_name}"
1101
+ # 检查檔案是否存在
1102
+ is_questions_answers_exists = GCS_SERVICE.check_file_exists(bucket_name, blob_name)
1103
+ if not is_questions_answers_exists:
1104
+ questions_answers = generate_questions_answers(df_string)
1105
+ questions_answers_text = json.dumps(questions_answers, ensure_ascii=False, indent=2)
1106
+ upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, questions_answers_text)
1107
+ print("questions_answers已上傳到GCS")
1108
+ else:
1109
+ # questions_answers已存在,下载内容
1110
+ print("questions_answers已存在于GCS中")
1111
+ questions_answers_text = download_blob_to_string(gcs_client, bucket_name, blob_name)
1112
+ questions_answers = json.loads(questions_answers_text)
1113
+ except:
1114
+ questions = get_questions(video_id, df_string, source)
1115
+ questions_answers = [{"question": q, "answer": ""} for q in questions]
1116
 
1117
  return questions_answers
1118
 
 
1122
  df_string_json = json.loads(df_string)
1123
  else:
1124
  df_string_json = df_string
 
 
 
 
 
1125
 
1126
  # JSON FORMAT: [{"question": "問題", "answer": "答案"}, ...]
1127
  sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,並用既有資料為本質猜測用戶可能會問的問題,使用 zh-TW"
 
1961
  for qa in questions_answers_json:
1962
  question = qa["question"]
1963
  answer = qa["answer"]
1964
+ if user_message == question and answer != "":
1965
  print("=== in questions_answers_json==")
1966
  print(f"question: {question}")
1967
  print(f"answer: {answer}")
 
2060
  for qa in questions_answers_json:
2061
  question = qa["question"]
2062
  answer = qa["answer"]
2063
+ if user_message == question and answer != "":
2064
  print("=== in questions_answers_json==")
2065
  print(f"question: {question}")
2066
  print(f"answer: {answer}")
 
2538
  with gr.Tab("AI小精靈"):
2539
  with gr.Accordion("選擇 AI 小精靈", open=True) as chatbot_select_accordion:
2540
  with gr.Row():
2541
+ with gr.Column(scale=1, variant="panel", visible=False):
2542
  chatbot_avatar_url = "https://junyitopicimg.s3.amazonaws.com/s4byy--icon.jpe?v=20200513013523726"
2543
  chatbot_description = """Hi,我是你的AI學伴【飛特精靈】,\n
2544
  我可以陪你一起學習本次的內容,有什麼問題都可以問我喔!\n
 
2552
  gr.Image(value=chatbot_avatar_url, height=100, width=100, show_label=False, show_download_button=False)
2553
  chatbot_open_ai_select_btn = gr.Button("👆選擇【飛特精靈】", elem_id="chatbot_btn", visible=True, variant="primary")
2554
  gr.Markdown(value=chatbot_description, visible=True)
2555
+ with gr.Column(scale=1, variant="panel", visible=False):
2556
  streaming_chatbot_avatar_url = "https://storage.googleapis.com/wpassets.junyiacademy.org/1/2020/11/1-%E6%98%9F%E7%A9%BA%E9%A0%AD%E8%B2%BC-%E5%A4%AA%E7%A9%BA%E7%8B%90%E7%8B%B8%E8%B2%93-150x150.png"
2557
  streaming_chatbot_description = """Hi,我是【飛特音速】, \n
2558
  說話比較快,但有什麼問題都可以問我喔! \n
 
2584
  bot_avatar = "https://junyitopicimg.s3.amazonaws.com/s4byy--icon.jpe?v=20200513013523726"
2585
  latex_delimiters = [{"left": "$", "right": "$", "display": False}]
2586
  chatbot_greeting = [[
2587
+ "請問你是誰?",
2588
  """Hi,我是你的AI學伴【飛特精靈】,我可以陪你一起學習本次的內容,有什麼問題都可以問我喔!
2589
  🤔 如果你不知道怎麼發問,可以點擊左下方的問題一、問題二、問題三,我會幫你生成問題!
2590
  🗣️ 也可以點擊右下方用語音輸入,我會幫你轉換成文字,厲害吧!
 
2631
  with gr.Row("其他精靈") as chatbot_jutor:
2632
  with gr.Column():
2633
  ai_chatbot_greeting = [[
2634
+ "請問你是誰?",
2635
  """Hi,我是飛特精靈的朋友們【梨梨、麥麥、狐狸貓】,也可以陪你一起學習本次的內容,有什麼問題都可以問我喔!
2636
  🤔 如果你不知道怎麼發問,可以點擊左下方的問題一、問題二、問題三,我會幫你生成問題!
2637
  🗣️ 也可以點擊右下方用語音輸入,我會幫你轉換成文字,厲害吧!
 
2640
  """,
2641
  ]]
2642
  ai_chatbot_bot_avatar = "https://storage.googleapis.com/wpassets.junyiacademy.org/1/2019/11/%E5%9B%9B%E6%A0%BC%E6%95%85%E4%BA%8B-04.jpg"
2643
+ ai_name = gr.Dropdown(label="選擇 AI 助理", choices=[
2644
+ # ("梨梨","jutor"),
2645
+ ("麥麥","claude3"),
2646
+ ("狐狸貓","groq")],
2647
+ value="claude3"
2648
+ )
2649
  ai_chatbot = gr.Chatbot(avatar_images=[user_avatar, ai_chatbot_bot_avatar], label="ai_chatbot", show_share_button=False, likeable=True, show_label=False, latex_delimiters=latex_delimiters, value=ai_chatbot_greeting)
2650
  ai_chatbot_socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=True, visible=False)
2651
  with gr.Row():