Rathapoom commited on
Commit
e8b6e00
·
verified ·
1 Parent(s): a5ebfa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -45
app.py CHANGED
@@ -416,34 +416,26 @@ def provide_feedback(text: str, level: str) -> Dict[str, str]:
416
  response_text = response.choices[0].message.content.strip()
417
 
418
  try:
419
- # Try to parse the JSON response
 
 
 
420
  feedback_data = json.loads(response_text)
421
 
422
- # Validate the required fields
423
- required_fields = ['feedback', 'corrected', 'has_errors']
424
- if not all(field in feedback_data for field in required_fields):
 
425
  raise ValueError("Missing required fields in response")
426
-
427
- return feedback_data
428
-
429
  except json.JSONDecodeError as json_err:
430
- # If JSON parsing fails, try to extract information manually
431
- st.error(f"Debug - Response received: {response_text}")
432
-
433
- # Return a safe fallback response
434
- return {
435
- "feedback": "⚠️ คำแนะนำ: ระบบไม่สามารถวิเคราะห์ประโยคได้อย่างถูกต้อง กรุณาลองใหม่อีกครั้ง",
436
- "corrected": text,
437
- "has_errors": False
438
- }
439
 
440
  except Exception as e:
441
- st.error(f"Debug - Error type: {type(e).__name__}")
442
- st.error(f"Debug - Error message: {str(e)}")
443
-
444
- # Return a safe fallback response
445
  return {
446
- "feedback": "⚠️ ขออภัย ระบบไม่สามารถวิเคราะห์ประโยคได้ในขณะนี้",
447
  "corrected": text,
448
  "has_errors": False
449
  }
@@ -560,8 +552,7 @@ with col1:
560
  )
561
 
562
  with col2:
563
- # Feedback Display
564
- if st.session_state.feedback and st.session_state.feedback.get('has_errors', False):
565
  st.markdown("""
566
  <div class="thai-eng">
567
  <div class="thai">📝 คำแนะนำจากครู</div>
@@ -569,9 +560,8 @@ with col2:
569
  </div>
570
  """, unsafe_allow_html=True)
571
 
572
- # แสดง feedback ในรูปแบบที่อ่านง่าย
573
- feedback_container = st.container()
574
- with feedback_container:
575
  st.markdown(f"""
576
  <div style='background-color: #f0f2f6;
577
  padding: 15px;
@@ -580,18 +570,18 @@ with col2:
580
  margin: 5px 0;
581
  font-family: "Sarabun", sans-serif;'>
582
  <p style='color: #1e88e5; margin-bottom: 10px;'>
583
- {st.session_state.feedback['feedback']}
584
  </p>
585
  <p style='color: #666; font-size: 0.9em;'>
586
  ประโยคที่ถูกต้อง:<br/>
587
  <span style='color: #4CAF50; font-weight: bold;'>
588
- {st.session_state.feedback['corrected']}
589
  </span>
590
  </p>
591
  </div>
592
  """, unsafe_allow_html=True)
593
 
594
- # แสดงปุ่มแก้ไขเพียงปุ่มเดียว
595
  if st.button("✍️ แก้ไขประโยคให้ถูกต้อง", key="correct_button"):
596
  last_user_entry_idx = next(
597
  (i for i, entry in reversed(list(enumerate(st.session_state.story)))
@@ -601,24 +591,22 @@ with col2:
601
  if last_user_entry_idx is not None:
602
  apply_correction(
603
  last_user_entry_idx,
604
- st.session_state.feedback['corrected']
605
  )
606
  st.rerun()
607
-
608
- elif st.session_state.feedback:
609
- # กรณีไม่มีข้อผิดพลาด
610
- st.markdown(f"""
611
- <div style='background-color: #f0f2f6;
612
- padding: 15px;
613
- border-radius: 8px;
614
- border-left: 4px solid #4CAF50;
615
- margin: 5px 0;
616
- font-family: "Sarabun", sans-serif;'>
617
- <p style='color: #1e88e5;'>
618
- ✨ {st.session_state.feedback['feedback']}
619
- </p>
620
- </div>
621
- """, unsafe_allow_html=True)
622
 
623
  # Help and Suggestions Box
624
  st.markdown("""
 
416
  response_text = response.choices[0].message.content.strip()
417
 
418
  try:
419
+ # แสดง debug info เพื่อตรวจสอบ response
420
+ st.write("Debug - Response received:", response_text)
421
+
422
+ # Parse JSON response
423
  feedback_data = json.loads(response_text)
424
 
425
+ # Validate required fields
426
+ if all(key in feedback_data for key in ['feedback', 'corrected', 'has_errors']):
427
+ return feedback_data
428
+ else:
429
  raise ValueError("Missing required fields in response")
430
+
 
 
431
  except json.JSONDecodeError as json_err:
432
+ st.error(f"Debug - JSON Error: {str(json_err)}")
433
+ raise
 
 
 
 
 
 
 
434
 
435
  except Exception as e:
436
+ st.error(f"Debug - Error: {str(e)}")
 
 
 
437
  return {
438
+ "feedback": "⚠️ ระบบไม่สามารถวิเคราะห์ประโยคได้ กรุณาลองใหม่อีกครั้ง",
439
  "corrected": text,
440
  "has_errors": False
441
  }
 
552
  )
553
 
554
  with col2:
555
+ if st.session_state.feedback:
 
556
  st.markdown("""
557
  <div class="thai-eng">
558
  <div class="thai">📝 คำแนะนำจากครู</div>
 
560
  </div>
561
  """, unsafe_allow_html=True)
562
 
563
+ feedback_data = st.session_state.feedback
564
+ if isinstance(feedback_data, dict) and feedback_data.get('has_errors'):
 
565
  st.markdown(f"""
566
  <div style='background-color: #f0f2f6;
567
  padding: 15px;
 
570
  margin: 5px 0;
571
  font-family: "Sarabun", sans-serif;'>
572
  <p style='color: #1e88e5; margin-bottom: 10px;'>
573
+ {feedback_data['feedback']}
574
  </p>
575
  <p style='color: #666; font-size: 0.9em;'>
576
  ประโยคที่ถูกต้อง:<br/>
577
  <span style='color: #4CAF50; font-weight: bold;'>
578
+ {feedback_data['corrected']}
579
  </span>
580
  </p>
581
  </div>
582
  """, unsafe_allow_html=True)
583
 
584
+ # แสดงปุ่มแก้ไข
585
  if st.button("✍️ แก้ไขประโยคให้ถูกต้อง", key="correct_button"):
586
  last_user_entry_idx = next(
587
  (i for i, entry in reversed(list(enumerate(st.session_state.story)))
 
591
  if last_user_entry_idx is not None:
592
  apply_correction(
593
  last_user_entry_idx,
594
+ feedback_data['corrected']
595
  )
596
  st.rerun()
597
+ else:
598
+ st.markdown(f"""
599
+ <div style='background-color: #f0f2f6;
600
+ padding: 15px;
601
+ border-radius: 8px;
602
+ border-left: 4px solid #4CAF50;
603
+ margin: 5px 0;
604
+ font-family: "Sarabun", sans-serif;'>
605
+ <p style='color: #1e88e5;'>
606
+ {feedback_data.get('feedback', '✨ เขียนได้ถูกต้องแล้วค่ะ!')}
607
+ </p>
608
+ </div>
609
+ """, unsafe_allow_html=True)
 
 
610
 
611
  # Help and Suggestions Box
612
  st.markdown("""