Spaces:
Sleeping
Sleeping
update
Browse files
app.py
CHANGED
@@ -718,7 +718,7 @@ def get_reading_passage(video_id, df_string, source):
|
|
718 |
print("===get_reading_passage on gcs===")
|
719 |
gcs_client = GCS_CLIENT
|
720 |
bucket_name = 'video_ai_assistant'
|
721 |
-
file_name = f'{video_id}
|
722 |
blob_name = f"{video_id}/{file_name}"
|
723 |
# 检查 reading_passage 是否存在
|
724 |
is_file_exists = GCS_SERVICE.check_file_exists(bucket_name, blob_name)
|
@@ -883,7 +883,7 @@ def get_video_id_summary(video_id, df_string, source):
|
|
883 |
print("===get_video_id_summary on gcs===")
|
884 |
gcs_client = GCS_CLIENT
|
885 |
bucket_name = 'video_ai_assistant'
|
886 |
-
file_name = f'{video_id}
|
887 |
summary_file_blob_name = f"{video_id}/{file_name}"
|
888 |
# 检查 summary_file 是否存在
|
889 |
is_summary_file_exists = GCS_SERVICE.check_file_exists(bucket_name, summary_file_blob_name)
|
@@ -1448,9 +1448,9 @@ def get_LLM_content(video_id, kind):
|
|
1448 |
if is_file_exists:
|
1449 |
content = download_blob_to_string(gcs_client, bucket_name, blob_name)
|
1450 |
content_json = json.loads(content)
|
1451 |
-
if kind == "
|
1452 |
content_text = content_json["reading_passage"]
|
1453 |
-
elif kind == "
|
1454 |
content_text = content_json["summary"]
|
1455 |
else:
|
1456 |
content_text = json.dumps(content_json, ensure_ascii=False, indent=2)
|
@@ -1481,14 +1481,14 @@ def update_LLM_content(video_id, new_content, kind):
|
|
1481 |
file_name = f'{video_id}_{kind}.json'
|
1482 |
blob_name = f"{video_id}/{file_name}"
|
1483 |
|
1484 |
-
if kind == "
|
1485 |
print("=========reading_passage=======")
|
1486 |
print(new_content)
|
1487 |
reading_passage_json = {"reading_passage": str(new_content)}
|
1488 |
reading_passage_text = json.dumps(reading_passage_json, ensure_ascii=False, indent=2)
|
1489 |
upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, reading_passage_text)
|
1490 |
updated_content = new_content
|
1491 |
-
elif kind == "
|
1492 |
summary_json = {"summary": str(new_content)}
|
1493 |
summary_text = json.dumps(summary_json, ensure_ascii=False, indent=2)
|
1494 |
upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, summary_text)
|
@@ -1535,10 +1535,10 @@ def create_LLM_content(video_id, df_string, kind):
|
|
1535 |
print(f"===create_{kind}===")
|
1536 |
print(f"video_id: {video_id}")
|
1537 |
|
1538 |
-
if kind == "
|
1539 |
content = generate_reading_passage(df_string)
|
1540 |
update_LLM_content(video_id, content, kind)
|
1541 |
-
elif kind == "
|
1542 |
content = generate_summarise(df_string)
|
1543 |
update_LLM_content(video_id, content, kind)
|
1544 |
elif kind == "mind_map":
|
@@ -1975,11 +1975,27 @@ def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, tras
|
|
1975 |
|
1976 |
# 创建线程
|
1977 |
if not thread_id:
|
1978 |
-
thread = client.beta.threads.create(
|
|
|
|
|
1979 |
thread_id = thread.id
|
1980 |
else:
|
1981 |
thread = client.beta.threads.retrieve(thread_id)
|
1982 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1983 |
# 向线程添加用户的消息
|
1984 |
client.beta.threads.messages.create(
|
1985 |
thread_id=thread.id,
|
@@ -2088,7 +2104,7 @@ def poll_run_status(run_id, thread_id, timeout=600, poll_interval=5):
|
|
2088 |
|
2089 |
return run.status
|
2090 |
|
2091 |
-
def streaming_chat_with_open_ai(user_message, chat_history, password, user_data, thread_id, trascript, key_moments, content_subject, content_grade):
|
2092 |
verify_password(password)
|
2093 |
|
2094 |
print("=====user_data=====")
|
@@ -2127,6 +2143,8 @@ def streaming_chat_with_open_ai(user_message, chat_history, password, user_data,
|
|
2127 |
# key_moments_json remove images
|
2128 |
for moment in key_moments_json:
|
2129 |
moment.pop('images', None)
|
|
|
|
|
2130 |
key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
|
2131 |
|
2132 |
instructions = get_instructions(content_subject, content_grade, key_moments_text)
|
@@ -2139,6 +2157,18 @@ def streaming_chat_with_open_ai(user_message, chat_history, password, user_data,
|
|
2139 |
thread = client.beta.threads.retrieve(thread_id)
|
2140 |
print(f"old thread_id: {thread_id}")
|
2141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2142 |
# 向线程添加用户的消息
|
2143 |
client.beta.threads.messages.create(
|
2144 |
thread_id=thread.id,
|
@@ -2438,7 +2468,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2438 |
🔠 鍵盤輸入你的問題,我會盡力回答你的問題喔!\n
|
2439 |
💤 我還在成長,體力有限,每一次學習只能回答十個問題,請讓我休息一下再問問題喔!
|
2440 |
"""
|
2441 |
-
additional_inputs = [password, user_data, streaming_chat_thread_id_state, trascript_state, key_moments_state, content_subject_state, content_grade_state]
|
2442 |
streaming_chat = gr.ChatInterface(
|
2443 |
fn=streaming_chat_with_open_ai,
|
2444 |
additional_inputs=additional_inputs,
|
@@ -2577,37 +2607,37 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2577 |
transcript_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2578 |
transcript_update_button = gr.Button("儲存", size="sm", variant="primary")
|
2579 |
transcript_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2580 |
-
transcript_create_button = gr.Button("
|
2581 |
with gr.Row():
|
2582 |
df_string_output = gr.Textbox(lines=40, label="Data Text", interactive=False, show_copy_button=True)
|
2583 |
with gr.Tab("文章本文"):
|
2584 |
with gr.Row() as reading_passage_admin:
|
2585 |
with gr.Column():
|
2586 |
with gr.Row():
|
2587 |
-
reading_passage_kind = gr.Textbox(value="
|
2588 |
with gr.Row():
|
2589 |
-
reading_passage_text_to_latex = gr.Button("新增 LaTeX", size="sm", variant="primary")
|
2590 |
reading_passage_get_button = gr.Button("取得", size="sm", variant="primary")
|
2591 |
reading_passage_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2592 |
-
reading_passage_update_button = gr.Button("
|
2593 |
reading_passage_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2594 |
-
reading_passage_create_button = gr.Button("
|
2595 |
with gr.Row():
|
2596 |
-
reading_passage_text = gr.Textbox(label="
|
2597 |
with gr.Tab("重點摘要本文"):
|
2598 |
with gr.Row() as summary_admmin:
|
2599 |
with gr.Column():
|
2600 |
with gr.Row():
|
2601 |
-
summary_kind = gr.Textbox(value="
|
2602 |
with gr.Row():
|
2603 |
-
summary_to_markdown = gr.Button("新增 Markdown", size="sm", variant="primary")
|
2604 |
summary_get_button = gr.Button("取得", size="sm", variant="primary")
|
2605 |
summary_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2606 |
-
summary_update_button = gr.Button("
|
2607 |
summary_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2608 |
-
summary_create_button = gr.Button("
|
2609 |
with gr.Row():
|
2610 |
-
summary_text = gr.Textbox(label="
|
2611 |
with gr.Tab("關鍵時刻本文"):
|
2612 |
with gr.Row() as key_moments_admin:
|
2613 |
key_moments_kind = gr.Textbox(value="key_moments", show_label=False)
|
@@ -2615,7 +2645,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2615 |
key_moments_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2616 |
key_moments_update_button = gr.Button("儲存", size="sm", variant="primary")
|
2617 |
key_moments_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2618 |
-
key_moments_create_button = gr.Button("
|
2619 |
with gr.Row():
|
2620 |
key_moments = gr.Textbox(label="Key Moments", lines=40, interactive=False, show_copy_button=True)
|
2621 |
with gr.Tab("問題本文"):
|
@@ -2806,11 +2836,11 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2806 |
# web_link.change(process_web_link, inputs=web_link, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
|
2807 |
|
2808 |
# reading_passage event
|
2809 |
-
reading_passage_text_to_latex.click(
|
2810 |
-
|
2811 |
-
|
2812 |
-
|
2813 |
-
)
|
2814 |
reading_passage_get_button.click(
|
2815 |
get_LLM_content,
|
2816 |
inputs=[video_id, reading_passage_kind],
|
@@ -2838,11 +2868,11 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2838 |
)
|
2839 |
|
2840 |
# summary event
|
2841 |
-
summary_to_markdown.click(
|
2842 |
-
|
2843 |
-
|
2844 |
-
|
2845 |
-
)
|
2846 |
summary_get_button.click(
|
2847 |
get_LLM_content,
|
2848 |
inputs=[video_id, summary_kind],
|
|
|
718 |
print("===get_reading_passage on gcs===")
|
719 |
gcs_client = GCS_CLIENT
|
720 |
bucket_name = 'video_ai_assistant'
|
721 |
+
file_name = f'{video_id}_reading_passage_latex.json'
|
722 |
blob_name = f"{video_id}/{file_name}"
|
723 |
# 检查 reading_passage 是否存在
|
724 |
is_file_exists = GCS_SERVICE.check_file_exists(bucket_name, blob_name)
|
|
|
883 |
print("===get_video_id_summary on gcs===")
|
884 |
gcs_client = GCS_CLIENT
|
885 |
bucket_name = 'video_ai_assistant'
|
886 |
+
file_name = f'{video_id}_summary_markdown.json'
|
887 |
summary_file_blob_name = f"{video_id}/{file_name}"
|
888 |
# 检查 summary_file 是否存在
|
889 |
is_summary_file_exists = GCS_SERVICE.check_file_exists(bucket_name, summary_file_blob_name)
|
|
|
1448 |
if is_file_exists:
|
1449 |
content = download_blob_to_string(gcs_client, bucket_name, blob_name)
|
1450 |
content_json = json.loads(content)
|
1451 |
+
if kind == "reading_passage_latex":
|
1452 |
content_text = content_json["reading_passage"]
|
1453 |
+
elif kind == "summary_markdown":
|
1454 |
content_text = content_json["summary"]
|
1455 |
else:
|
1456 |
content_text = json.dumps(content_json, ensure_ascii=False, indent=2)
|
|
|
1481 |
file_name = f'{video_id}_{kind}.json'
|
1482 |
blob_name = f"{video_id}/{file_name}"
|
1483 |
|
1484 |
+
if kind == "reading_passage_latex":
|
1485 |
print("=========reading_passage=======")
|
1486 |
print(new_content)
|
1487 |
reading_passage_json = {"reading_passage": str(new_content)}
|
1488 |
reading_passage_text = json.dumps(reading_passage_json, ensure_ascii=False, indent=2)
|
1489 |
upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, reading_passage_text)
|
1490 |
updated_content = new_content
|
1491 |
+
elif kind == "summary_markdown":
|
1492 |
summary_json = {"summary": str(new_content)}
|
1493 |
summary_text = json.dumps(summary_json, ensure_ascii=False, indent=2)
|
1494 |
upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, summary_text)
|
|
|
1535 |
print(f"===create_{kind}===")
|
1536 |
print(f"video_id: {video_id}")
|
1537 |
|
1538 |
+
if kind == "reading_passage_latex":
|
1539 |
content = generate_reading_passage(df_string)
|
1540 |
update_LLM_content(video_id, content, kind)
|
1541 |
+
elif kind == "summary_markdown":
|
1542 |
content = generate_summarise(df_string)
|
1543 |
update_LLM_content(video_id, content, kind)
|
1544 |
elif kind == "mind_map":
|
|
|
1975 |
|
1976 |
# 创建线程
|
1977 |
if not thread_id:
|
1978 |
+
thread = client.beta.threads.create(
|
1979 |
+
|
1980 |
+
)
|
1981 |
thread_id = thread.id
|
1982 |
else:
|
1983 |
thread = client.beta.threads.retrieve(thread_id)
|
1984 |
|
1985 |
+
# add meta data to thread
|
1986 |
+
client.beta.threads.update(
|
1987 |
+
thread_id=thread_id,
|
1988 |
+
metadata={
|
1989 |
+
"youtube_id": youtube_id,
|
1990 |
+
"user_data": user_data,
|
1991 |
+
"content_subject": content_subject,
|
1992 |
+
"content_grade": content_grade,
|
1993 |
+
"socratic_mode": socratic_mode,
|
1994 |
+
"assistant_id": assistant_id,
|
1995 |
+
"is_streaming": "false",
|
1996 |
+
}
|
1997 |
+
)
|
1998 |
+
|
1999 |
# 向线程添加用户的消息
|
2000 |
client.beta.threads.messages.create(
|
2001 |
thread_id=thread.id,
|
|
|
2104 |
|
2105 |
return run.status
|
2106 |
|
2107 |
+
def streaming_chat_with_open_ai(user_message, chat_history, password, video_id, user_data, thread_id, trascript, key_moments, content_subject, content_grade):
|
2108 |
verify_password(password)
|
2109 |
|
2110 |
print("=====user_data=====")
|
|
|
2143 |
# key_moments_json remove images
|
2144 |
for moment in key_moments_json:
|
2145 |
moment.pop('images', None)
|
2146 |
+
moment.pop('end', None)
|
2147 |
+
moment.pop('transcript', None)
|
2148 |
key_moments_text = json.dumps(key_moments_json, ensure_ascii=False)
|
2149 |
|
2150 |
instructions = get_instructions(content_subject, content_grade, key_moments_text)
|
|
|
2157 |
thread = client.beta.threads.retrieve(thread_id)
|
2158 |
print(f"old thread_id: {thread_id}")
|
2159 |
|
2160 |
+
client.beta.threads.update(
|
2161 |
+
thread_id=thread_id,
|
2162 |
+
metadata={
|
2163 |
+
"youtube_id": video_id,
|
2164 |
+
"user_data": user_data,
|
2165 |
+
"content_subject": content_subject,
|
2166 |
+
"content_grade": content_grade,
|
2167 |
+
"assistant_id": assistant_id,
|
2168 |
+
"is_streaming": "true",
|
2169 |
+
}
|
2170 |
+
)
|
2171 |
+
|
2172 |
# 向线程添加用户的消息
|
2173 |
client.beta.threads.messages.create(
|
2174 |
thread_id=thread.id,
|
|
|
2468 |
🔠 鍵盤輸入你的問題,我會盡力回答你的問題喔!\n
|
2469 |
💤 我還在成長,體力有限,每一次學習只能回答十個問題,請讓我休息一下再問問題喔!
|
2470 |
"""
|
2471 |
+
additional_inputs = [password, video_id, user_data, streaming_chat_thread_id_state, trascript_state, key_moments_state, content_subject_state, content_grade_state]
|
2472 |
streaming_chat = gr.ChatInterface(
|
2473 |
fn=streaming_chat_with_open_ai,
|
2474 |
additional_inputs=additional_inputs,
|
|
|
2607 |
transcript_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2608 |
transcript_update_button = gr.Button("儲存", size="sm", variant="primary")
|
2609 |
transcript_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2610 |
+
transcript_create_button = gr.Button("重建", size="sm", variant="primary")
|
2611 |
with gr.Row():
|
2612 |
df_string_output = gr.Textbox(lines=40, label="Data Text", interactive=False, show_copy_button=True)
|
2613 |
with gr.Tab("文章本文"):
|
2614 |
with gr.Row() as reading_passage_admin:
|
2615 |
with gr.Column():
|
2616 |
with gr.Row():
|
2617 |
+
reading_passage_kind = gr.Textbox(value="reading_passage_latex", show_label=False)
|
2618 |
with gr.Row():
|
2619 |
+
# reading_passage_text_to_latex = gr.Button("新增 LaTeX", size="sm", variant="primary")
|
2620 |
reading_passage_get_button = gr.Button("取得", size="sm", variant="primary")
|
2621 |
reading_passage_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2622 |
+
reading_passage_update_button = gr.Button("儲存", size="sm", variant="primary")
|
2623 |
reading_passage_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2624 |
+
reading_passage_create_button = gr.Button("重建", size="sm", variant="primary")
|
2625 |
with gr.Row():
|
2626 |
+
reading_passage_text = gr.Textbox(label="reading_passage_latex", lines=40, interactive=False, show_copy_button=True)
|
2627 |
with gr.Tab("重點摘要本文"):
|
2628 |
with gr.Row() as summary_admmin:
|
2629 |
with gr.Column():
|
2630 |
with gr.Row():
|
2631 |
+
summary_kind = gr.Textbox(value="summary_markdown", show_label=False)
|
2632 |
with gr.Row():
|
2633 |
+
# summary_to_markdown = gr.Button("新增 Markdown", size="sm", variant="primary")
|
2634 |
summary_get_button = gr.Button("取得", size="sm", variant="primary")
|
2635 |
summary_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2636 |
+
summary_update_button = gr.Button("儲存", size="sm", variant="primary")
|
2637 |
summary_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2638 |
+
summary_create_button = gr.Button("重建", size="sm", variant="primary")
|
2639 |
with gr.Row():
|
2640 |
+
summary_text = gr.Textbox(label="summary_markdown", lines=40, interactive=False, show_copy_button=True)
|
2641 |
with gr.Tab("關鍵時刻本文"):
|
2642 |
with gr.Row() as key_moments_admin:
|
2643 |
key_moments_kind = gr.Textbox(value="key_moments", show_label=False)
|
|
|
2645 |
key_moments_edit_button = gr.Button("編輯", size="sm", variant="primary")
|
2646 |
key_moments_update_button = gr.Button("儲存", size="sm", variant="primary")
|
2647 |
key_moments_delete_button = gr.Button("刪除", size="sm", variant="primary")
|
2648 |
+
key_moments_create_button = gr.Button("重建", size="sm", variant="primary")
|
2649 |
with gr.Row():
|
2650 |
key_moments = gr.Textbox(label="Key Moments", lines=40, interactive=False, show_copy_button=True)
|
2651 |
with gr.Tab("問題本文"):
|
|
|
2836 |
# web_link.change(process_web_link, inputs=web_link, outputs=[btn_1, btn_2, btn_3, df_summarise, df_string_output])
|
2837 |
|
2838 |
# reading_passage event
|
2839 |
+
# reading_passage_text_to_latex.click(
|
2840 |
+
# reading_passage_add_latex_version,
|
2841 |
+
# inputs=[video_id],
|
2842 |
+
# outputs=[reading_passage_text]
|
2843 |
+
# )
|
2844 |
reading_passage_get_button.click(
|
2845 |
get_LLM_content,
|
2846 |
inputs=[video_id, reading_passage_kind],
|
|
|
2868 |
)
|
2869 |
|
2870 |
# summary event
|
2871 |
+
# summary_to_markdown.click(
|
2872 |
+
# summary_add_markdown_version,
|
2873 |
+
# inputs=[video_id],
|
2874 |
+
# outputs=[summary_text]
|
2875 |
+
# )
|
2876 |
summary_get_button.click(
|
2877 |
get_LLM_content,
|
2878 |
inputs=[video_id, summary_kind],
|