youngtsai commited on
Commit
c5051e3
1 Parent(s): f10efbb

chat_with_youtube_transcript

Browse files
Files changed (1) hide show
  1. app.py +94 -12
app.py CHANGED
@@ -55,7 +55,7 @@ CURRENT_INDEX = 0
55
  VIDEO_ID = ""
56
 
57
  OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
58
- client = OpenAI(api_key=OPEN_AI_KEY)
59
  DRIVE_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
60
  GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
61
 
@@ -659,7 +659,7 @@ def generate_mind_map(df_string):
659
  "max_tokens": 4000,
660
  }
661
 
662
- response = client.chat.completions.create(**request_payload)
663
  mind_map = response.choices[0].message.content.strip()
664
  print("=====mind_map=====")
665
  print(mind_map)
@@ -815,7 +815,7 @@ def generate_summarise(df_string):
815
  "max_tokens": 4000,
816
  }
817
 
818
- response = client.chat.completions.create(**request_payload)
819
  df_summarise = response.choices[0].message.content.strip()
820
  print("=====df_summarise=====")
821
  print(df_summarise)
@@ -847,7 +847,7 @@ def generate_questions(df_string):
847
  "response_format": response_format
848
  }
849
 
850
- response = client.chat.completions.create(**request_payload)
851
  questions = json.loads(response.choices[0].message.content)["questions"]
852
  print("=====json_response=====")
853
  print(questions)
@@ -990,7 +990,7 @@ def respond(user_message, data, chat_history, socratic_mode=False):
990
  "messages": messages,
991
  "max_tokens": 4000 # 設定一個較大的值,可根據需要調整
992
  }
993
- response = client.chat.completions.create(**request_payload)
994
  response_text = response.choices[0].message.content.strip()
995
 
996
  # 更新聊天历史
@@ -1003,6 +1003,63 @@ def respond(user_message, data, chat_history, socratic_mode=False):
1003
  # 返回聊天历史和空字符串清空输入框
1004
  return "", chat_history
1005
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1006
  def update_slide(direction):
1007
  global TRANSCRIPTS
1008
  global CURRENT_INDEX
@@ -1070,6 +1127,7 @@ with gr.Blocks() as demo:
1070
  youtube_link_btn = gr.Button("Submit_YouTube_Link")
1071
  web_link = gr.Textbox(label="Enter Web Page Link", visible=False)
1072
  chatbot = gr.Chatbot()
 
1073
  socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=False)
1074
  msg = gr.Textbox(label="Message")
1075
  send_button = gr.Button("Send")
@@ -1103,16 +1161,40 @@ with gr.Blocks() as demo:
1103
  mind_map = gr.Textbox(container=True, show_copy_button=True, lines=40, elem_id="mind_map_markdown")
1104
  with gr.Tab("心智圖",elem_id="mind_map_tab"):
1105
  mind_map_html = gr.HTML()
1106
-
 
 
 
 
 
 
 
 
 
 
 
 
1107
  send_button.click(
1108
- respond,
1109
- inputs=[msg, df_string_output, chatbot, socratic_mode_btn],
1110
- outputs=[msg, chatbot]
1111
  )
1112
  # 连接按钮点击事件
1113
- btn_1.click(respond, inputs=[btn_1, df_string_output, chatbot, socratic_mode_btn], outputs=[msg, chatbot])
1114
- btn_2.click(respond, inputs=[btn_2, df_string_output, chatbot, socratic_mode_btn], outputs=[msg, chatbot])
1115
- btn_3.click(respond, inputs=[btn_3, df_string_output, chatbot, socratic_mode_btn], outputs=[msg, chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
1116
 
1117
  btn_create_question.click(change_questions, inputs = [df_string_output], outputs = [btn_1, btn_2, btn_3])
1118
 
 
55
  VIDEO_ID = ""
56
 
57
  OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
58
+ OPEN_AI_CLIENT = OpenAI(api_key=OPEN_AI_KEY)
59
  DRIVE_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
60
  GCS_KEY = os.getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON")
61
 
 
659
  "max_tokens": 4000,
660
  }
661
 
662
+ response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
663
  mind_map = response.choices[0].message.content.strip()
664
  print("=====mind_map=====")
665
  print(mind_map)
 
815
  "max_tokens": 4000,
816
  }
817
 
818
+ response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
819
  df_summarise = response.choices[0].message.content.strip()
820
  print("=====df_summarise=====")
821
  print(df_summarise)
 
847
  "response_format": response_format
848
  }
849
 
850
+ response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
851
  questions = json.loads(response.choices[0].message.content)["questions"]
852
  print("=====json_response=====")
853
  print(questions)
 
990
  "messages": messages,
991
  "max_tokens": 4000 # 設定一個較大的值,可根據需要調整
992
  }
993
+ response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
994
  response_text = response.choices[0].message.content.strip()
995
 
996
  # 更新聊天历史
 
1003
  # 返回聊天历史和空字符串清空输入框
1004
  return "", chat_history
1005
 
