Rathapoom commited on
Commit
fbb20e0
·
verified ·
1 Parent(s): 273663a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -22
app.py CHANGED
@@ -967,7 +967,7 @@ def update_achievements():
967
  return False
968
 
969
  def provide_feedback(text: str, level: str) -> Dict[str, str]:
970
- """Provide feedback on the user's writing with appropriate level"""
971
 
972
  level_prompts = {
973
  'Beginner': """
@@ -977,6 +977,7 @@ def provide_feedback(text: str, level: str) -> Dict[str, str]:
977
  - Basic vocabulary
978
  - Capitalization and periods
979
  Provide very simple, encouraging feedback in Thai.
 
980
  """,
981
  'Intermediate': """
982
  Focus on:
@@ -986,6 +987,7 @@ def provide_feedback(text: str, level: str) -> Dict[str, str]:
986
  - Basic punctuation
987
  - Simple conjunctions
988
  Provide moderately detailed feedback in Thai.
 
989
  """,
990
  'Advanced': """
991
  Focus on:
@@ -995,6 +997,7 @@ def provide_feedback(text: str, level: str) -> Dict[str, str]:
995
  - All punctuation
996
  - Style and flow
997
  Provide comprehensive feedback in Thai.
 
998
  """
999
  }
1000
 
@@ -1025,8 +1028,8 @@ def provide_feedback(text: str, level: str) -> Dict[str, str]:
1025
  "content": f"Review this sentence: {text}"
1026
  }
1027
  ],
1028
- max_tokens=200,
1029
- temperature=0.3
1030
  )
1031
 
1032
  # Parse response
@@ -1057,6 +1060,7 @@ def provide_feedback(text: str, level: str) -> Dict[str, str]:
1057
  "difficulty_score": 5
1058
  }
1059
 
 
1060
  def get_vocabulary_suggestions(context: str = "", level: str = DEFAULT_LEVEL) -> List[str]:
1061
  """Get contextual vocabulary suggestions with Thai translations"""
1062
  try:
@@ -1628,24 +1632,40 @@ def handle_ending_mode(text: str):
1628
  # Get current remaining sentences
1629
  remaining = st.session_state.sentences_to_end
1630
 
 
 
 
 
 
1631
  # Add user's sentence
1632
  st.session_state.story.append({
1633
  "role": "You",
1634
  "content": text,
1635
  "is_corrected": False,
1636
- "is_correct": True,
1637
  "timestamp": datetime.now().isoformat(),
1638
  "remaining_sentences": remaining
1639
  })
1640
 
 
 
 
 
 
 
 
 
1641
  # Decrease remaining count for user's sentence
1642
  st.session_state.sentences_to_end -= 1
1643
  remaining = st.session_state.sentences_to_end
1644
 
1645
  if remaining > 0:
 
 
 
1646
  # Generate AI's ending continuation
