Spaces:
Sleeping
Sleeping
def chat_with_youtube_transcript(youtube_id, thread_id, user_message, chat_history, socratic_mode=False):
Browse files
app.py
CHANGED
@@ -292,8 +292,7 @@ def update_file_on_drive(service, file_id, file_content):
|
|
292 |
print(f"文件已更新,文件ID: {updated_file['id']}")
|
293 |
|
294 |
|
295 |
-
#
|
296 |
-
|
297 |
def process_file(file):
|
298 |
# 读取文件
|
299 |
if file.name.endswith('.csv'):
|
@@ -464,9 +463,6 @@ def process_transcript_and_screenshots_on_gcs(video_id):
|
|
464 |
|
465 |
return updated_transcript_json
|
466 |
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
def process_youtube_link(link):
|
471 |
# 使用 YouTube API 获取逐字稿
|
472 |
# 假设您已经获取了 YouTube 视频的逐字稿并存储在变量 `transcript` 中
|
@@ -546,8 +542,6 @@ def process_youtube_link(link):
|
|
546 |
first_image, \
|
547 |
first_text,
|
548 |
|
549 |
-
|
550 |
-
|
551 |
def format_transcript_to_html(formatted_transcript):
|
552 |
html_content = ""
|
553 |
for entry in formatted_transcript:
|
@@ -583,7 +577,6 @@ def download_youtube_video(youtube_id, output_path=OUTPUT_PATH):
|
|
583 |
|
584 |
print(f"Video downloaded successfully: {output_path}/{youtube_id}.mp4")
|
585 |
|
586 |
-
|
587 |
def screenshot_youtube_video(youtube_id, snapshot_sec):
|
588 |
video_path = f'{OUTPUT_PATH}/{youtube_id}.mp4'
|
589 |
file_name = f"{youtube_id}_{snapshot_sec}.jpg"
|
@@ -1007,18 +1000,32 @@ def respond(user_message, data, chat_history, socratic_mode=False):
|
|
1007 |
def chat_with_youtube_transcript(youtube_id, thread_id, user_message, chat_history, socratic_mode=False):
|
1008 |
assistant_id = "asst_kmvZLNkDUYaNkMNtZEAYxyPq"
|
1009 |
client = OPEN_AI_CLIENT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1010 |
# 创建线程
|
1011 |
-
if
|
1012 |
thread = client.beta.threads.create()
|
1013 |
-
|
1014 |
-
client.beta.threads.messages.create(
|
1015 |
-
thread_id=thread.id,
|
1016 |
-
role="system",
|
1017 |
-
content=f"""
|
1018 |
-
使用 YouTube 视频逐字稿数据:youtube_id = {youtube_id},
|
1019 |
-
如果沒有找到逐字稿,就告訴 user 你有沒有逐字稿的資料。
|
1020 |
-
"""
|
1021 |
-
)
|
1022 |
else:
|
1023 |
thread = client.beta.threads.retrieve(thread_id)
|
1024 |
|
@@ -1032,7 +1039,8 @@ def chat_with_youtube_transcript(youtube_id, thread_id, user_message, chat_histo
|
|
1032 |
# 运行助手,生成响应
|
1033 |
run = client.beta.threads.runs.create(
|
1034 |
thread_id=thread.id,
|
1035 |
-
assistant_id=assistant_id
|
|
|
1036 |
)
|
1037 |
|
1038 |
# check run status
|
|
|
292 |
print(f"文件已更新,文件ID: {updated_file['id']}")
|
293 |
|
294 |
|
295 |
+
# ---- Main Functions ----
|
|
|
296 |
def process_file(file):
|
297 |
# 读取文件
|
298 |
if file.name.endswith('.csv'):
|
|
|
463 |
|
464 |
return updated_transcript_json
|
465 |
|
|
|
|
|
|
|
466 |
def process_youtube_link(link):
|
467 |
# 使用 YouTube API 获取逐字稿
|
468 |
# 假设您已经获取了 YouTube 视频的逐字稿并存储在变量 `transcript` 中
|
|
|
542 |
first_image, \
|
543 |
first_text,
|
544 |
|
|
|
|
|
545 |
def format_transcript_to_html(formatted_transcript):
|
546 |
html_content = ""
|
547 |
for entry in formatted_transcript:
|
|
|
577 |
|
578 |
print(f"Video downloaded successfully: {output_path}/{youtube_id}.mp4")
|
579 |
|
|
|
580 |
def screenshot_youtube_video(youtube_id, snapshot_sec):
|
581 |
video_path = f'{OUTPUT_PATH}/{youtube_id}.mp4'
|
582 |
file_name = f"{youtube_id}_{snapshot_sec}.jpg"
|
|
|
1000 |
def chat_with_youtube_transcript(youtube_id, thread_id, user_message, chat_history, socratic_mode=False):
|
1001 |
assistant_id = "asst_kmvZLNkDUYaNkMNtZEAYxyPq"
|
1002 |
client = OPEN_AI_CLIENT
|
1003 |
+
instructions = f"""
|
1004 |
+
你是一個擅長資料分析跟影片教學的老師,user 為學生
|
1005 |
+
請找到 file youtube_id = {youtube_id} 為資料文本,自行判斷資料的種類,
|
1006 |
+
如果 file 沒有資料,請告訴用戶沒有逐字稿資料,但仍然可以進行對話
|
1007 |
+
|
1008 |
+
並進行對話,使用 zh-TW
|
1009 |
+
如果是影片類型,不用解釋逐字稿格式,直接回答學生問題
|
1010 |
+
socratic_mode = {socratic_mode}
|
1011 |
+
如果 socratic_mode = True,
|
1012 |
+
- 請用蘇格拉底式的提問方式,引導學生思考,並且給予學生一些提示
|
1013 |
+
- 不要直接給予答案,讓學生自己思考
|
1014 |
+
- 但可以給予一些提示跟引導,例如給予影片的時間軸,讓學生自己去找答案
|
1015 |
+
- 在你回答的開頭標註【蘇格拉底助教】
|
1016 |
+
如果 socratic_mode = False,
|
1017 |
+
- 直接回答學生問題
|
1018 |
+
|
1019 |
+
如果學生問了一些問題你無法判斷,請告訴學生你無法判斷,並建議學生可以問其他問題
|
1020 |
+
或者你可以反問學生一些問題,幫助學生更好的理解資料
|
1021 |
+
如果學生的問題與資料文本無關,請告訴學生你無法回答超出範圍的問題
|
1022 |
+
|
1023 |
+
最後,如果 socratic_mode = True
|
1024 |
+
"""
|
1025 |
# 创建线程
|
1026 |
+
if not thread_id:
|
1027 |
thread = client.beta.threads.create()
|
1028 |
+
thread_id = thread.id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1029 |
else:
|
1030 |
thread = client.beta.threads.retrieve(thread_id)
|
1031 |
|
|
|
1039 |
# 运行助手,生成响应
|
1040 |
run = client.beta.threads.runs.create(
|
1041 |
thread_id=thread.id,
|
1042 |
+
assistant_id=assistant_id,
|
1043 |
+
instructions=instructions,
|
1044 |
)
|
1045 |
|
1046 |
# check run status
|