Spaces:
Sleeping
Sleeping
CHAT_LIMIT = 10
Browse files
app.py
CHANGED
@@ -83,6 +83,7 @@ else:
|
|
83 |
|
84 |
TRANSCRIPTS = []
|
85 |
CURRENT_INDEX = 0
|
|
|
86 |
|
87 |
# CLIENTS CONFIG
|
88 |
GROQ_CLIENT = Groq(api_key=GROQ_API_KEY)
|
@@ -2085,7 +2086,7 @@ def get_instructions(content_subject, content_grade, key_moments):
|
|
2085 |
"""
|
2086 |
return instructions
|
2087 |
|
2088 |
-
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):
|
2089 |
verify_password(password)
|
2090 |
|
2091 |
print("=====user_data=====")
|
@@ -2108,10 +2109,17 @@ def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_mo
|
|
2108 |
|
2109 |
# 等待 3 秒
|
2110 |
time.sleep(3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2111 |
|
2112 |
-
return "", chat_history
|
2113 |
|
2114 |
-
if chat_history is not None and len(chat_history) >
|
2115 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
2116 |
raise gr.Error(error_msg)
|
2117 |
|
@@ -2197,14 +2205,22 @@ def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_mo
|
|
2197 |
else:
|
2198 |
chat_history.append(new_chat_history)
|
2199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2200 |
# 返回聊天历史和空字符串清空输入框
|
2201 |
-
return "", chat_history
|
2202 |
except Exception as e:
|
2203 |
# 处理错误情况
|
2204 |
print(f"Error: {e}")
|
2205 |
return "请求失败,请稍后再试!", chat_history
|
2206 |
|
2207 |
-
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):
|
2208 |
verify_password(password)
|
2209 |
|
2210 |
print("=====user_data=====")
|
@@ -2233,12 +2249,19 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2233 |
|
2234 |
# 等待 3 秒
|
2235 |
time.sleep(3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2236 |
|
2237 |
-
return "", chat_history, thread_id
|
2238 |
|
2239 |
|
2240 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
2241 |
-
if chat_history is not None and len(chat_history) >
|
2242 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
2243 |
raise gr.Error(error_msg)
|
2244 |
|
@@ -2329,8 +2352,17 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
2329 |
print(f"Error: {e}")
|
2330 |
raise gr.Error(f"Error: {e}")
|
2331 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2332 |
# 返回聊天历史和空字符串清空输入框
|
2333 |
-
return "", chat_history, thread.id
|
2334 |
|
2335 |
def process_open_ai_audio_to_chatbot(password, audio_url):
|
2336 |
verify_password(password)
|
@@ -2418,8 +2450,8 @@ def chat_with_opan_ai_assistant_streaming(user_message, chat_history, password,
|
|
2418 |
raise gr.Error(error_msg)
|
2419 |
|
2420 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
2421 |
-
if chat_history is not None and len(chat_history) >
|
2422 |
-
error_msg = "此次對話超過上限(對話一輪
|
2423 |
raise gr.Error(error_msg)
|
2424 |
|
2425 |
try:
|
@@ -3076,8 +3108,8 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3076 |
# OPENAI ASSISTANT CHATBOT 模式
|
3077 |
send_button.click(
|
3078 |
chat_with_opan_ai_assistant,
|
3079 |
-
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, questions_answers_json, socratic_mode_btn],
|
3080 |
-
outputs=[msg, chatbot, thread_id],
|
3081 |
scroll_to_output=True
|
3082 |
)
|
3083 |
openai_chatbot_audio_input.change(
|
@@ -3095,8 +3127,8 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3095 |
)
|
3096 |
question_buttons = [btn_1, btn_2, btn_3]
|
3097 |
for question_btn in question_buttons:
|
3098 |
-
inputs_list = [password, video_id, user_data, thread_id, trascript_state, key_moments, question_btn, chatbot, content_subject, content_grade, questions_answers_json,
|
3099 |
-
outputs_list = [msg, chatbot, thread_id]
|
3100 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_opan_ai_assistant)
|
3101 |
|
3102 |
# 為生成問題按鈕設定特殊的點擊事件
|
@@ -3109,15 +3141,15 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3109 |
# 其他精靈 ai_chatbot 模式
|
3110 |
ai_send_button.click(
|
3111 |
chat_with_ai,
|
3112 |
-
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],
|
3113 |
-
outputs=[ai_msg, ai_chatbot],
|
3114 |
scroll_to_output=True
|
3115 |
)
|
3116 |
# 其他精靈 ai_chatbot 连接按钮点击事件
|
3117 |
ai_chatbot_buttons = [ai_chatbot_question_1, ai_chatbot_question_2, ai_chatbot_question_3]
|
3118 |
for ai_question_btn in ai_chatbot_buttons:
|
3119 |
-
inputs_list = [ai_name, password, video_id, user_data, trascript_state, key_moments, ai_question_btn, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
3120 |
-
outputs_list = [ai_msg, ai_chatbot]
|
3121 |
setup_question_button_click(ai_question_btn, inputs_list, outputs_list, chat_with_ai)
|
3122 |
|
3123 |
# file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
|
|
|
83 |
|
84 |
TRANSCRIPTS = []
|
85 |
CURRENT_INDEX = 0
|
86 |
+
CHAT_LIMIT = 10
|
87 |
|
88 |
# CLIENTS CONFIG
|
89 |
GROQ_CLIENT = Groq(api_key=GROQ_API_KEY)
|
|
|
2086 |
"""
|
2087 |
return instructions
|
2088 |
|
2089 |
+
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, send_btn, socratic_mode=False):
|
2090 |
verify_password(password)
|
2091 |
|
2092 |
print("=====user_data=====")
|
|
|
2109 |
|
2110 |
# 等待 3 秒
|
2111 |
time.sleep(3)
|
2112 |
+
send_count = len(chat_history)-1
|
2113 |
+
if len(chat_history) > CHAT_LIMIT:
|
2114 |
+
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2115 |
+
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2116 |
+
else:
|
2117 |
+
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2118 |
+
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2119 |
|
2120 |
+
return "", chat_history, send_btn
|
2121 |
|
2122 |
+
if chat_history is not None and len(chat_history) > CHAT_LIMIT:
|
2123 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
2124 |
raise gr.Error(error_msg)
|
2125 |
|
|
|
2205 |
else:
|
2206 |
chat_history.append(new_chat_history)
|
2207 |
|
2208 |
+
send_count = len(chat_history)-1
|
2209 |
+
if len(chat_history) > CHAT_LIMIT:
|
2210 |
+
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2211 |
+
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2212 |
+
else:
|
2213 |
+
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2214 |
+
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2215 |
+
|
2216 |
# 返回聊天历史和空字符串清空输入框
|
2217 |
+
return "", chat_history, send_btn
|
2218 |
except Exception as e:
|
2219 |
# 处理错误情况
|
2220 |
print(f"Error: {e}")
|
2221 |
return "请求失败,请稍后再试!", chat_history
|
2222 |
|
2223 |
+
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, send_btn, socratic_mode=False):
|
2224 |
verify_password(password)
|
2225 |
|
2226 |
print("=====user_data=====")
|
|
|
2249 |
|
2250 |
# 等待 3 秒
|
2251 |
time.sleep(3)
|
2252 |
+
send_count = len(chat_history)-1
|
2253 |
+
if len(chat_history) > CHAT_LIMIT:
|
2254 |
+
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2255 |
+
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2256 |
+
else:
|
2257 |
+
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2258 |
+
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2259 |
|
2260 |
+
return "", chat_history, thread_id, send_btn
|
2261 |
|
2262 |
|
2263 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
2264 |
+
if chat_history is not None and len(chat_history) > CHAT_LIMIT:
|
2265 |
error_msg = "此次對話超過上限(對話一輪10次)"
|
2266 |
raise gr.Error(error_msg)
|
2267 |
|
|
|
2352 |
print(f"Error: {e}")
|
2353 |
raise gr.Error(f"Error: {e}")
|
2354 |
|
2355 |
+
send_count = len(chat_history)-1
|
2356 |
+
if len(chat_history) > CHAT_LIMIT:
|
2357 |
+
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2358 |
+
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2359 |
+
else:
|
2360 |
+
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2361 |
+
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2362 |
+
|
2363 |
+
|
2364 |
# 返回聊天历史和空字符串清空输入框
|
2365 |
+
return "", chat_history, thread.id, send_btn
|
2366 |
|
2367 |
def process_open_ai_audio_to_chatbot(password, audio_url):
|
2368 |
verify_password(password)
|
|
|
2450 |
raise gr.Error(error_msg)
|
2451 |
|
2452 |
# 如果 chat_history 超過 10 則訊息,直接 return "對話超過上限"
|
2453 |
+
if chat_history is not None and len(chat_history) > CHAT_LIMIT:
|
2454 |
+
error_msg = f"此次對話超過上限(對話一輪{CHAT_LIMIT}次)"
|
2455 |
raise gr.Error(error_msg)
|
2456 |
|
2457 |
try:
|
|
|
3108 |
# OPENAI ASSISTANT CHATBOT 模式
|
3109 |
send_button.click(
|
3110 |
chat_with_opan_ai_assistant,
|
3111 |
+
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, questions_answers_json, send_button, socratic_mode_btn],
|
3112 |
+
outputs=[msg, chatbot, thread_id, send_button],
|
3113 |
scroll_to_output=True
|
3114 |
)
|
3115 |
openai_chatbot_audio_input.change(
|
|
|
3127 |
)
|
3128 |
question_buttons = [btn_1, btn_2, btn_3]
|
3129 |
for question_btn in question_buttons:
|
3130 |
+
inputs_list = [password, video_id, user_data, thread_id, trascript_state, key_moments, question_btn, chatbot, content_subject, content_grade, questions_answers_json, send_button, socratic_mode_btn]
|
3131 |
+
outputs_list = [msg, chatbot, thread_id, send_button]
|
3132 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_opan_ai_assistant)
|
3133 |
|
3134 |
# 為生成問題按鈕設定特殊的點擊事件
|
|
|
3141 |
# 其他精靈 ai_chatbot 模式
|
3142 |
ai_send_button.click(
|
3143 |
chat_with_ai,
|
3144 |
+
inputs=[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_msg, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_send_button, ai_chatbot_socratic_mode_btn],
|
3145 |
+
outputs=[ai_msg, ai_chatbot, ai_send_button],
|
3146 |
scroll_to_output=True
|
3147 |
)
|
3148 |
# 其他精靈 ai_chatbot 连接按钮点击事件
|
3149 |
ai_chatbot_buttons = [ai_chatbot_question_1, ai_chatbot_question_2, ai_chatbot_question_3]
|
3150 |
for ai_question_btn in ai_chatbot_buttons:
|
3151 |
+
inputs_list = [ai_name, password, video_id, user_data, trascript_state, key_moments, ai_question_btn, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_send_button, ai_chatbot_socratic_mode_btn]
|
3152 |
+
outputs_list = [ai_msg, ai_chatbot, ai_send_button]
|
3153 |
setup_question_button_click(ai_question_btn, inputs_list, outputs_list, chat_with_ai)
|
3154 |
|
3155 |
# file_upload.change(process_file, inputs=file_upload, outputs=df_string_output)
|