1647
  ai_response = generate_ending_continuation(
1648
- text,
1649
  ending_type=st.session_state.ending_type,
1650
  remaining_sentences=remaining,
1651
  level=st.session_state.level
@@ -1691,36 +1711,41 @@ def handle_ending_mode(text: str):
1691
  st.error("เกิดข้อผิดพลาดในโหมดจบเรื่อง กรุณาลองใหม่อีกครั้ง")
1692
 
1693
 
 
1694
  def generate_final_ending(story: List[dict], ending_type: str, level: str) -> str:
1695
- """Generate the final ending sentence based on the story and chosen ending type"""
1696
  try:
1697
  # Create a comprehensive prompt for the final ending
1698
  story_summary = " ".join([entry['content'] for entry in story[-5:]]) # Last 5 sentences
1699
 
1700
  ending_prompts = {
1701
  "Happy Ending": """
1702
- Create a final happy ending sentence that:
1703
  - Brings joy and satisfaction
1704
  - Resolves the main story elements
1705
  - Ends on a positive note
 
1706
  """,
1707
  "Mysterious Ending": """
1708
- Create a final mysterious ending sentence that:
1709
  - Leaves an intriguing question
1710
  - Creates a sense of wonder
1711
  - Maintains some mystery
 
1712
  """,
1713
  "Lesson Learned": """
1714
- Create a final sentence that:
1715
  - Shows what was learned
1716
  - Provides a moral lesson
1717
  - Connects to the story's events
 
1718
  """,
1719
  "Surprise Ending": """
1720
  Create a final twist ending that:
1721
  - Provides an unexpected but logical conclusion
1722
  - Connects to previous story elements
1723
  - Creates a satisfying surprise
 
1724
  """
1725
  }
1726
 
@@ -1728,37 +1753,34 @@ def generate_final_ending(story: List[dict], ending_type: str, level: str) -> st
1728
  'Beginner': {
1729
  'instructions': """
1730
  Additional Guidelines:
1731
- - Use only 1-2 VERY simple sentences
1732
- - Use Present Simple Tense only
1733
  - Basic vocabulary
1734
- - 5-7 words per sentence maximum
1735
- - Focus on clear, basic responses
1736
  """,
1737
- 'max_tokens': 30,
1738
  'temperature': 0.6
1739
  },
1740
  'Intermediate': {
1741
  'instructions': """
1742
  Additional Guidelines:
1743
- - Use exactly 2 sentences maximum
1744
  - Can use Present or Past Tense
1745
- - Keep each sentence under 12 words
1746
  - Grade-appropriate vocabulary
1747
- - Add simple descriptions but stay concise
1748
  """,
1749
- 'max_tokens': 40,
1750
  'temperature': 0.7
1751
  },
1752
  'Advanced': {
1753
  'instructions': """
1754
  Additional Guidelines:
1755
- - Use 2-3 sentences maximum
1756
  - Various tenses allowed
1757
  - Natural sentence length but keep overall response concise
1758
  - More sophisticated vocabulary and structures
1759
- - Create engaging responses that encourage creative continuation
1760
  """,
1761
- 'max_tokens': 50,
1762
  'temperature': 0.8
1763
  }
1764
  }
@@ -1775,7 +1797,7 @@ def generate_final_ending(story: List[dict], ending_type: str, level: str) -> st
1775
 
1776
  {level_settings['instructions']}
1777
 
1778
- This must be the absolute final sentence(s) of the story.
1779
  Make it conclusive and satisfying.
1780
  """
1781
  },
@@ -1795,6 +1817,7 @@ def generate_final_ending(story: List[dict], ending_type: str, level: str) -> st
1795
  return "And so, the story came to an end."
1796
 
1797
 
 
1798
  def generate_ending_continuation(text: str, ending_type: str, remaining_sentences: int, level: str) -> str:
1799
  """Generate AI continuation focusing on story conclusion"""
1800
  try:
 
967
  return False
968
 
969
  def provide_feedback(text: str, level: str) -> Dict[str, str]:
970
+ """Provide more detailed feedback on the user's writing with appropriate level"""
971
 
972
  level_prompts = {
973
  'Beginner': """
 
977
  - Basic vocabulary
978
  - Capitalization and periods
979
  Provide very simple, encouraging feedback in Thai.
980
+ Explain any mistakes and how to correct them.
981
  """,
982
  'Intermediate': """
983
  Focus on:
 
987
  - Basic punctuation
988
  - Simple conjunctions
989
  Provide moderately detailed feedback in Thai.
990
+ Explain any mistakes and suggest improvements.
991
  """,
992
  'Advanced': """
993
  Focus on:
 
997
  - All punctuation
998
  - Style and flow
999
  Provide comprehensive feedback in Thai.
1000
+ Explain any mistakes and provide examples for improvement.
1001
  """
1002
  }
1003
 
 
1028
  "content": f"Review this sentence: {text}"
1029
  }
1030
  ],
1031
+ max_tokens=300, # Increased from 200
1032
+ temperature=0.5 # Adjusted for balanced variety
1033
  )
1034
 
1035
  # Parse response
 
1060
  "difficulty_score": 5
1061
  }
1062
 
1063
+
1064
  def get_vocabulary_suggestions(context: str = "", level: str = DEFAULT_LEVEL) -> List[str]:
1065
  """Get contextual vocabulary suggestions with Thai translations"""
1066
  try:
 
1632
  # Get current remaining sentences
1633
  remaining = st.session_state.sentences_to_end
1634
 
1635
+ # Get feedback
1636
+ feedback_data = provide_feedback(text, st.session_state.level)
1637
+ st.session_state.feedback = feedback_data
1638
+ is_correct = not feedback_data.get('has_errors', False)
1639
+
1640
  # Add user's sentence
