Spaces:
Sleeping
Sleeping
class EducationalMaterial:
Browse files- app.py +18 -22
- educational_material.py +66 -8
app.py
CHANGED
@@ -1963,30 +1963,26 @@ def generate_ai_content(password, df_string, topic, grade, level, specific_featu
|
|
1963 |
verify_password(password)
|
1964 |
material = EducationalMaterial(df_string, topic, grade, level, specific_feature, content_type)
|
1965 |
prompt = material.generate_content_prompt()
|
1966 |
-
|
1967 |
-
|
1968 |
-
|
1969 |
-
|
1970 |
-
"
|
1971 |
-
|
1972 |
-
"
|
1973 |
-
|
1974 |
-
|
1975 |
return ai_content, prompt
|
1976 |
|
1977 |
def generate_exam_fine_tune_result(password, exam_result_prompt , df_string_output, exam_result, exam_result_fine_tune_prompt):
|
1978 |
verify_password(password)
|
1979 |
material = EducationalMaterial(df_string_output, "", "", "", "", "")
|
1980 |
-
|
1981 |
-
|
1982 |
-
|
1983 |
-
|
1984 |
-
|
1985 |
-
|
1986 |
-
"max_tokens": 4000 # 举例,实际上您可能需要更详细的配置
|
1987 |
-
}
|
1988 |
-
ai_content = material.send_ai_request(OPEN_AI_CLIENT, request_payload)
|
1989 |
-
return ai_content
|
1990 |
|
1991 |
def return_original_exam_result(exam_result_original):
|
1992 |
return exam_result_original
|
@@ -2754,7 +2750,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2754 |
with gr.Row():
|
2755 |
worksheet_content_type_name = gr.Textbox(value="worksheet", visible=False)
|
2756 |
worksheet_algorithm = gr.Dropdown(label="選擇教學策略或理論", choices=["Bloom認知階層理論", "Polya數學解題法", "CRA教學法"], value="Bloom認知階層理論", visible=False)
|
2757 |
-
worksheet_content_btn = gr.Button("生成學習單 📄", variant="primary", visible=
|
2758 |
with gr.Accordion("微調", open=False):
|
2759 |
worksheet_exam_result_fine_tune_prompt = gr.Textbox(label="根據結果,輸入你想更改的想法")
|
2760 |
worksheet_exam_result_fine_tune_btn = gr.Button("微調結果", variant="primary")
|
@@ -2775,7 +2771,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2775 |
with gr.Row():
|
2776 |
lesson_plan_content_type_name = gr.Textbox(value="lesson_plan", visible=False)
|
2777 |
lesson_plan_time = gr.Slider(label="選擇課程時間(分鐘)", minimum=10, maximum=120, step=5, value=40)
|
2778 |
-
lesson_plan_btn = gr.Button("生成教案 📕", variant="primary", visible=
|
2779 |
with gr.Accordion("微調", open=False):
|
2780 |
lesson_plan_exam_result_fine_tune_prompt = gr.Textbox(label="根據結果,輸入你想更改的想法")
|
2781 |
lesson_plan_exam_result_fine_tune_btn = gr.Button("微調結果", variant="primary")
|
@@ -2796,7 +2792,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2796 |
with gr.Row():
|
2797 |
exit_ticket_content_type_name = gr.Textbox(value="exit_ticket", visible=False)
|
2798 |
exit_ticket_time = gr.Slider(label="選擇出場券時間(分鐘)", minimum=5, maximum=10, step=1, value=8)
|
2799 |
-
exit_ticket_btn = gr.Button("生成出場券 🎟️", variant="primary", visible=
|
2800 |
with gr.Accordion("微調", open=False):
|
2801 |
exit_ticket_exam_result_fine_tune_prompt = gr.Textbox(label="根據結果,輸入你想更改的想法")
|
2802 |
exit_ticket_exam_result_fine_tune_btn = gr.Button("微調結果", variant="primary")
|
|
|
1963 |
verify_password(password)
|
1964 |
material = EducationalMaterial(df_string, topic, grade, level, specific_feature, content_type)
|
1965 |
prompt = material.generate_content_prompt()
|
1966 |
+
try:
|
1967 |
+
ai_content = material.get_ai_content(OPEN_AI_CLIENT, ai_type="openai")
|
1968 |
+
except Exception as e:
|
1969 |
+
error_msg = f" {video_id} OPEN AI 生成教學素材錯誤: {str(e)}"
|
1970 |
+
print("===generate_ai_content error===")
|
1971 |
+
print(error_msg)
|
1972 |
+
print("===generate_ai_content error===")
|
1973 |
+
ai_content = material.get_ai_content(BEDROCK_CLIENT, ai_type="bedrock")
|
1974 |
+
|
1975 |
return ai_content, prompt
|
1976 |
|
1977 |
def generate_exam_fine_tune_result(password, exam_result_prompt , df_string_output, exam_result, exam_result_fine_tune_prompt):
|
1978 |
verify_password(password)
|
1979 |
material = EducationalMaterial(df_string_output, "", "", "", "", "")
|
1980 |
+
try:
|
1981 |
+
fine_tuned_ai_content = material.get_fine_tuned_ai_content(OPEN_AI_CLIENT, "openai", exam_result_prompt, exam_result, exam_result_fine_tune_prompt)
|
1982 |
+
except:
|
1983 |
+
fine_tuned_ai_content = material.get_fine_tuned_ai_content(BEDROCK_CLIENT, "bedrock", exam_result_prompt, exam_result, exam_result_fine_tune_prompt)
|
1984 |
+
|
1985 |
+
return fine_tuned_ai_content
|
|
|
|
|
|
|
|
|
1986 |
|
1987 |
def return_original_exam_result(exam_result_original):
|
1988 |
return exam_result_original
|
|
|
2750 |
with gr.Row():
|
2751 |
worksheet_content_type_name = gr.Textbox(value="worksheet", visible=False)
|
2752 |
worksheet_algorithm = gr.Dropdown(label="選擇教學策略或理論", choices=["Bloom認知階層理論", "Polya數學解題法", "CRA教學法"], value="Bloom認知階層理論", visible=False)
|
2753 |
+
worksheet_content_btn = gr.Button("生成學習單 📄", variant="primary", visible=True)
|
2754 |
with gr.Accordion("微調", open=False):
|
2755 |
worksheet_exam_result_fine_tune_prompt = gr.Textbox(label="根據結果,輸入你想更改的想法")
|
2756 |
worksheet_exam_result_fine_tune_btn = gr.Button("微調結果", variant="primary")
|
|
|
2771 |
with gr.Row():
|
2772 |
lesson_plan_content_type_name = gr.Textbox(value="lesson_plan", visible=False)
|
2773 |
lesson_plan_time = gr.Slider(label="選擇課程時間(分鐘)", minimum=10, maximum=120, step=5, value=40)
|
2774 |
+
lesson_plan_btn = gr.Button("生成教案 📕", variant="primary", visible=True)
|
2775 |
with gr.Accordion("微調", open=False):
|
2776 |
lesson_plan_exam_result_fine_tune_prompt = gr.Textbox(label="根據結果,輸入你想更改的想法")
|
2777 |
lesson_plan_exam_result_fine_tune_btn = gr.Button("微調結果", variant="primary")
|
|
|
2792 |
with gr.Row():
|
2793 |
exit_ticket_content_type_name = gr.Textbox(value="exit_ticket", visible=False)
|
2794 |
exit_ticket_time = gr.Slider(label="選擇出場券時間(分鐘)", minimum=5, maximum=10, step=1, value=8)
|
2795 |
+
exit_ticket_btn = gr.Button("生成出場券 🎟️", variant="primary", visible=True)
|
2796 |
with gr.Accordion("微調", open=False):
|
2797 |
exit_ticket_exam_result_fine_tune_prompt = gr.Textbox(label="根據結果,輸入你想更改的想法")
|
2798 |
exit_ticket_exam_result_fine_tune_btn = gr.Button("微調結果", variant="primary")
|
educational_material.py
CHANGED
@@ -32,6 +32,72 @@ class EducationalMaterial:
|
|
32 |
self.content_type = content_type # 'worksheet' or 'lesson_plan'
|
33 |
self.system_content = "你是一個擅長資料分析跟影片教學備課的老師,請精讀資料文本,自行判斷資料的種類,使用 zh-TW,遇到數學符號或是敘述請用 Latex 語法($...$),例如:$x^2$。"
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def _prepare_context(self, context):
|
36 |
context_json = json.loads(context)
|
37 |
processed_context = ""
|
@@ -420,14 +486,6 @@ class EducationalMaterial:
|
|
420 |
"""
|
421 |
return exit_ticket_prompt
|
422 |
|
423 |
-
def create_ai_content(self, ai_client, request_payload):
|
424 |
-
user_content = self.build_user_content()
|
425 |
-
messages = self.build_messages(user_content)
|
426 |
-
request_payload['messages'] = messages
|
427 |
-
response_content = self.send_ai_request(ai_client, request_payload)
|
428 |
-
|
429 |
-
return response_content
|
430 |
-
|
431 |
def build_user_content(self):
|
432 |
if self.content_type == 'worksheet':
|
433 |
specific_feature_text = f"理論模型: {self.specific_feature}"
|
|
|
32 |
self.content_type = content_type # 'worksheet' or 'lesson_plan'
|
33 |
self.system_content = "你是一個擅長資料分析跟影片教學備課的老師,請精讀資料文本,自行判斷資料的種類,使用 zh-TW,遇到數學符號或是敘述請用 Latex 語法($...$),例如:$x^2$。"
|
34 |
|
35 |
+
def get_ai_content(self, AI_Client ,ai_type="openai"):
|
36 |
+
system_content = self.system_content
|
37 |
+
user_content = self.build_user_content()
|
38 |
+
if ai_type.lower() == "openai":
|
39 |
+
return self.send_openai_request(AI_Client, system_content, user_content)
|
40 |
+
elif ai_type.lower() == "bedrock":
|
41 |
+
return self.send_bedrock_request(AI_Client, system_content, user_content)
|
42 |
+
else:
|
43 |
+
raise ValueError("Unsupported AI type. Please choose 'openai' or 'redrock'.")
|
44 |
+
|
45 |
+
def get_fine_tuned_ai_content(self, ai_client, ai_type, original_prompt, result, fine_tune_prompt):
|
46 |
+
system_content = self.system_content
|
47 |
+
user_content = self.build_fine_tune_user_content(original_prompt, result, fine_tune_prompt)
|
48 |
+
if ai_type.lower() == "openai":
|
49 |
+
return self.send_openai_request(ai_client, system_content, user_content)
|
50 |
+
elif ai_type.lower() == "bedrock":
|
51 |
+
return self.send_bedrock_request(ai_client, system_content, user_content)
|
52 |
+
else:
|
53 |
+
raise ValueError("Unsupported AI type. Please choose 'openai' or 'redrock'.")
|
54 |
+
|
55 |
+
def send_openai_request(self, AI_Client, system_content, user_content):
|
56 |
+
OPEN_AI_CLIENT = AI_Client
|
57 |
+
messages = [{"role": "system", "content": system_content}, {"role": "user", "content": user_content}]
|
58 |
+
request_payload = {
|
59 |
+
"model": "gpt-4-turbo",
|
60 |
+
"messages": messages,
|
61 |
+
"max_tokens": 512,
|
62 |
+
"temperature": 0.9,
|
63 |
+
"stream": False,
|
64 |
+
}
|
65 |
+
try:
|
66 |
+
response = OPEN_AI_CLIENT.chat.completions.create(**request_payload)
|
67 |
+
return response.choices[0].message.content.strip()
|
68 |
+
except Exception as e:
|
69 |
+
print(f"OpenAI failed: {e}")
|
70 |
+
raise # Optionally re-raise the exception if fallback is not desired
|
71 |
+
|
72 |
+
def send_bedrock_request(self, AI_Client, system_content, user_content):
|
73 |
+
BEDROCK_CLIENT = AI_Client
|
74 |
+
#REDROCK
|
75 |
+
messages = [
|
76 |
+
{"role": "user", "content": user_content}
|
77 |
+
]
|
78 |
+
model_id = "anthropic.claude-3-sonnet-20240229-v1:0"
|
79 |
+
# model_id = "anthropic.claude-3-haiku-20240307-v1:0"
|
80 |
+
kwargs = {
|
81 |
+
"modelId": model_id,
|
82 |
+
"contentType": "application/json",
|
83 |
+
"accept": "application/json",
|
84 |
+
"body": json.dumps({
|
85 |
+
"anthropic_version": "bedrock-2023-05-31",
|
86 |
+
"max_tokens": 4000,
|
87 |
+
"system": system_content,
|
88 |
+
"messages": messages
|
89 |
+
})
|
90 |
+
}
|
91 |
+
|
92 |
+
try:
|
93 |
+
response = response = BEDROCK_CLIENT.invoke_model(**kwargs)
|
94 |
+
response_body = json.loads(response.get('body').read())
|
95 |
+
response_content = response_body.get('content')[0].get('text')
|
96 |
+
return response_content
|
97 |
+
except Exception as e:
|
98 |
+
print(f"Bedrock failed: {e}")
|
99 |
+
raise
|
100 |
+
|
101 |
def _prepare_context(self, context):
|
102 |
context_json = json.loads(context)
|
103 |
processed_context = ""
|
|
|
486 |
"""
|
487 |
return exit_ticket_prompt
|
488 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
489 |
def build_user_content(self):
|
490 |
if self.content_type == 'worksheet':
|
491 |
specific_feature_text = f"理論模型: {self.specific_feature}"
|