Commit
Β·
72123d0
1
Parent(s):
7df763f
feat: Enhance chat message handling in app.py with improved session ID validation and error handling. Update SupabaseService to log session creation attempts and handle exceptions gracefully. Ensure user and AI messages are only saved when session ID is present.
Browse files- app.py +60 -20
- services/supabase_service.py +12 -5
app.py
CHANGED
|
@@ -394,6 +394,9 @@ def handle_chat_message(message, chat_history, session_id, student_id, scenario_
|
|
| 394 |
if not message or not message.strip():
|
| 395 |
return chat_history, ""
|
| 396 |
|
|
|
|
|
|
|
|
|
|
| 397 |
# μλ리μ€κ° μ νλμ§ μμ κ²½μ°
|
| 398 |
if not scenario_id or scenario_id == "":
|
| 399 |
warning_msg = """
|
|
@@ -422,8 +425,15 @@ EMR μ 보 μ‘°ν νμμ μλ리μ€λ₯Ό λ€μ μ νν΄μ£ΌμΈμ.
|
|
| 422 |
"""
|
| 423 |
return chat_history + [[message, error_msg]], ""
|
| 424 |
|
| 425 |
-
# μ¬μ©μ λ©μμ§ μ μ₯
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
|
| 428 |
# μλλ¦¬μ€ λ° νμ λ°μ΄ν° μ€λΉ
|
| 429 |
scenario_data = {
|
|
@@ -456,8 +466,15 @@ EMR μ 보 μ‘°ν νμμ μλ리μ€λ₯Ό λ€μ μ νν΄μ£ΌμΈμ.
|
|
| 456 |
# Gemini AI νΌλλ°± μμ±
|
| 457 |
ai_response = evaluator.chat_feedback(message, history_for_gemini, scenario_data, patient_data)
|
| 458 |
|
| 459 |
-
# AI μλ΅ μ μ₯
|
| 460 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 461 |
|
| 462 |
# μ±ν
νμ€ν 리 μ
λ°μ΄νΈ
|
| 463 |
updated_history = chat_history + [[message, ai_response]]
|
|
@@ -779,12 +796,19 @@ with gr.Blocks(title="κ°νΈ μΈμμΈκ³ κ΅μ‘ νλ«νΌ", theme=gr.themes.Sof
|
|
| 779 |
|
| 780 |
if scenario:
|
| 781 |
# μ μ±ν
μΈμ
μμ±
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 788 |
|
| 789 |
return emr_html, "", scenario_id, "**νμ¬ μλ리μ€**: μ νλμ§ μμ"
|
| 790 |
|
|
@@ -819,10 +843,15 @@ with gr.Blocks(title="κ°νΈ μΈμμΈκ³ κ΅μ‘ νλ«νΌ", theme=gr.themes.Sof
|
|
| 819 |
# λν μ΄κΈ°ν
|
| 820 |
def clear_chat(student_id, scenario_id):
|
| 821 |
"""λν μ΄κΈ°ν λ° μ μΈμ
μμ±"""
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 826 |
|
| 827 |
clear_btn.click(
|
| 828 |
fn=clear_chat,
|
|
@@ -864,8 +893,9 @@ with gr.Blocks(title="κ°νΈ μΈμμΈκ³ κ΅μ‘ νλ«νΌ", theme=gr.themes.Sof
|
|
| 864 |
"""μ± μ΄κΈ° λ‘λ μ μ€ν"""
|
| 865 |
default_scenario = "S001_D0_ER_WARD"
|
| 866 |
|
| 867 |
-
# student_id_stateλ₯Ό μ΄κΈ° IDλ‘ μ€μ
|
| 868 |
-
|
|
|
|
| 869 |
|
| 870 |
# Supabase μλΉμ€κ° μμΌλ©΄ μλλ¦¬μ€ λ°μ΄ν° λ‘λ
|
| 871 |
if supabase_service:
|
|
@@ -874,16 +904,26 @@ with gr.Blocks(title="κ°νΈ μΈμμΈκ³ κ΅μ‘ νλ«νΌ", theme=gr.themes.Sof
|
|
| 874 |
if scenario:
|
| 875 |
# κΈ°λ³Έ μλλ¦¬μ€ μ 보 νμ
|
| 876 |
scenario_info = f"**νμ¬ μλ리μ€**: {scenario.title}"
|
| 877 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 878 |
else:
|
| 879 |
-
return emr_html, default_scenario, "**νμ¬ μλ리μ€**: μ νλμ§ μμ",
|
| 880 |
else:
|
| 881 |
-
return "", default_scenario, "**νμ¬ μλ리μ€**: μ νλμ§ μμ",
|
| 882 |
|
| 883 |
# μ΄κΈ° λ‘λ
|
| 884 |
app.load(
|
| 885 |
fn=initialize_app,
|
| 886 |
-
outputs=[emr_display, scenario_id_state, current_scenario_display, student_id_state, user_role_state]
|
| 887 |
)
|
| 888 |
|
| 889 |
|
|
|
|
| 394 |
if not message or not message.strip():
|
| 395 |
return chat_history, ""
|
| 396 |
|
| 397 |
+
# μΈμ
ID νμΈ
|
| 398 |
+
print(f"π¬ μ±ν
λ©μμ§ μ²λ¦¬: session_id={session_id}, student_id={student_id}, scenario_id={scenario_id}")
|
| 399 |
+
|
| 400 |
# μλ리μ€κ° μ νλμ§ μμ κ²½μ°
|
| 401 |
if not scenario_id or scenario_id == "":
|
| 402 |
warning_msg = """
|
|
|
|
| 425 |
"""
|
| 426 |
return chat_history + [[message, error_msg]], ""
|
| 427 |
|
| 428 |
+
# μ¬μ©μ λ©μμ§ μ μ₯ (session_idκ° μλ κ²½μ°μλ§)
|
| 429 |
+
if session_id:
|
| 430 |
+
try:
|
| 431 |
+
supabase_service.save_chat_message(session_id, student_id, scenario_id, "user", message)
|
| 432 |
+
print(f"β
μ¬μ©μ λ©μμ§ μ μ₯ μλ£: session_id={session_id}")
|
| 433 |
+
except Exception as e:
|
| 434 |
+
print(f"β οΈ μ¬μ©μ λ©μμ§ μ μ₯ μ€ν¨: {e}")
|
| 435 |
+
else:
|
| 436 |
+
print(f"β οΈ session_idκ° λΉμ΄μμ΄ λ©μμ§ μ μ₯ μ€ν¨")
|
| 437 |
|
| 438 |
# μλλ¦¬μ€ λ° νμ λ°μ΄ν° μ€λΉ
|
| 439 |
scenario_data = {
|
|
|
|
| 466 |
# Gemini AI νΌλλ°± μμ±
|
| 467 |
ai_response = evaluator.chat_feedback(message, history_for_gemini, scenario_data, patient_data)
|
| 468 |
|
| 469 |
+
# AI μλ΅ μ μ₯ (session_idκ° μλ κ²½μ°μλ§)
|
| 470 |
+
if session_id:
|
| 471 |
+
try:
|
| 472 |
+
supabase_service.save_chat_message(session_id, student_id, scenario_id, "assistant", ai_response)
|
| 473 |
+
print(f"β
AI μλ΅ μ μ₯ μλ£: session_id={session_id}")
|
| 474 |
+
except Exception as e:
|
| 475 |
+
print(f"β οΈ AI μλ΅ μ μ₯ μ€ν¨: {e}")
|
| 476 |
+
else:
|
| 477 |
+
print(f"β οΈ session_idκ° λΉμ΄μμ΄ AI μλ΅ μ μ₯ μ€ν¨")
|
| 478 |
|
| 479 |
# μ±ν
νμ€ν 리 μ
λ°μ΄νΈ
|
| 480 |
updated_history = chat_history + [[message, ai_response]]
|
|
|
|
| 796 |
|
| 797 |
if scenario:
|
| 798 |
# μ μ±ν
μΈμ
μμ±
|
| 799 |
+
try:
|
| 800 |
+
supabase_service = SupabaseService()
|
| 801 |
+
new_session_id = supabase_service.create_chat_session(student_id, scenario_id)
|
| 802 |
+
print(f"π μλλ¦¬μ€ μ ν μ μΈμ
μμ±: session_id={new_session_id}")
|
| 803 |
+
|
| 804 |
+
scenario_info = f"**νμ¬ μλ리μ€**: {scenario.title}"
|
| 805 |
+
|
| 806 |
+
return emr_html, new_session_id, scenario_id, scenario_info
|
| 807 |
+
except Exception as e:
|
| 808 |
+
print(f"β οΈ μΈμ
μμ± μ€ν¨: {e}")
|
| 809 |
+
import traceback
|
| 810 |
+
traceback.print_exc()
|
| 811 |
+
return emr_html, "", scenario_id, "**νμ¬ μλ리μ€**: μ νλμ§ μμ"
|
| 812 |
|
| 813 |
return emr_html, "", scenario_id, "**νμ¬ μλ리μ€**: μ νλμ§ μμ"
|
| 814 |
|
|
|
|
| 843 |
# λν μ΄κΈ°ν
|
| 844 |
def clear_chat(student_id, scenario_id):
|
| 845 |
"""λν μ΄κΈ°ν λ° μ μΈμ
μμ±"""
|
| 846 |
+
try:
|
| 847 |
+
supabase_service = SupabaseService()
|
| 848 |
+
new_session_id = supabase_service.create_chat_session(student_id, scenario_id)
|
| 849 |
+
print(f"π λν μ΄κΈ°ν - μ μΈμ
μμ±: {new_session_id}")
|
| 850 |
+
|
| 851 |
+
return [], new_session_id
|
| 852 |
+
except Exception as e:
|
| 853 |
+
print(f"β οΈ λν μ΄κΈ°ν μ€ μΈμ
μμ± μ€ν¨: {e}")
|
| 854 |
+
return [], ""
|
| 855 |
|
| 856 |
clear_btn.click(
|
| 857 |
fn=clear_chat,
|
|
|
|
| 893 |
"""μ± μ΄κΈ° λ‘λ μ μ€ν"""
|
| 894 |
default_scenario = "S001_D0_ER_WARD"
|
| 895 |
|
| 896 |
+
# student_id_stateλ₯Ό μ΄κΈ° IDλ‘ μ€μ (λͺ¨λ λ 벨 λ³μ μ¬μ©)
|
| 897 |
+
current_student_id = initial_student_id
|
| 898 |
+
initial_session_id = ""
|
| 899 |
|
| 900 |
# Supabase μλΉμ€κ° μμΌλ©΄ μλλ¦¬μ€ λ°μ΄ν° λ‘λ
|
| 901 |
if supabase_service:
|
|
|
|
| 904 |
if scenario:
|
| 905 |
# κΈ°λ³Έ μλλ¦¬μ€ μ 보 νμ
|
| 906 |
scenario_info = f"**νμ¬ μλ리μ€**: {scenario.title}"
|
| 907 |
+
|
| 908 |
+
# μ΄κΈ° μΈμ
μμ±
|
| 909 |
+
try:
|
| 910 |
+
initial_session_id = supabase_service.create_chat_session(current_student_id, default_scenario)
|
| 911 |
+
print(f"β
μ΄κΈ° μΈμ
μμ± μλ£: {initial_session_id}")
|
| 912 |
+
except Exception as e:
|
| 913 |
+
print(f"β οΈ μ΄κΈ° μΈμ
μμ± μ€ν¨: {e}")
|
| 914 |
+
import traceback
|
| 915 |
+
traceback.print_exc()
|
| 916 |
+
|
| 917 |
+
return emr_html, default_scenario, scenario_info, current_student_id, "student", initial_session_id
|
| 918 |
else:
|
| 919 |
+
return emr_html, default_scenario, "**νμ¬ μλ리μ€**: μ νλμ§ μμ", current_student_id, "student", ""
|
| 920 |
else:
|
| 921 |
+
return "", default_scenario, "**νμ¬ μλ리μ€**: μ νλμ§ μμ", current_student_id, "student", ""
|
| 922 |
|
| 923 |
# μ΄κΈ° λ‘λ
|
| 924 |
app.load(
|
| 925 |
fn=initialize_app,
|
| 926 |
+
outputs=[emr_display, scenario_id_state, current_scenario_display, student_id_state, user_role_state, session_id_state]
|
| 927 |
)
|
| 928 |
|
| 929 |
|
services/supabase_service.py
CHANGED
|
@@ -518,19 +518,26 @@ class SupabaseService:
|
|
| 518 |
'session_id': session_id,
|
| 519 |
'student_id': student_id,
|
| 520 |
'scenario_id': scenario_id,
|
| 521 |
-
'status': 'active'
|
| 522 |
-
|
| 523 |
}
|
| 524 |
|
|
|
|
|
|
|
| 525 |
# μΈμ
ν
μ΄λΈμ΄ μλ€λ©΄ μ μ₯ (μμΌλ©΄ κ·Έλ₯ IDλ§ λ°ν)
|
| 526 |
try:
|
| 527 |
-
self.client.table('sessions').insert(session_data).execute()
|
| 528 |
-
|
|
|
|
| 529 |
# sessions ν
μ΄λΈμ΄ μμ κ²½μ° λ¬΄μ
|
| 530 |
-
|
|
|
|
|
|
|
| 531 |
|
| 532 |
except Exception as e:
|
| 533 |
print(f"β οΈ μΈμ
μ μ₯ μ€ν¨ (IDλ§ μ¬μ©): {e}")
|
|
|
|
|
|
|
| 534 |
|
| 535 |
return session_id
|
| 536 |
|
|
|
|
| 518 |
'session_id': session_id,
|
| 519 |
'student_id': student_id,
|
| 520 |
'scenario_id': scenario_id,
|
| 521 |
+
'status': 'active'
|
| 522 |
+
# started_atμ ν
μ΄λΈ DEFAULT NOW()λ‘ μλ μ€μ λ¨
|
| 523 |
}
|
| 524 |
|
| 525 |
+
print(f"π μΈμ
μμ± μλ: session_id={session_id}, student_id={student_id}, scenario_id={scenario_id}")
|
| 526 |
+
|
| 527 |
# μΈμ
ν
μ΄λΈμ΄ μλ€λ©΄ μ μ₯ (μμΌλ©΄ κ·Έλ₯ IDλ§ λ°ν)
|
| 528 |
try:
|
| 529 |
+
result = self.client.table('sessions').insert(session_data).execute()
|
| 530 |
+
print(f"β
μΈμ
μ μ₯ μ±κ³΅: {result.data}")
|
| 531 |
+
except Exception as insert_error:
|
| 532 |
# sessions ν
μ΄λΈμ΄ μμ κ²½μ° λ¬΄μ
|
| 533 |
+
print(f"β οΈ sessions ν
μ΄λΈ insert μ€ν¨ (κ³μ μ§ν): {insert_error}")
|
| 534 |
+
import traceback
|
| 535 |
+
traceback.print_exc()
|
| 536 |
|
| 537 |
except Exception as e:
|
| 538 |
print(f"β οΈ μΈμ
μ μ₯ μ€ν¨ (IDλ§ μ¬μ©): {e}")
|
| 539 |
+
import traceback
|
| 540 |
+
traceback.print_exc()
|
| 541 |
|
| 542 |
return session_id
|
| 543 |
|