youngtsai commited on
Commit
ba6d23b
·
1 Parent(s): 5cc5b35
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -1638,12 +1638,14 @@ def poll_run_status(run_id, thread_id, timeout=600, poll_interval=5):
1638
 
1639
  return run.status
1640
 
1641
- def chat_with_claude3(password, user_message, data, chat_history, content_subject, content_grade, socratic_mode=False):
1642
  verify_password(password)
1643
- data_json = json.loads(data)
1644
  for entry in data_json:
1645
  entry.pop('embed_url', None) # Remove 'embed_url' if it exists
1646
  entry.pop('screenshot_path', None)
 
 
1647
 
1648
  sys_content = f"""
1649
  科目:{content_subject}
@@ -1657,11 +1659,11 @@ def chat_with_claude3(password, user_message, data, chat_history, content_subjec
1657
  - 一次只問一個問題,字數在100字以內
1658
  - 不要直接給予答案,讓學生自己思考
1659
  - 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
1660
- - 在你回答的開頭標註【蘇格拉底助教:{youtube_id} 】
1661
 
1662
  if socratic_mode is False,
1663
  - 直接回答學生問題,字數在100字以內
1664
- - 在你回答的開頭標註【一般學習精靈:{youtube_id} 】
1665
 
1666
  rule:
1667
  - 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
@@ -1678,10 +1680,6 @@ def chat_with_claude3(password, user_message, data, chat_history, content_subjec
1678
 
1679
 
1680
  messages = []
1681
-
1682
- # if chat_history is not none, append role, content to messages
1683
- # chat_history = [(user, assistant), (user, assistant), ...]
1684
- # In the list, first one is user, then assistant
1685
  if chat_history is not None:
1686
  # 如果超過10則訊息,只保留最後10則訊息
1687
  if len(chat_history) > 10:
@@ -1713,7 +1711,7 @@ def chat_with_claude3(password, user_message, data, chat_history, content_subjec
1713
  # 建立 message API,讀取回應
1714
  response = BEDROCK_CLIENT.invoke_model(**kwargs)
1715
 
1716
- if response:
1717
  # 处理响应数据
1718
  response_body = json.loads(response.get('body').read())
1719
  response_completion = response_body.get('content')[0].get('text').strip()
@@ -1726,9 +1724,9 @@ def chat_with_claude3(password, user_message, data, chat_history, content_subjec
1726
 
1727
  # 返回聊天历史和空字符串清空输入框
1728
  return "", chat_history
1729
- else:
1730
  # 处理错误情况
1731
- print(f"Error: {response.status_code}")
1732
  return "请求失败,请稍后再试!", chat_history
1733
 
1734
  # --- Slide mode ---
@@ -1994,7 +1992,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
1994
  # CLAUDE 模式
1995
  claude_send_button.click(
1996
  chat_with_claude3,
1997
- inputs=[password, claude_msg, df_string_output, claude_chatbot, content_subject, content_grade, socratic_mode_btn],
1998
  outputs=[claude_msg, claude_chatbot]
1999
  )
2000
 
 
1638
 
1639
  return run.status
1640
 
1641
+ def chat_with_claude3(password, video_id, trascript, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
1642
  verify_password(password)
1643
+ trascript_json = json.loads(trascript)
1644
  for entry in data_json:
1645
  entry.pop('embed_url', None) # Remove 'embed_url' if it exists
1646
  entry.pop('screenshot_path', None)
1647
+ trascript_text = json.dumps(trascript_json, ensure_ascii=False, indent=2)
1648
+
1649
 
1650
  sys_content = f"""
1651
  科目:{content_subject}
 
1659
  - 一次只問一個問題,字數在100字以內
1660
  - 不要直接給予答案,讓學生自己思考
1661
  - 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
1662
+ - 在你回答的開頭標註【蘇格拉底助教:{video_id} 】
1663
 
1664
  if socratic_mode is False,
1665
  - 直接回答學生問題,字數在100字以內
1666
+ - 在你回答的開頭標註【一般學習精靈:{video_id} 】
1667
 
1668
  rule:
1669
  - 請一定要用繁體中文回答 zh-TW,並用台灣人的口語表達,回答時不用特別說明這是台灣人的語氣,也不用說這是「台語的說法」
 
1680
 
1681
 
1682
  messages = []
 
 
 
 
1683
  if chat_history is not None:
1684
  # 如果超過10則訊息,只保留最後10則訊息
1685
  if len(chat_history) > 10:
 
1711
  # 建立 message API,讀取回應
1712
  response = BEDROCK_CLIENT.invoke_model(**kwargs)
1713
 
1714
+ try:
1715
  # 处理响应数据
1716
  response_body = json.loads(response.get('body').read())
1717
  response_completion = response_body.get('content')[0].get('text').strip()
 
1724
 
1725
  # 返回聊天历史和空字符串清空输入框
1726
  return "", chat_history
1727
+ except Exception as e:
1728
  # 处理错误情况
1729
+ print(f"Error: {e}")
1730
  return "请求失败,请稍后再试!", chat_history
1731
 
1732
  # --- Slide mode ---
 
1992
  # CLAUDE 模式
1993
  claude_send_button.click(
1994
  chat_with_claude3,
1995
+ inputs=[password, video_id, df_string_output, claude_msg, claude_chatbot, content_subject, content_grade, socratic_mode_btn],
1996
  outputs=[claude_msg, claude_chatbot]
1997
  )
1998