1641
  st.session_state.story.append({
1642
  "role": "You",
1643
  "content": text,
1644
  "is_corrected": False,
1645
+ "is_correct": is_correct,
1646
  "timestamp": datetime.now().isoformat(),
1647
  "remaining_sentences": remaining
1648
  })
1649
 
1650
+ # Update vocabulary
1651
+ words = set(text.lower().split())
1652
+ st.session_state.stats['vocabulary_used'].update(words)
1653
+
1654
+ # Update points and achievements
1655
+ update_points(is_correct)
1656
+ update_achievements()
1657
+
1658
  # Decrease remaining count for user's sentence
1659
  st.session_state.sentences_to_end -= 1
1660
  remaining = st.session_state.sentences_to_end
1661
 
1662
  if remaining > 0:
1663
+ # Use corrected text if there are errors
1664
+ text_for_continuation = feedback_data['corrected'] if feedback_data.get('has_errors') else text
1665
+
1666
  # Generate AI's ending continuation
1667
  ai_response = generate_ending_continuation(
1668
+ text_for_continuation,
1669
  ending_type=st.session_state.ending_type,
1670
  remaining_sentences=remaining,
1671
  level=st.session_state.level
 
1711
  st.error("เกิดข้อผิดพลาดในโหมดจบเรื่อง กรุณาลองใหม่อีกครั้ง")
1712
 
1713
 
1714
+
1715
  def generate_final_ending(story: List[dict], ending_type: str, level: str) -> str:
1716
+ """Generate the final ending sentences based on the story and chosen ending type"""
1717
  try:
1718
  # Create a comprehensive prompt for the final ending
1719
  story_summary = " ".join([entry['content'] for entry in story[-5:]]) # Last 5 sentences
1720
 
1721
  ending_prompts = {
1722
  "Happy Ending": """
1723
+ Create a final happy ending that:
1724
  - Brings joy and satisfaction
1725
  - Resolves the main story elements
1726
  - Ends on a positive note
1727
+ - Use 2-3 sentences
1728
  """,
1729
  "Mysterious Ending": """
1730
+ Create a final mysterious ending that:
1731
  - Leaves an intriguing question
1732
  - Creates a sense of wonder
1733
  - Maintains some mystery
1734
+ - Use 2-3 sentences
1735
  """,
1736
  "Lesson Learned": """
1737
+ Create a final ending that:
1738
  - Shows what was learned
1739
  - Provides a moral lesson
1740
  - Connects to the story's events
1741
+ - Use 2-3 sentences
1742
  """,
1743
  "Surprise Ending": """
1744
  Create a final twist ending that:
1745
  - Provides an unexpected but logical conclusion
1746
  - Connects to previous story elements
1747
  - Creates a satisfying surprise
1748
+ - Use 2-3 sentences
1749
  """
1750
  }
1751
 
 
1753
  'Beginner': {
1754
  'instructions': """
1755
  Additional Guidelines:
1756
+ - Use simple sentences
1757
+ - Use Present Simple Tense
1758
  - Basic vocabulary
1759
+ - 7-10 words per sentence maximum
 
1760
  """,
1761
+ 'max_tokens': 100, # Increased to allow for longer endings
1762
  'temperature': 0.6
1763
  },
1764
  'Intermediate': {
1765
  'instructions': """
1766
  Additional Guidelines:
1767
+ - Use 2-3 sentences
1768
  - Can use Present or Past Tense
1769
+ - Keep each sentence under 15 words
1770
  - Grade-appropriate vocabulary
 
1771
  """,
1772
+ 'max_tokens': 120, # Increased
1773
  'temperature': 0.7
1774
  },
1775
  'Advanced': {
1776
  'instructions': """
1777
  Additional Guidelines:
1778
+ - Use 2-3 sentences
1779
  - Various tenses allowed
1780
  - Natural sentence length but keep overall response concise
1781
  - More sophisticated vocabulary and structures
 
1782
  """,
1783
+ 'max_tokens': 150, # Increased
1784
  'temperature': 0.8
1785
  }
1786
  }
 
1797
 
1798
  {level_settings['instructions']}
1799
 
1800
+ This must be the absolute final sentences of the story.
1801
  Make it conclusive and satisfying.
1802
  """
1803
  },
 
1817
  return "And so, the story came to an end."
1818
 
1819
 
1820
+
1821
  def generate_ending_continuation(text: str, ending_type: str, remaining_sentences: int, level: str) -> str:
1822
  """Generate AI continuation focusing on story conclusion"""
1823
  try: