Spaces:
Running
Running
def get_ai_content(password, video_id, df_string, topic, grade, level, specific_feature, content_type, source="gcs"):
Browse files
app.py
CHANGED
@@ -906,37 +906,6 @@ def generate_summarise(df_string):
|
|
906 |
|
907 |
return df_summarise
|
908 |
|
909 |
-
def generate_questions(df_string):
|
910 |
-
# 使用 OpenAI 生成基于上传数据的问题
|
911 |
-
|
912 |
-
sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,並用既有資料為本質猜測用戶可能會問的問題,使用 zh-TW"
|
913 |
-
user_content = f"請根據 {df_string} 生成三個問題,並用 JSON 格式返回 questions:[q1的敘述text, q2的敘述text, q3的敘述text]"
|
914 |
-
messages = [
|
915 |
-
{"role": "system", "content": sys_content},
|
916 |
-
{"role": "user", "content": user_content}
|
917 |
-
]
|
918 |
-
response_format = { "type": "json_object" }
|
919 |
-
|
920 |
-
print("=====messages=====")
|
921 |
-
print(messages)
|
922 |
-
print("=====messages=====")
|
923 |
-
|
924 |
-
|
925 |
-
request_payload = {
|
926 |
-
"model": "gpt-4-1106-preview",
|
927 |
-
"messages": messages,
|
928 |
-
"max_tokens": 4000,
|
929 |
-
"response_format": response_format
|
930 |
-
}
|
931 |
-
|
932 |
-
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
933 |
-
questions = json.loads(response.choices[0].message.content)["questions"]
|
934 |
-
print("=====json_response=====")
|
935 |
-
print(questions)
|
936 |
-
print("=====json_response=====")
|
937 |
-
|
938 |
-
return questions
|
939 |
-
|
940 |
def get_questions(video_id, df_string, source="gcs"):
|
941 |
if source == "gcs":
|
942 |
# 去 gcs 確認是有有 video_id_questions.json
|
@@ -989,6 +958,37 @@ def get_questions(video_id, df_string, source="gcs"):
|
|
989 |
print("=====get_questions=====")
|
990 |
return q1, q2, q3
|
991 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
992 |
def change_questions(password, df_string):
|
993 |
verify_password(password)
|
994 |
|
@@ -1005,7 +1005,63 @@ def change_questions(password, df_string):
|
|
1005 |
|
1006 |
|
1007 |
# AI 生成教學素材
|
1008 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1009 |
verify_password(password)
|
1010 |
material = EducationalMaterial(df_string, topic, grade, level, specific_feature, content_type)
|
1011 |
prompt = material.generate_content_prompt()
|
@@ -1018,7 +1074,7 @@ def on_generate_ai_content(password, df_string, topic, grade, level, specific_fe
|
|
1018 |
"max_tokens": 4000 # 举例,实际上您可能需要更详细的配置
|
1019 |
}
|
1020 |
ai_content = material.send_ai_request(OPEN_AI_CLIENT, request_payload)
|
1021 |
-
return ai_content,
|
1022 |
|
1023 |
def generate_exam_fine_tune_result(password, exam_result_prompt , df_string_output, exam_result, exam_result_fine_tune_prompt):
|
1024 |
verify_password(password)
|
@@ -1850,18 +1906,18 @@ with gr.Blocks() as demo:
|
|
1850 |
|
1851 |
# 教師版 學習單
|
1852 |
worksheet_content_btn.click(
|
1853 |
-
|
1854 |
-
inputs=[password, df_string_output, content_topic, content_grade, content_level, worksheet_algorithm, worksheet_content_type_name],
|
1855 |
outputs=[worksheet_exam_result_original, worksheet_exam_result, worksheet_prompt, worksheet_exam_result_prompt]
|
1856 |
)
|
1857 |
lesson_plan_btn.click(
|
1858 |
-
|
1859 |
-
inputs=[password, df_string_output, content_topic, content_grade, content_level, lesson_plan_time, lesson_plan_content_type_name],
|
1860 |
outputs=[lesson_plan_exam_result_original, lesson_plan_exam_result, lesson_plan_prompt, lesson_plan_exam_result_prompt]
|
1861 |
)
|
1862 |
exit_ticket_btn.click(
|
1863 |
-
|
1864 |
-
inputs=[password, df_string_output, content_topic, content_grade, content_level, exit_ticket_time, exit_ticket_content_type_name],
|
1865 |
outputs=[exit_ticket_exam_result_original, exit_ticket_exam_result, exit_ticket_prompt, exit_ticket_exam_result_prompt]
|
1866 |
)
|
1867 |
|
|
|
906 |
|
907 |
return df_summarise
|
908 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
909 |
def get_questions(video_id, df_string, source="gcs"):
|
910 |
if source == "gcs":
|
911 |
# 去 gcs 確認是有有 video_id_questions.json
|
|
|
958 |
print("=====get_questions=====")
|
959 |
return q1, q2, q3
|
960 |
|
961 |
+
def generate_questions(df_string):
|
962 |
+
# 使用 OpenAI 生成基于上传数据的问题
|
963 |
+
|
964 |
+
sys_content = "你是一個擅長資料分析跟影片教學的老師,user 為學生,請精讀資料文本,自行判斷資料的種類,並用既有資料為本質猜測用戶可能會問的問題,使用 zh-TW"
|
965 |
+
user_content = f"請根據 {df_string} 生成三個問題,並用 JSON 格式返回 questions:[q1的敘述text, q2的敘述text, q3的敘述text]"
|
966 |
+
messages = [
|
967 |
+
{"role": "system", "content": sys_content},
|
968 |
+
{"role": "user", "content": user_content}
|
969 |
+
]
|
970 |
+
response_format = { "type": "json_object" }
|
971 |
+
|
972 |
+
print("=====messages=====")
|
973 |
+
print(messages)
|
974 |
+
print("=====messages=====")
|
975 |
+
|
976 |
+
|
977 |
+
request_payload = {
|
978 |
+
"model": "gpt-4-1106-preview",
|
979 |
+
"messages": messages,
|
980 |
+
"max_tokens": 4000,
|
981 |
+
"response_format": response_format
|
982 |
+
}
|
983 |
+
|
984 |
+
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
985 |
+
questions = json.loads(response.choices[0].message.content)["questions"]
|
986 |
+
print("=====json_response=====")
|
987 |
+
print(questions)
|
988 |
+
print("=====json_response=====")
|
989 |
+
|
990 |
+
return questions
|
991 |
+
|
992 |
def change_questions(password, df_string):
|
993 |
verify_password(password)
|
994 |
|
|
|
1005 |
|
1006 |
|
1007 |
# AI 生成教學素材
|
1008 |
+
def get_ai_content(password, video_id, df_string, topic, grade, level, specific_feature, content_type, source="gcs"):
|
1009 |
+
verify_password(password)
|
1010 |
+
if source == "gcs":
|
1011 |
+
print("===get_ai_content on gcs===")
|
1012 |
+
gcs_client = GCS_CLIENT
|
1013 |
+
bucket_name = 'video_ai_assistant'
|
1014 |
+
file_name = f'{video_id}_ai_content_list.json'
|
1015 |
+
blob_name = f"{video_id}/{file_name}"
|
1016 |
+
# 检查檔案是否存在
|
1017 |
+
is_file_exists = GCS_SERVICE.check_file_exists(bucket_name, blob_name)
|
1018 |
+
if not is_file_exists:
|
1019 |
+
# 先建立一個 ai_content_list.json
|
1020 |
+
ai_content_list = []
|
1021 |
+
ai_content_text = json.dumps(ai_content_list, ensure_ascii=False, indent=2)
|
1022 |
+
upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, ai_content_text)
|
1023 |
+
print("ai_content_list [] 已上傳到GCS")
|
1024 |
+
|
1025 |
+
# 此時 ai_content_list 已存在
|
1026 |
+
ai_content_list_string = download_blob_to_string(gcs_client, bucket_name, blob_name)
|
1027 |
+
ai_content_list = json.loads(ai_content_list_string)
|
1028 |
+
# by key 找到 ai_content (topic, grade, level, specific_feature, content_type)
|
1029 |
+
target_kvs = {
|
1030 |
+
"topic": topic,
|
1031 |
+
"grade": grade,
|
1032 |
+
"level": level,
|
1033 |
+
"specific_feature": specific_feature,
|
1034 |
+
"content_type": content_type
|
1035 |
+
}
|
1036 |
+
ai_content_json = [
|
1037 |
+
item for item in ai_content_list
|
1038 |
+
if all(item[k] == v for k, v in target_kvs.items())
|
1039 |
+
]
|
1040 |
+
|
1041 |
+
if len(ai_content_json) == 0:
|
1042 |
+
ai_content, prompt = generate_ai_content(password, df_string, topic, grade, level, specific_feature, content_type)
|
1043 |
+
ai_content_json = {
|
1044 |
+
"content": str(ai_content),
|
1045 |
+
"prompt": prompt,
|
1046 |
+
"topic": topic,
|
1047 |
+
"grade": grade,
|
1048 |
+
"level": level,
|
1049 |
+
"specific_feature": specific_feature,
|
1050 |
+
"content_type": content_type
|
1051 |
+
}
|
1052 |
+
|
1053 |
+
ai_content_list.append(ai_content_json)
|
1054 |
+
ai_content_text = json.dumps(ai_content_list, ensure_ascii=False, indent=2)
|
1055 |
+
upload_file_to_gcs_with_json_string(gcs_client, bucket_name, blob_name, ai_content_text)
|
1056 |
+
print("ai_content已上傳到GCS")
|
1057 |
+
else:
|
1058 |
+
ai_content_json = ai_content_json[0]
|
1059 |
+
ai_content = ai_content_json["content"]
|
1060 |
+
prompt = ai_content_json["prompt"]
|
1061 |
+
|
1062 |
+
return ai_content, ai_content, prompt, prompt
|
1063 |
+
|
1064 |
+
def generate_ai_content(password, df_string, topic, grade, level, specific_feature, content_type):
|
1065 |
verify_password(password)
|
1066 |
material = EducationalMaterial(df_string, topic, grade, level, specific_feature, content_type)
|
1067 |
prompt = material.generate_content_prompt()
|
|
|
1074 |
"max_tokens": 4000 # 举例,实际上您可能需要更详细的配置
|
1075 |
}
|
1076 |
ai_content = material.send_ai_request(OPEN_AI_CLIENT, request_payload)
|
1077 |
+
return ai_content, prompt
|
1078 |
|
1079 |
def generate_exam_fine_tune_result(password, exam_result_prompt , df_string_output, exam_result, exam_result_fine_tune_prompt):
|
1080 |
verify_password(password)
|
|
|
1906 |
|
1907 |
# 教師版 學習單
|
1908 |
worksheet_content_btn.click(
|
1909 |
+
get_ai_content,
|
1910 |
+
inputs=[password, video_id, df_string_output, content_topic, content_grade, content_level, worksheet_algorithm, worksheet_content_type_name],
|
1911 |
outputs=[worksheet_exam_result_original, worksheet_exam_result, worksheet_prompt, worksheet_exam_result_prompt]
|
1912 |
)
|
1913 |
lesson_plan_btn.click(
|
1914 |
+
get_ai_content,
|
1915 |
+
inputs=[password, video_id, df_string_output, content_topic, content_grade, content_level, lesson_plan_time, lesson_plan_content_type_name],
|
1916 |
outputs=[lesson_plan_exam_result_original, lesson_plan_exam_result, lesson_plan_prompt, lesson_plan_exam_result_prompt]
|
1917 |
)
|
1918 |
exit_ticket_btn.click(
|
1919 |
+
get_ai_content,
|
1920 |
+
inputs=[password, video_id, df_string_output, content_topic, content_grade, content_level, exit_ticket_time, exit_ticket_content_type_name],
|
1921 |
outputs=[exit_ticket_exam_result_original, exit_ticket_exam_result, exit_ticket_prompt, exit_ticket_exam_result_prompt]
|
1922 |
)
|
1923 |
|