Spaces:
Sleeping
Sleeping
def prepare_system_prompt(self, socratic_mode):
Browse files- app.py +22 -7
- chatbot.py +4 -3
app.py
CHANGED
@@ -1656,7 +1656,7 @@ def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user
|
|
1656 |
print(f"Error: {e}")
|
1657 |
return "请求失败,请稍后再试!", chat_history
|
1658 |
|
1659 |
-
def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
|
1660 |
verify_password(password)
|
1661 |
|
1662 |
# 先計算 user_message 是否超過 500 個字
|
@@ -1678,12 +1678,27 @@ def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript, user
|
|
1678 |
for entry in trascript_json:
|
1679 |
entry.pop('embed_url', None)
|
1680 |
entry.pop('screenshot_path', None)
|
1681 |
-
trascript_text = json.dumps(trascript_json, ensure_ascii=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1682 |
|
1683 |
instructions = f"""
|
1684 |
科目:{content_subject}
|
1685 |
年級:{content_grade}
|
1686 |
-
逐字稿資料:{
|
1687 |
-------------------------------------
|
1688 |
你是一個專業的{content_subject}老師, user 為{content_grade}的學生
|
1689 |
socratic_mode = {socratic_mode}
|
@@ -2280,7 +2295,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2280 |
# OPENAI ASSISTANT CHATBOT 模式
|
2281 |
send_button.click(
|
2282 |
chat_with_opan_ai_assistant,
|
2283 |
-
inputs=[password, video_id, thread_id, df_string_output, msg, chatbot, content_subject, content_grade, socratic_mode_btn],
|
2284 |
outputs=[msg, chatbot, thread_id]
|
2285 |
)
|
2286 |
openai_chatbot_audio_input.change(
|
@@ -2289,9 +2304,9 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2289 |
outputs=[msg]
|
2290 |
)
|
2291 |
# OPENAI ASSISTANT CHATBOT 連接按鈕點擊事件
|
2292 |
-
btn_1_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, df_string_output, btn_1, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2293 |
-
btn_2_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, df_string_output, btn_2, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2294 |
-
btn_3_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, df_string_output, btn_3, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2295 |
btn_1.click(
|
2296 |
chat_with_opan_ai_assistant,
|
2297 |
inputs=btn_1_chat_with_opan_ai_assistant_input,
|
|
|
1656 |
print(f"Error: {e}")
|
1657 |
return "请求失败,请稍后再试!", chat_history
|
1658 |
|
1659 |
+
def chat_with_opan_ai_assistant(password, youtube_id, thread_id, trascript, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
|
1660 |
verify_password(password)
|
1661 |
|
1662 |
# 先計算 user_message 是否超過 500 個字
|
|
|
1678 |
for entry in trascript_json:
|
1679 |
entry.pop('embed_url', None)
|
1680 |
entry.pop('screenshot_path', None)
|
1681 |
+
trascript_text = json.dumps(trascript_json, ensure_ascii=False)
|
1682 |
+
|
1683 |
+
if len(trascript_text) > 25000:
|
1684 |
+
if isinstance(key_moments, str):
|
1685 |
+
key_moments_json = json.loads(key_moments)
|
1686 |
+
else:
|
1687 |
+
key_moments_json = key_moments
|
1688 |
+
|
1689 |
+
# key_moments_json remove images
|
1690 |
+
for moment in key_moments_json:
|
1691 |
+
moment.pop('images', None)
|
1692 |
+
content_text = json.dumps(key_moments_json, ensure_ascii=False)
|
1693 |
+
print("=== transcript_text is too long, replace by key_moments_text ===")
|
1694 |
+
else:
|
1695 |
+
content_text = trascript_text
|
1696 |
+
print("=== transcript_text is used ===")
|
1697 |
|
1698 |
instructions = f"""
|
1699 |
科目:{content_subject}
|
1700 |
年級:{content_grade}
|
1701 |
+
逐字稿資料:{content_text}
|
1702 |
-------------------------------------
|
1703 |
你是一個專業的{content_subject}老師, user 為{content_grade}的學生
|
1704 |
socratic_mode = {socratic_mode}
|
|
|
2295 |
# OPENAI ASSISTANT CHATBOT 模式
|
2296 |
send_button.click(
|
2297 |
chat_with_opan_ai_assistant,
|
2298 |
+
inputs=[password, video_id, thread_id, df_string_output, key_moments, msg, chatbot, content_subject, content_grade, socratic_mode_btn],
|
2299 |
outputs=[msg, chatbot, thread_id]
|
2300 |
)
|
2301 |
openai_chatbot_audio_input.change(
|
|
|
2304 |
outputs=[msg]
|
2305 |
)
|
2306 |
# OPENAI ASSISTANT CHATBOT 連接按鈕點擊事件
|
2307 |
+
btn_1_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, df_string_output, key_moments, btn_1, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2308 |
+
btn_2_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, df_string_output, key_moments, btn_2, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2309 |
+
btn_3_chat_with_opan_ai_assistant_input =[password, video_id, thread_id, df_string_output, key_moments, btn_3, chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
|
2310 |
btn_1.click(
|
2311 |
chat_with_opan_ai_assistant,
|
2312 |
inputs=btn_1_chat_with_opan_ai_assistant_input,
|
chatbot.py
CHANGED
@@ -26,6 +26,10 @@ class Chatbot:
|
|
26 |
key_moments_json = json.loads(key_moments_data)
|
27 |
else:
|
28 |
key_moments_json = key_moments_data
|
|
|
|
|
|
|
|
|
29 |
key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
|
30 |
return key_moments_text
|
31 |
|
@@ -86,9 +90,6 @@ class Chatbot:
|
|
86 |
- 回答時數學式請用數學符號代替文字(Latex 用 $ 字號 render)
|
87 |
"""
|
88 |
|
89 |
-
print("====================================")
|
90 |
-
print(system_prompt)
|
91 |
-
|
92 |
return system_prompt
|
93 |
|
94 |
def prepare_messages(self, chat_history, user_message):
|
|
|
26 |
key_moments_json = json.loads(key_moments_data)
|
27 |
else:
|
28 |
key_moments_json = key_moments_data
|
29 |
+
# key_moments_json remove images
|
30 |
+
for moment in key_moments_json:
|
31 |
+
moment.pop('images', None)
|
32 |
+
|
33 |
key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
|
34 |
return key_moments_text
|
35 |
|
|
|
90 |
- 回答時數學式請用數學符號代替文字(Latex 用 $ 字號 render)
|
91 |
"""
|
92 |
|
|
|
|
|
|
|
93 |
return system_prompt
|
94 |
|
95 |
def prepare_messages(self, chat_history, user_message):
|