Spaces:
Running
Running
for qa in questions_answers_json:
Browse files
app.py
CHANGED
@@ -1099,8 +1099,8 @@ def get_questions_answers(video_id, df_string, source="gcs"):
|
|
1099 |
blob_name = f"{video_id}/{file_name}"
|
1100 |
# 检查檔案是否存在
|
1101 |
is_questions_answers_exists = GCS_SERVICE.check_file_exists(bucket_name, blob_name)
|
1102 |
-
|
1103 |
-
if True:
|
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)
|
@@ -1953,12 +1953,32 @@ def get_instructions(content_subject, content_grade, key_moments):
|
|
1953 |
"""
|
1954 |
return instructions
|
1955 |
|
1956 |
-
def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
|
1957 |
verify_password(password)
|
1958 |
|
1959 |
print("=====user_data=====")
|
1960 |
print(f"user_data: {user_data}")
|
1961 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1962 |
if chat_history is not None and len(chat_history) > 11:
|
1963 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
1964 |
raise gr.Error(error_msg)
|
@@ -2027,7 +2047,7 @@ def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_mo
|
|
2027 |
print(f"Error: {e}")
|
2028 |
return "请求失败,请稍后再试!", chat_history
|
2029 |
|
2030 |
-
def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
|
2031 |
verify_password(password)
|
2032 |
|
2033 |
print("=====user_data=====")
|
@@ -2038,6 +2058,28 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2038 |
error_msg = "你的訊息太長了,請縮短訊息長度至五百字以內"
|
2039 |
raise gr.Error(error_msg)
|
2040 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2041 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
2042 |
if chat_history is not None and len(chat_history) > 10:
|
2043 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
@@ -2820,7 +2862,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2820 |
# OPENAI ASSISTANT CHATBOT 模式
|
2821 |
send_button.click(
|
2822 |
chat_with_opan_ai_assistant,
|
2823 |
-
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, socratic_mode_btn],
|
2824 |
outputs=[msg, chatbot, thread_id],
|
2825 |
scroll_to_output=True
|
2826 |
)
|
@@ -2830,9 +2872,9 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2830 |
outputs=[msg]
|
2831 |
)
|
2832 |
# OPENAI ASSISTANT CHATBOT 連接按鈕點擊事件
|
2833 |
-
btn_1_chat_with_opan_ai_assistant_input =[password, video_id, user_data, thread_id, trascript_state, key_moments, btn_1, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2834 |
-
btn_2_chat_with_opan_ai_assistant_input =[password, video_id, user_data, thread_id, trascript_state, key_moments, btn_2, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2835 |
-
btn_3_chat_with_opan_ai_assistant_input =[password, video_id, user_data, thread_id, trascript_state, key_moments, btn_3, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2836 |
btn_1.click(
|
2837 |
chat_with_opan_ai_assistant,
|
2838 |
inputs=btn_1_chat_with_opan_ai_assistant_input,
|
@@ -2860,14 +2902,14 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2860 |
# 其他精靈 ai_chatbot 模式
|
2861 |
ai_send_button.click(
|
2862 |
chat_with_ai,
|
2863 |
-
inputs=[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_msg, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn],
|
2864 |
outputs=[ai_msg, ai_chatbot],
|
2865 |
scroll_to_output=True
|
2866 |
)
|
2867 |
# 其他精靈 ai_chatbot 连接按钮点击事件
|
2868 |
-
ai_chatbot_question_1_chat_with_ai_input =[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_chatbot_question_1, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2869 |
-
ai_chatbot_question_2_chat_with_ai_input =[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_chatbot_question_2, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2870 |
-
ai_chatbot_question_3_chat_with_ai_input =[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_chatbot_question_3, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2871 |
ai_chatbot_question_1.click(
|
2872 |
chat_with_ai,
|
2873 |
inputs=ai_chatbot_question_1_chat_with_ai_input,
|
|
|
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 |
+
# if True:
|
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)
|
|
|
1953 |
"""
|
1954 |
return instructions
|
1955 |
|
1956 |
+
def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, questions_answers_json, socratic_mode=False):
|
1957 |
verify_password(password)
|
1958 |
|
1959 |
print("=====user_data=====")
|
1960 |
print(f"user_data: {user_data}")
|
1961 |
|
1962 |
+
questions_answers_json = json.loads(questions_answers_json)
|
1963 |
+
for qa in questions_answers_json:
|
1964 |
+
question = qa["question"]
|
1965 |
+
answer = qa["answer"]
|
1966 |
+
if user_message == question:
|
1967 |
+
print("=== in questions_answers_json==")
|
1968 |
+
print(f"question: {question}")
|
1969 |
+
print(f"answer: {answer}")
|
1970 |
+
# 更新聊天历史
|
1971 |
+
new_chat_history = (user_message, answer)
|
1972 |
+
if chat_history is None:
|
1973 |
+
chat_history = [new_chat_history]
|
1974 |
+
else:
|
1975 |
+
chat_history.append(new_chat_history)
|
1976 |
+
|
1977 |
+
# 等待 3 秒
|
1978 |
+
time.sleep(3)
|
1979 |
+
|
1980 |
+
return "", chat_history
|
1981 |
+
|
1982 |
if chat_history is not None and len(chat_history) > 11:
|
1983 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
1984 |
raise gr.Error(error_msg)
|
|
|
2047 |
print(f"Error: {e}")
|
2048 |
return "请求失败,请稍后再试!", chat_history
|
2049 |
|
2050 |
+
def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, questions_answers_json, socratic_mode=False):
|
2051 |
verify_password(password)
|
2052 |
|
2053 |
print("=====user_data=====")
|
|
|
2058 |
error_msg = "你的訊息太長了,請縮短訊息長度至五百字以內"
|
2059 |
raise gr.Error(error_msg)
|
2060 |
|
2061 |
+
questions_answers_json = json.loads(questions_answers_json)
|
2062 |
+
for qa in questions_answers_json:
|
2063 |
+
question = qa["question"]
|
2064 |
+
answer = qa["answer"]
|
2065 |
+
if user_message == question:
|
2066 |
+
print("=== in questions_answers_json==")
|
2067 |
+
print(f"question: {question}")
|
2068 |
+
print(f"answer: {answer}")
|
2069 |
+
print(f"thread_id: {thread_id}")
|
2070 |
+
# 更新聊天历史
|
2071 |
+
new_chat_history = (user_message, answer)
|
2072 |
+
if chat_history is None:
|
2073 |
+
chat_history = [new_chat_history]
|
2074 |
+
else:
|
2075 |
+
chat_history.append(new_chat_history)
|
2076 |
+
|
2077 |
+
# 等待 3 秒
|
2078 |
+
time.sleep(3)
|
2079 |
+
|
2080 |
+
return "", chat_history, thread_id
|
2081 |
+
|
2082 |
+
|
2083 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
2084 |
if chat_history is not None and len(chat_history) > 10:
|
2085 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
|
|
2862 |
# OPENAI ASSISTANT CHATBOT 模式
|
2863 |
send_button.click(
|
2864 |
chat_with_opan_ai_assistant,
|
2865 |
+
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, questions_answers_json, socratic_mode_btn],
|
2866 |
outputs=[msg, chatbot, thread_id],
|
2867 |
scroll_to_output=True
|
2868 |
)
|
|
|
2872 |
outputs=[msg]
|
2873 |
)
|
2874 |
# OPENAI ASSISTANT CHATBOT 連接按鈕點擊事件
|
2875 |
+
btn_1_chat_with_opan_ai_assistant_input =[password, video_id, user_data, thread_id, trascript_state, key_moments, btn_1, chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
2876 |
+
btn_2_chat_with_opan_ai_assistant_input =[password, video_id, user_data, thread_id, trascript_state, key_moments, btn_2, chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
2877 |
+
btn_3_chat_with_opan_ai_assistant_input =[password, video_id, user_data, thread_id, trascript_state, key_moments, btn_3, chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
2878 |
btn_1.click(
|
2879 |
chat_with_opan_ai_assistant,
|
2880 |
inputs=btn_1_chat_with_opan_ai_assistant_input,
|
|
|
2902 |
# 其他精靈 ai_chatbot 模式
|
2903 |
ai_send_button.click(
|
2904 |
chat_with_ai,
|
2905 |
+
inputs=[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_msg, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn],
|
2906 |
outputs=[ai_msg, ai_chatbot],
|
2907 |
scroll_to_output=True
|
2908 |
)
|
2909 |
# 其他精靈 ai_chatbot 连接按钮点击事件
|
2910 |
+
ai_chatbot_question_1_chat_with_ai_input =[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_chatbot_question_1, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
2911 |
+
ai_chatbot_question_2_chat_with_ai_input =[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_chatbot_question_2, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
2912 |
+
ai_chatbot_question_3_chat_with_ai_input =[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_chatbot_question_3, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
2913 |
ai_chatbot_question_1.click(
|
2914 |
chat_with_ai,
|
2915 |
inputs=ai_chatbot_question_1_chat_with_ai_input,
|