Spaces:
Sleeping
Sleeping
feedback_with_ai
Browse files
app.py
CHANGED
@@ -2100,55 +2100,26 @@ def get_instructions(content_subject, content_grade, key_moments, socratic_mode=
|
|
2100 |
"""
|
2101 |
return instructions
|
2102 |
|
2103 |
-
def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, questions_answers_json,
|
2104 |
verify_password(password)
|
2105 |
|
2106 |
print("=====user_data=====")
|
2107 |
print(f"user_data: {user_data}")
|
2108 |
|
2109 |
-
|
2110 |
-
for qa in questions_answers_json:
|
2111 |
-
question = qa["question"]
|
2112 |
-
answer = qa["answer"]
|
2113 |
-
if user_message == question and answer != "":
|
2114 |
-
print("=== in questions_answers_json==")
|
2115 |
-
print(f"question: {question}")
|
2116 |
-
print(f"answer: {answer}")
|
2117 |
-
# 更新聊天历史
|
2118 |
-
new_chat_history = (user_message, answer)
|
2119 |
-
if chat_history is None:
|
2120 |
-
chat_history = [new_chat_history]
|
2121 |
-
else:
|
2122 |
-
chat_history.append(new_chat_history)
|
2123 |
-
|
2124 |
-
# 等待 3 秒
|
2125 |
-
time.sleep(3)
|
2126 |
-
send_count = len(chat_history)-1
|
2127 |
-
if len(chat_history) > CHAT_LIMIT:
|
2128 |
-
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2129 |
-
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2130 |
-
else:
|
2131 |
-
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2132 |
-
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2133 |
|
2134 |
-
|
|
|
|
|
|
|
|
|
|
|
2135 |
|
2136 |
-
|
2137 |
-
error_msg = "此次對話超過上限(對話一輪10次)"
|
2138 |
-
raise gr.Error(error_msg)
|
2139 |
|
2140 |
if not ai_name in ["foxcat", "lili", "maimai"]:
|
2141 |
ai_name = "foxcat"
|
2142 |
|
2143 |
-
# if ai_name == "jutor":
|
2144 |
-
# ai_client = ""
|
2145 |
-
# elif ai_name == "claude3":
|
2146 |
-
# ai_client = BEDROCK_CLIENT
|
2147 |
-
# elif ai_name == "groq":
|
2148 |
-
# ai_client = GROQ_CLIENT
|
2149 |
-
# else:
|
2150 |
-
# ai_client = ""
|
2151 |
-
|
2152 |
ai_name_clients_model = {
|
2153 |
"foxcat": {
|
2154 |
"ai_name": "foxcat",
|
@@ -2160,11 +2131,6 @@ def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_mo
|
|
2160 |
"ai_client": BEDROCK_CLIENT,
|
2161 |
"ai_model_name": "claude3",
|
2162 |
},
|
2163 |
-
# "maimai": {
|
2164 |
-
# "ai_name": "maimai",
|
2165 |
-
# "ai_client": OPEN_AI_CLIENT,
|
2166 |
-
# "ai_model_name": "openai",
|
2167 |
-
# }
|
2168 |
"maimai": {
|
2169 |
"ai_name": "maimai",
|
2170 |
"ai_client": GROQ_CLIENT,
|
@@ -2213,28 +2179,75 @@ def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_mo
|
|
2213 |
|
2214 |
try:
|
2215 |
# 更新聊天历史
|
2216 |
-
|
2217 |
-
|
2218 |
-
chat_history = [new_chat_history]
|
2219 |
-
else:
|
2220 |
-
chat_history.append(new_chat_history)
|
2221 |
-
|
2222 |
-
send_count = len(chat_history)-1
|
2223 |
-
if len(chat_history) > CHAT_LIMIT:
|
2224 |
-
send_btn_value = f"對話已達上限 ({send_count}/{CHAT_LIMIT})"
|
2225 |
-
send_btn = gr.update(value=send_btn_value, interactive=False)
|
2226 |
-
else:
|
2227 |
-
send_btn_value = f"發送 ({send_count}/{CHAT_LIMIT})"
|
2228 |
-
send_btn = gr.update(value=send_btn_value, interactive=True)
|
2229 |
|
2230 |
# 返回聊天历史和空字符串清空输入框
|
2231 |
-
return "", chat_history,
|
2232 |
except Exception as e:
|
2233 |
# 处理错误情况
|
2234 |
print(f"Error: {e}")
|
2235 |
return "请求失败,请稍后再试!", chat_history
|
2236 |
|
2237 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2238 |
verify_password(password)
|
2239 |
|
2240 |
print("=====user_data=====")
|
@@ -2979,7 +2992,8 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
2979 |
ai_chatbot_audio_input = gr.Audio(sources=["microphone"], type="filepath", max_length=60, label="語音輸入")
|
2980 |
with gr.Row():
|
2981 |
ai_msg = gr.Textbox(label="訊息輸入",scale=3)
|
2982 |
-
ai_send_button = gr.Button("送出", variant="primary",scale=1)
|
|
|
2983 |
with gr.Tab("文章模式"):
|
2984 |
with gr.Row():
|
2985 |
reading_passage = gr.Markdown(show_label=False, latex_delimiters = [{"left": "$", "right": "$", "display": False}])
|
@@ -3221,7 +3235,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3221 |
# OPENAI ASSISTANT CHATBOT 模式
|
3222 |
send_button.click(
|
3223 |
chat_with_opan_ai_assistant,
|
3224 |
-
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, questions_answers_json,
|
3225 |
outputs=[msg, chatbot, thread_id, send_button, send_feedback_btn],
|
3226 |
scroll_to_output=True
|
3227 |
)
|
@@ -3246,7 +3260,7 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3246 |
)
|
3247 |
question_buttons = [btn_1, btn_2, btn_3]
|
3248 |
for question_btn in question_buttons:
|
3249 |
-
inputs_list = [password, video_id, user_data, thread_id, trascript_state, key_moments, question_btn, chatbot, content_subject, content_grade, questions_answers_json,
|
3250 |
outputs_list = [msg, chatbot, thread_id, send_button, send_feedback_btn]
|
3251 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_opan_ai_assistant)
|
3252 |
|
@@ -3260,15 +3274,21 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue=gr.themes.colors.orange, seconda
|
|
3260 |
# 其他精靈 ai_chatbot 模式
|
3261 |
ai_send_button.click(
|
3262 |
chat_with_ai,
|
3263 |
-
inputs=[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_msg, ai_chatbot, content_subject, content_grade, questions_answers_json,
|
3264 |
-
outputs=[ai_msg, ai_chatbot, ai_send_button],
|
|
|
|
|
|
|
|
|
|
|
|
|
3265 |
scroll_to_output=True
|
3266 |
)
|
3267 |
# 其他精靈 ai_chatbot 连接 QA 按钮点击事件
|
3268 |
ai_chatbot_question_buttons = [ai_chatbot_question_1, ai_chatbot_question_2, ai_chatbot_question_3]
|
3269 |
for question_btn in ai_chatbot_question_buttons:
|
3270 |
-
inputs_list = [ai_name, password, video_id, user_data, trascript_state, key_moments, question_btn, ai_chatbot, content_subject, content_grade, questions_answers_json,
|
3271 |
-
outputs_list = [ai_msg, ai_chatbot, ai_send_button]
|
3272 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_ai)
|
3273 |
|
3274 |
ai_chatbot_audio_input.change(
|
|
|
2100 |
"""
|
2101 |
return instructions
|
2102 |
|
2103 |
+
def chat_with_ai(ai_name, password, video_id, user_data, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, questions_answers_json, socratic_mode=False):
|
2104 |
verify_password(password)
|
2105 |
|
2106 |
print("=====user_data=====")
|
2107 |
print(f"user_data: {user_data}")
|
2108 |
|
2109 |
+
verify_message_length(user_message, max_length=1500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2110 |
|
2111 |
+
is_questions_answers_exists, question_message, answer_message = check_questions_answers(user_message, questions_answers_json)
|
2112 |
+
if is_questions_answers_exists:
|
2113 |
+
chat_history = update_chat_history(question_message, answer_message, chat_history)
|
2114 |
+
send_btn_update, send_feedback_btn_update = update_send_and_feedback_buttons(chat_history, CHAT_LIMIT)
|
2115 |
+
time.sleep(3)
|
2116 |
+
return "", chat_history, send_btn_update, send_feedback_btn_update
|
2117 |
|
2118 |
+
verify_chat_limit(chat_history, CHAT_LIMIT)
|
|
|
|
|
2119 |
|
2120 |
if not ai_name in ["foxcat", "lili", "maimai"]:
|
2121 |
ai_name = "foxcat"
|
2122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2123 |
ai_name_clients_model = {
|
2124 |
"foxcat": {
|
2125 |
"ai_name": "foxcat",
|
|
|
2131 |
"ai_client": BEDROCK_CLIENT,
|
2132 |
"ai_model_name": "claude3",
|
2133 |
},
|
|
|
|
|
|
|
|
|
|
|
2134 |
"maimai": {
|
2135 |
"ai_name": "maimai",
|
2136 |
"ai_client": GROQ_CLIENT,
|
|
|
2179 |
|
2180 |
try:
|
2181 |
# 更新聊天历史
|
2182 |
+
chat_history = update_chat_history(user_message, response_completion, chat_history)
|
2183 |
+
send_btn_update, send_feedback_btn_update = update_send_and_feedback_buttons(chat_history, CHAT_LIMIT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2184 |
|
2185 |
# 返回聊天历史和空字符串清空输入框
|
2186 |
+
return "", chat_history, send_btn_update, send_feedback_btn_update
|
2187 |
except Exception as e:
|
2188 |
# 处理错误情况
|
2189 |
print(f"Error: {e}")
|
2190 |
return "请求失败,请稍后再试!", chat_history
|
2191 |
|
2192 |
+
def feedback_with_ai(chat_history):
|
2193 |
+
# prompt: 請依據以上的對話(chat_history),總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議
|
2194 |
+
system_content = """
|
2195 |
+
你是一個擅長引導問答素養的老師,user 為學生的提問跟回答,請精讀對話過程,針對 user 給予回饋就好,根據以下 Rule:
|
2196 |
+
- 請使用繁體中文 zh-TW 總結 user 的提問力,並給予是否有問對問題的回饋和建議
|
2197 |
+
- 不採計【預設提問】的問題,如果 user 的提問都來自【預設提問】,表達用戶善於使用系統,請給予回饋並鼓勵 user 親自提問更具體的問題
|
2198 |
+
- 如果用戶提問都相當簡短,甚至就是一個字或都是一個數字(像是 user: 1, user:2),請給予回饋並建議 user 提問更具體的問題
|
2199 |
+
- 如果用戶提問內容只有符號或是亂碼,像是?,!, ..., 3bhwbqhfw2vve2 等,請給予回饋並建議 user 提問更具體的問題
|
2200 |
+
- 如果用戶提問內容有色情、暴力、仇恨、不當言論等,請給予嚴厲的回饋並建議 user 提問更具體的問題
|
2201 |
+
- 並用第二人稱「你」來代表 user
|
2202 |
+
- 請禮貌,並給予鼓勵
|
2203 |
+
"""
|
2204 |
+
chat_history_conversation = ""
|
2205 |
+
# 標註 user and assistant as string
|
2206 |
+
# chat_history 第一組不採計
|
2207 |
+
for chat in chat_history[1:]:
|
2208 |
+
user_message = chat[0]
|
2209 |
+
assistant_message = chat[1]
|
2210 |
+
chat_history_conversation += f"User: {user_message}\nAssistant: {assistant_message}\n"
|
2211 |
+
|
2212 |
+
feedback_request_message = "請依據以上的對話,總結我的「提問力」,並給予我是否有「問對問題」的回饋和建議"
|
2213 |
+
user_content = f"""conversation: {chat_history_conversation}
|
2214 |
+
{feedback_request_message}
|
2215 |
+
最後根據提問力表現,給予提問建議、提問表現,並用 emoji 來表示評分:
|
2216 |
+
🟢:(表現很好的回饋,給予正向肯定)
|
2217 |
+
🟡:(還可以加油的的回饋,給予明確的建議)
|
2218 |
+
🔴:(非常不懂提問的回饋,給予鼓勵並給出明確示範)
|
2219 |
+
|
2220 |
+
example:
|
2221 |
+
另一方面,你表達「我不想學了」這個情感,其實也是一種重要的反饋。這顯示你可能感到挫折或疲倦。在這種情況下,表達出你的感受是好的,但如果能具體說明是什麼讓你感到這樣,或是有什麼具體的學習障礙,會更有助於找到解決方案。
|
2222 |
+
給予你的建議是,嘗試在提問時更明確一些,這樣不僅能幫助你獲得更好的學習支持,也能提高你的問題解決技巧。
|
2223 |
+
......
|
2224 |
+
提問建議:在提問時,試著具體並清晰地表達你的需求和疑惑,這樣能更有效地得到幫助。
|
2225 |
+
提問表現:【🟡】加油,持續練習,你的提問力會越來越好!
|
2226 |
+
"""
|
2227 |
+
|
2228 |
+
|
2229 |
+
client = OPEN_AI_CLIENT
|
2230 |
+
model_name = "gpt-4-turbo"
|
2231 |
+
response_text = handle_conversation_by_open_ai_chat_completions(client, model_name, user_content, system_content)
|
2232 |
+
chat_history = update_chat_history(feedback_request_message, response_text, chat_history)
|
2233 |
+
feedback_btn_update = gr.update(value="已回饋", interactive=False, variant="secondary")
|
2234 |
+
|
2235 |
+
return chat_history, feedback_btn_update
|
2236 |
+
|
2237 |
+
def handle_conversation_by_open_ai_chat_completions(client, model_name, user_content, system_content):
|
2238 |
+
response = client.chat.completions.create(
|
2239 |
+
model=model_name,
|
2240 |
+
messages=[
|
2241 |
+
{"role": "system", "content": system_content},
|
2242 |
+
{"role": "user", "content": user_content}
|
2243 |
+
],
|
2244 |
+
max_tokens=4000,
|
2245 |
+
)
|
2246 |
+
response_text = response.choices[0].message.content.strip()
|
2247 |
+
return response_text
|
2248 |
+
|
2249 |
+
# open ai assistant
|
2250 |
+
def chat_with_opan_ai_assistant(password, youtube_id, user_data, thread_id, trascript_state, key_moments, user_message, chat_history, content_subject, content_grade, questions_answers_json, socratic_mode=False):
|
2251 |
verify_password(password)
|
2252 |
|
2253 |
print("=====user_data=====")
|
|
|
2992 |
ai_chatbot_audio_input = gr.Audio(sources=["microphone"], type="filepath", max_length=60, label="語音輸入")
|
2993 |
with gr.Row():
|
2994 |
ai_msg = gr.Textbox(label="訊息輸入",scale=3)
|
2995 |
+
ai_send_button = gr.Button("送出", variant="primary",scale=1)
|
2996 |
+
ai_send_feedback_btn = gr.Button("提問力回饋", variant="primary", scale=1, visible=False)
|
2997 |
with gr.Tab("文章模式"):
|
2998 |
with gr.Row():
|
2999 |
reading_passage = gr.Markdown(show_label=False, latex_delimiters = [{"left": "$", "right": "$", "display": False}])
|
|
|
3235 |
# OPENAI ASSISTANT CHATBOT 模式
|
3236 |
send_button.click(
|
3237 |
chat_with_opan_ai_assistant,
|
3238 |
+
inputs=[password, video_id, user_data, thread_id, trascript_state, key_moments, msg, chatbot, content_subject, content_grade, questions_answers_json, socratic_mode_btn],
|
3239 |
outputs=[msg, chatbot, thread_id, send_button, send_feedback_btn],
|
3240 |
scroll_to_output=True
|
3241 |
)
|
|
|
3260 |
)
|
3261 |
question_buttons = [btn_1, btn_2, btn_3]
|
3262 |
for question_btn in question_buttons:
|
3263 |
+
inputs_list = [password, video_id, user_data, thread_id, trascript_state, key_moments, question_btn, chatbot, content_subject, content_grade, questions_answers_json, socratic_mode_btn]
|
3264 |
outputs_list = [msg, chatbot, thread_id, send_button, send_feedback_btn]
|
3265 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_opan_ai_assistant)
|
3266 |
|
|
|
3274 |
# 其他精靈 ai_chatbot 模式
|
3275 |
ai_send_button.click(
|
3276 |
chat_with_ai,
|
3277 |
+
inputs=[ai_name, password, video_id, user_data, trascript_state, key_moments, ai_msg, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn],
|
3278 |
+
outputs=[ai_msg, ai_chatbot, ai_send_button, ai_send_feedback_btn],
|
3279 |
+
scroll_to_output=True
|
3280 |
+
)
|
3281 |
+
ai_send_feedback_btn.click(
|
3282 |
+
feedback_with_ai,
|
3283 |
+
inputs=[ai_chatbot],
|
3284 |
+
outputs=[ai_chatbot, ai_send_feedback_btn],
|
3285 |
scroll_to_output=True
|
3286 |
)
|
3287 |
# 其他精靈 ai_chatbot 连接 QA 按钮点击事件
|
3288 |
ai_chatbot_question_buttons = [ai_chatbot_question_1, ai_chatbot_question_2, ai_chatbot_question_3]
|
3289 |
for question_btn in ai_chatbot_question_buttons:
|
3290 |
+
inputs_list = [ai_name, password, video_id, user_data, trascript_state, key_moments, question_btn, ai_chatbot, content_subject, content_grade, questions_answers_json, ai_chatbot_socratic_mode_btn]
|
3291 |
+
outputs_list = [ai_msg, ai_chatbot, ai_send_button, ai_send_feedback_btn]
|
3292 |
setup_question_button_click(question_btn, inputs_list, outputs_list, chat_with_ai)
|
3293 |
|
3294 |
ai_chatbot_audio_input.change(
|