youngtsai commited on
Commit
4754d23
·
1 Parent(s): ec27b5b

if len(trascript_text) > 25000:

Browse files
Files changed (2) hide show
  1. app.py +7 -5
  2. chatbot.py +22 -1
app.py CHANGED
@@ -1609,7 +1609,7 @@ def download_exam_result(content):
1609
  return word_path
1610
 
1611
  # ---- Chatbot ----
1612
- def chat_with_ai(ai_name, password, video_id, trascript_state, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
1613
  verify_password(password)
1614
 
1615
  if chat_history is not None and len(chat_history) > 10:
@@ -1630,12 +1630,14 @@ def chat_with_ai(ai_name, password, video_id, trascript_state, user_message, cha
1630
  chatbot_config = {
1631
  "video_id": video_id,
1632
  "trascript": simple_transcript,
 
1633
  "content_subject": content_subject,
1634
  "content_grade": content_grade,
1635
  "jutor_chat_key": JUTOR_CHAT_KEY,
1636
  "ai_name": ai_name,
1637
  "ai_client": ai_client
1638
  }
 
1639
  chatbot = Chatbot(chatbot_config)
1640
  response_completion = chatbot.chat(user_message, chat_history, socratic_mode, ai_name)
1641
 
@@ -2314,13 +2316,13 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
2314
  # ai_chatbot 模式
2315
  ai_send_button.click(
2316
  chat_with_ai,
2317
- inputs=[ai_name, password, video_id, trascript_state, ai_msg, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn],
2318
  outputs=[ai_msg, ai_chatbot]
2319
  )
2320
  # ai_chatbot 连接按钮点击事件
2321
- ai_chatbot_question_1_chat_with_ai_input =[ai_name, password, video_id, trascript_state, ai_chatbot_question_1, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
2322
- ai_chatbot_question_2_chat_with_ai_input =[ai_name, password, video_id, trascript_state, ai_chatbot_question_2, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
2323
- ai_chatbot_question_3_chat_with_ai_input =[ai_name, password, video_id, trascript_state, ai_chatbot_question_3, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
2324
  ai_chatbot_question_1.click(
2325
  chat_with_ai,
2326
  inputs=ai_chatbot_question_1_chat_with_ai_input,
 
1609
  return word_path
1610
 
1611
  # ---- Chatbot ----
1612
+ def chat_with_ai(ai_name, password, video_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, socratic_mode=False):
1613
  verify_password(password)
1614
 
1615
  if chat_history is not None and len(chat_history) > 10:
 
1630
  chatbot_config = {
1631
  "video_id": video_id,
1632
  "trascript": simple_transcript,
1633
+ "key_moments": key_moments,
1634
  "content_subject": content_subject,
1635
  "content_grade": content_grade,
1636
  "jutor_chat_key": JUTOR_CHAT_KEY,
1637
  "ai_name": ai_name,
1638
  "ai_client": ai_client
1639
  }
1640
+
1641
  chatbot = Chatbot(chatbot_config)
1642
  response_completion = chatbot.chat(user_message, chat_history, socratic_mode, ai_name)
1643
 
 
2316
  # ai_chatbot 模式
2317
  ai_send_button.click(
2318
  chat_with_ai,
2319
+ inputs=[ai_name, password, video_id, trascript_state, key_moments, ai_msg, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn],
2320
  outputs=[ai_msg, ai_chatbot]
2321
  )
2322
  # ai_chatbot 连接按钮点击事件
2323
+ ai_chatbot_question_1_chat_with_ai_input =[ai_name, password, video_id, trascript_state, key_moments, ai_chatbot_question_1, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
2324
+ ai_chatbot_question_2_chat_with_ai_input =[ai_name, password, video_id, trascript_state, key_moments, ai_chatbot_question_2, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
2325
+ ai_chatbot_question_3_chat_with_ai_input =[ai_name, password, video_id, trascript_state, key_moments, ai_chatbot_question_3, ai_chatbot, content_subject, content_grade, ai_chatbot_socratic_mode_btn]
2326
  ai_chatbot_question_1.click(
2327
  chat_with_ai,
2328
  inputs=ai_chatbot_question_1_chat_with_ai_input,
chatbot.py CHANGED
@@ -9,6 +9,7 @@ class Chatbot:
9
  self.content_grade = config.get('content_grade')
10
  self.jutor_chat_key = config.get('jutor_chat_key')
11
  self.transcript_text = self.get_transcript_text(config.get('trascript'))
 
12
  self.ai_name = config.get('ai_name')
13
  self.ai_client = config.get('ai_client')
14
 
@@ -19,6 +20,15 @@ class Chatbot:
19
  transcript_json = transcript_data
20
  transcript_text = json.dumps(transcript_json, ensure_ascii=False)
21
  return transcript_text
 
 
 
 
 
 
 
 
 
22
 
23
  def chat(self, user_message, chat_history, socratic_mode=False, service_type='jutor'):
24
  messages = self.prepare_messages(chat_history, user_message)
@@ -34,12 +44,23 @@ class Chatbot:
34
  content_grade = self.content_grade
35
  video_id = self.video_id
36
  trascript_text = self.transcript_text
 
37
  socratic_mode = str(socratic_mode)
38
  ai_name = self.ai_name
 
 
 
 
 
 
 
 
 
 
39
  system_prompt = f"""
40
  科目:{content_subject}
41
  年級:{content_grade}
42
- 逐字稿資料:{trascript_text}
43
  -------------------------------------
44
  你是一個專業的{content_subject}老師, user 為{content_grade}的學生
45
  socratic_mode = {socratic_mode}
 
9
  self.content_grade = config.get('content_grade')
10
  self.jutor_chat_key = config.get('jutor_chat_key')
11
  self.transcript_text = self.get_transcript_text(config.get('trascript'))
12
+ self.key_moments_text = self.get_key_moments_text(config.get('key_moments'))
13
  self.ai_name = config.get('ai_name')
14
  self.ai_client = config.get('ai_client')
15
 
 
20
  transcript_json = transcript_data
21
  transcript_text = json.dumps(transcript_json, ensure_ascii=False)
22
  return transcript_text
23
+
24
+ def get_key_moments_text(self, key_moments_data):
25
+ if isinstance(key_moments_data, str):
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
+
32
 
33
  def chat(self, user_message, chat_history, socratic_mode=False, service_type='jutor'):
34
  messages = self.prepare_messages(chat_history, user_message)
 
44
  content_grade = self.content_grade
45
  video_id = self.video_id
46
  trascript_text = self.transcript_text
47
+ key_moments_text = self.key_moments_text
48
  socratic_mode = str(socratic_mode)
49
  ai_name = self.ai_name
50
+
51
+ # string with maximum length 32768
52
+ # if trascript_text is too long, replace by key_moments_text
53
+ if len(trascript_text) > 25000:
54
+ content_text = key_moments_text
55
+ print("=== transcript_text is too long, replace by key_moments_text ===")
56
+ else:
57
+ content_text = trascript_text
58
+ print("=== transcript_text is used ===")
59
+
60
  system_prompt = f"""
61
  科目:{content_subject}
62
  年級:{content_grade}
63
+ 逐字稿資料:{content_text}
64
  -------------------------------------
65
  你是一個專業的{content_subject}老師, user 為{content_grade}的學生
66
  socratic_mode = {socratic_mode}