1006
+ def chat_with_youtube_transcript(youtube_id, thread_id, user_message, chat_history, socratic_mode=False):
1007
+ assistant_id = "asst_kmvZLNkDUYaNkMNtZEAYxyPq"
1008
+ client = OPEN_AI_CLIENT
1009
+ # 创建线程
1010
+ if thread_id is None:
1011
+ thread = client.beta.threads.create()
1012
+ # 向线程添加初始系统消息,指定使用的 YouTube 视频ID
1013
+ client.beta.threads.messages.create(
1014
+ thread_id=thread.id,
1015
+ role="system",
1016
+ content=f"""
1017
+ 使用 YouTube 视频逐字稿数据:youtube_id = {youtube_id},
1018
+ 如果沒有找到逐字稿,就告訴 user 你有沒有逐字稿的資料。
1019
+ """
1020
+ )
1021
+ else:
1022
+ thread = client.beta.threads.retrieve(thread_id)
1023
+
1024
+ # 向线程添加用户的消息
1025
+ client.beta.threads.messages.create(
1026
+ thread_id=thread.id,
1027
+ role="user",
1028
+ content=user_message
1029
+ )
1030
+
1031
+ # 运行助手,生成响应
1032
+ run = client.beta.threads.runs.retrieve(
1033
+ thread_id=thread.id,
1034
+ assistant_id=assistant_id
1035
+ )
1036
+
1037
+ # check run status
1038
+ while run.status == "running":
1039
+ time.sleep(3)
1040
+ run = client.beta.threads.runs.retrieve(
1041
+ thread_id=thread.id,
1042
+ assistant_id=assistant_id
1043
+ )
1044
+
1045
+ # 获取助手的响应消息
1046
+ messages = client.beta.threads.messages.list(thread_id=thread.id)
1047
+ assistant_responses = [msg for msg in messages.data if msg.role == "assistant"]
1048
+
1049
+ # 返回最新的助手响应
1050
+ response_text = assistant_responses[-1].content if assistant_responses else "没有响应。"
1051
+
1052
+ # 更新聊天历史
1053
+ new_chat_history = (user_message, response_text)
1054
+ if chat_history is None:
1055
+ chat_history = [new_chat_history]
1056
+ else:
1057
+ chat_history.append(new_chat_history)
1058
+
1059
+ # 返回聊天历史和空字符串清空输入框
1060
+ return "", chat_history, thread.id
1061
+
1062
+
1063
  def update_slide(direction):
1064
  global TRANSCRIPTS
1065
  global CURRENT_INDEX
 
1127
  youtube_link_btn = gr.Button("Submit_YouTube_Link")
1128
  web_link = gr.Textbox(label="Enter Web Page Link", visible=False)
1129
  chatbot = gr.Chatbot()
1130
+ thread_id = gr.Textbox(label="thread_id")
1131
  socratic_mode_btn = gr.Checkbox(label="蘇格拉底家教助理模式", value=False)
1132
  msg = gr.Textbox(label="Message")
1133
  send_button = gr.Button("Send")
 
1161
  mind_map = gr.Textbox(container=True, show_copy_button=True, lines=40, elem_id="mind_map_markdown")
1162
  with gr.Tab("心智圖",elem_id="mind_map_tab"):
1163
  mind_map_html = gr.HTML()
1164
+
1165
+ # 傳統模式
1166
+ # send_button.click(
1167
+ # respond,
1168
+ # inputs=[msg, df_string_output, chatbot, socratic_mode_btn],
1169
+ # outputs=[msg, chatbot]
1170
+ # )
1171
+ # # 连接按钮点击事件
1172
+ # btn_1.click(respond, inputs=[btn_1, df_string_output, chatbot, socratic_mode_btn], outputs=[msg, chatbot])
1173
+ # btn_2.click(respond, inputs=[btn_2, df_string_output, chatbot, socratic_mode_btn], outputs=[msg, chatbot])
1174
+ # btn_3.click(respond, inputs=[btn_3, df_string_output, chatbot, socratic_mode_btn], outputs=[msg, chatbot])
1175
+
1176
+ # chat_with_youtube_transcript
1177
  send_button.click(
1178
+ chat_with_youtube_transcript,
1179
+ inputs=[get_video_id, thread_id, msg, chatbot, socratic_mode_btn],
1180
+ outputs=[msg, chatbot, thread_id]
1181
  )
1182
  # 连接按钮点击事件
1183
+ btn_1.click(
1184
+ chat_with_youtube_transcript,
1185
+ inputs=[get_video_id, thread_id, btn_1, chatbot, socratic_mode_btn],
1186
+ outputs=[msg, chatbot, thread_id]
1187
+ )
1188
+ btn_2.click(
1189
+ chat_with_youtube_transcript,
1190
+ inputs=[get_video_id, thread_id, btn_2, chatbot, socratic_mode_btn],
1191
+ outputs=[msg, chatbot, thread_id]
1192
+ )
1193
+ btn_3.click(
1194
+ chat_with_youtube_transcript,
1195
+ inputs=[get_video_id, thread_id, btn_3, chatbot, socratic_mode_btn],
1196
+ outputs=[msg, chatbot, thread_id]
1197
+ )
1198
 
1199
  btn_create_question.click(change_questions, inputs = [df_string_output], outputs = [btn_1, btn_2, btn_3])
1200