Rathapoom commited on
Commit
cc3c1dd
Β·
verified Β·
1 Parent(s): 695191e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -14
app.py CHANGED
@@ -56,21 +56,48 @@ def generate_story_continuation(user_input: str, level: str) -> str:
56
  return "I'm having trouble continuing the story. Please try again."
57
 
58
  def get_vocabulary_suggestions() -> List[str]:
59
- """Get vocabulary suggestions based on the story context."""
60
  try:
 
 
 
61
  response = client.chat.completions.create(
62
- model="gpt-4o-mini",
63
  messages=[
64
- {"role": "system", "content": f"You are a language tutor. Suggest 3 appropriate vocabulary words for {st.session_state.level} English learners."},
65
- {"role": "user", "content": "Suggest 3 interesting words that could be used in the story. Format: word1, word2, word3"}
 
 
 
 
 
 
66
  ],
67
- max_tokens=50,
68
- temperature=0.6
69
  )
70
- return response.choices[0].message.content.split(', ')
 
 
 
71
  except Exception as e:
72
  st.error(f"Error getting vocabulary suggestions: {str(e)}")
73
- return ["exciting", "beautiful", "happy"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  def get_creative_prompt() -> str:
76
  """Generate a creative prompt to inspire the next part of the story."""
@@ -90,21 +117,25 @@ def get_creative_prompt() -> str:
90
  return "What happens next in your story?"
91
 
92
  def provide_feedback(text: str, level: str) -> str:
93
- """Provide constructive feedback on the user's writing."""
94
  try:
95
  response = client.chat.completions.create(
96
- model="gpt-4o-mini",
97
  messages=[
98
- {"role": "system", "content": f"You are an encouraging English teacher for {level} students. Provide one brief, positive piece of feedback and one gentle correction if needed."},
99
- {"role": "user", "content": f"Provide encouraging feedback on: {text}"}
 
 
 
 
100
  ],
101
- max_tokens=50,
102
  temperature=0.6
103
  )
104
  return response.choices[0].message.content
105
  except Exception as e:
106
  st.error(f"Error generating feedback: {str(e)}")
107
- return "Great effort! Keep writing!"
108
 
109
  def update_achievements(text: str):
110
  """Update user achievements based on their writing."""
 
56
  return "I'm having trouble continuing the story. Please try again."
57
 
58
  def get_vocabulary_suggestions() -> List[str]:
59
+ """Get contextual vocabulary suggestions based on the current story."""
60
  try:
61
+ # Get the last few story entries for context
62
+ recent_story = '\n'.join([entry['content'] for entry in st.session_state.story[-3:]] if st.session_state.story else "Story just started")
63
+
64
  response = client.chat.completions.create(
65
+ model="gpt-3.5-turbo",
66
  messages=[
67
+ {"role": "system", "content": f"""You are a creative writing tutor for {st.session_state.level} English learners.
68
+ Based on the story context, suggest 5 varied words that could be used next:
69
+ - Include a mix of nouns, verbs, and adjectives
70
+ - Match the {st.session_state.level} level
71
+ - Words should fit the story theme
72
+ - No basic words like 'happy', 'sad', 'good'
73
+ Format: word1 (type), word2 (type), etc."""},
74
+ {"role": "user", "content": f"Story context:\n{recent_story}\n\nSuggest 5 relevant, interesting words:"}
75
  ],
76
+ max_tokens=100,
77
+ temperature=0.8
78
  )
79
+
80
+ # Parse the response to get words and their types
81
+ word_suggestions = response.choices[0].message.content.split(', ')
82
+ return word_suggestions
83
  except Exception as e:
84
  st.error(f"Error getting vocabulary suggestions: {str(e)}")
85
+ return ["adventure (noun)", "whisper (verb)", "mysterious (adjective)"]
86
+
87
+ # In the main UI section, update how vocabulary suggestions are displayed:
88
+ if st.button("Get Vocabulary Ideas"):
89
+ vocab_suggestions = get_vocabulary_suggestions()
90
+ st.markdown("#### πŸ“š Suggested Words")
91
+ for word in vocab_suggestions:
92
+ st.markdown(f"β€’ *{word}*")
93
+
94
+ # And update how feedback is displayed to be more concise:
95
+ if st.session_state.feedback:
96
+ st.markdown("""
97
+ <div style='background-color: #f0f2f6; padding: 8px; border-radius: 4px; margin-bottom: 10px;'>
98
+ πŸ“ <i>{}</i>
99
+ </div>
100
+ """.format(st.session_state.feedback), unsafe_allow_html=True)
101
 
102
  def get_creative_prompt() -> str:
103
  """Generate a creative prompt to inspire the next part of the story."""
 
117
  return "What happens next in your story?"
118
 
119
  def provide_feedback(text: str, level: str) -> str:
120
+ """Provide concise, constructive feedback on the user's writing."""
121
  try:
122
  response = client.chat.completions.create(
123
+ model="gpt-3.5-turbo",
124
  messages=[
125
+ {"role": "system", "content": f"""You are an encouraging English teacher for {level} students.
126
+ Provide very brief feedback (max 15 words) focusing on ONE specific aspect:
127
+ - Either praise a good point OR
128
+ - Suggest one small improvement
129
+ Keep it short and friendly."""},
130
+ {"role": "user", "content": f"Give brief feedback on: {text}"}
131
  ],
132
+ max_tokens=30,
133
  temperature=0.6
134
  )
135
  return response.choices[0].message.content
136
  except Exception as e:
137
  st.error(f"Error generating feedback: {str(e)}")
138
+ return "Good effort! Keep writing!"
139
 
140
  def update_achievements(text: str):
141
  """Update user achievements based on their writing."""