File size: 9,781 Bytes
6514487
a60751e
6514487
a60751e
 
 
 
 
6514487
a60751e
6514487
 
 
 
 
 
a60751e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6514487
a60751e
 
 
 
 
 
 
 
 
6514487
a60751e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6514487
 
 
 
a60751e
6514487
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a60751e
 
6514487
 
 
 
 
a60751e
 
 
 
6514487
 
 
 
 
 
a60751e
 
 
6514487
 
 
 
 
 
 
 
a60751e
 
6514487
 
 
 
 
a60751e
 
6514487
 
 
a60751e
6514487
 
a60751e
 
 
 
 
 
 
 
 
 
 
 
 
6514487
 
 
a60751e
 
 
 
 
 
 
6514487
 
 
 
a60751e
 
 
 
 
 
 
 
 
 
 
 
 
6514487
 
 
 
 
 
 
a60751e
 
 
 
 
 
 
 
 
 
 
 
 
 
6514487
a60751e
 
 
 
 
6514487
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import streamlit as st
import openai
from datetime import datetime
import json
from collections import defaultdict

# Initialize OpenAI
openai.api_key = st.secrets["OPENAI_API_KEY"]

# Initialize session state variables
if 'story' not in st.session_state:
    st.session_state.story = []
if 'current_level' not in st.session_state:
    st.session_state.current_level = 'beginner'
if 'story_started' not in st.session_state:
    st.session_state.story_started = False
if 'badges' not in st.session_state:
    st.session_state.badges = defaultdict(int)
if 'vocabulary_used' not in st.session_state:
    st.session_state.vocabulary_used = set()
if 'learning_summary' not in st.session_state:
    st.session_state.learning_summary = []

def get_ai_response(prompt, level):
    """Get response from OpenAI API"""
    system_prompt = f"""You are an English teacher helping a {level} level student write a story. 
    Provide the following in JSON format:
    1. next_sentence: Continue the story naturally
    2. feedback: Constructive feedback about their writing
    3. vocabulary_suggestions: 3 new words they could use, appropriate for their level
    4. question: An open-ended question about the story to stimulate creativity
    5. grammar_check: Any grammar corrections needed
    6. visual_description: A simple description of the current scene
    Keep responses appropriate for the student's level."""
    
    try:
        response = openai.ChatCompletion.create(
            model="gpt-4-turbo-preview",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": f"Story so far: {prompt}\n\nProvide response in JSON format."}
            ],
            temperature=0.7
        )
        return json.loads(response.choices[0].message.content)
    except Exception as e:
        st.error(f"Error with AI response: {e}")
        return get_default_response(level)

def get_default_response(level):
    """Fallback response if API fails"""
    return {
        "next_sentence": "The story continues...",
        "feedback": "Please try again.",
        "vocabulary_suggestions": ["word1", "word2", "word3"],
        "question": "What happens next?",
        "grammar_check": "No issues found.",
        "visual_description": "A simple scene"
    }

def award_badge(category):
    """Award badges based on achievements"""
    st.session_state.badges[category] += 1
    st.balloons()
    st.success(f"πŸ† New Badge Earned: {category}!")

def generate_learning_summary():
    """Generate end-of-story learning summary"""
    unique_words = len(st.session_state.vocabulary_used)
    story_length = len(st.session_state.story)
    summary = {
        "total_sentences": story_length,
        "unique_vocabulary": unique_words,
        "badges_earned": dict(st.session_state.badges),
        "level": st.session_state.current_level,
        "completion_date": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    }
    return summary

def main():
    st.title("🎨 Interactive Story Adventure")
    
    # Level selection
    if not st.session_state.story_started:
        col1, col2 = st.columns([2, 1])
        with col1:
            st.write("### Welcome to Story Adventure!")
            st.write("Let's create an amazing story together. Choose your level and start writing!")
        
        level = st.selectbox(
            "Choose your English level:",
            ['beginner', 'intermediate', 'advanced'],
            key='level_select'
        )
        
        if st.button("Start Story!"):
            st.session_state.current_level = level
            st.session_state.story_started = True
            st.rerun()
    
    # Main story interface
    if st.session_state.story_started:
        # Story display area
        with st.container():
            st.write("### Our Story So Far:")
            for i, entry in enumerate(st.session_state.story):
                if entry['author'] == 'AI':
                    st.info(entry['text'])
                    if 'visual_description' in entry:
                        st.caption(f"🎨 Scene: {entry['visual_description']}")
                else:
                    st.success(entry['text'])
        
        # Writing area
        with st.container():
            # Show AI's question if available
            if st.session_state.story and 'question' in st.session_state.story[-1]:
                st.write(f"πŸ€” Think about: {st.session_state.story[-1]['question']}")
            
            user_input = st.text_area("Add to the story:", key='story_input')
            col1, col2, col3 = st.columns([1,1,1])
            
            with col1:
                if st.button("Submit"):
                    if user_input:
                        # Get story so far as context
                        story_context = " ".join([entry['text'] for entry in st.session_state.story])
                        
                        # Add user's sentence to story
                        st.session_state.story.append({
                            'author': 'User',
                            'text': user_input,
                            'timestamp': datetime.now().isoformat()
                        })
                        
                        # Get AI response
                        ai_response = get_ai_response(f"{story_context}\n{user_input}", 
                                                    st.session_state.current_level)
                        
                        # Add AI's sentence to story
                        st.session_state.story.append({
                            'author': 'AI',
                            'text': ai_response['next_sentence'],
                            'question': ai_response['question'],
                            'visual_description': ai_response['visual_description'],
                            'timestamp': datetime.now().isoformat()
                        })
                        
                        # Show feedback and grammar check
                        st.write("### Feedback:")
                        st.write(ai_response['feedback'])
                        if ai_response['grammar_check'] != "No issues found.":
                            st.warning(ai_response['grammar_check'])
                        
                        # Update vocabulary used
                        words = set(user_input.lower().split())
                        st.session_state.vocabulary_used.update(words)
                        
                        # Check for badges
                        if len(st.session_state.story) >= 10:
                            award_badge("Storyteller")
                        if len(st.session_state.vocabulary_used) >= 20:
                            award_badge("Vocabulary Master")
                        
                        st.rerun()
            
            with col2:
                if st.button("Get Help"):
                    if st.session_state.story:
                        ai_response = get_ai_response(" ".join([entry['text'] for entry in st.session_state.story]), 
                                                    st.session_state.current_level)
                        st.write("### Suggested Words:")
                        for word in ai_response['vocabulary_suggestions']:
                            st.write(f"- {word}")
            
            with col3:
                if st.button("Finish Story"):
                    if len(st.session_state.story) > 0:
                        # Generate learning summary
                        summary = generate_learning_summary()
                        st.session_state.learning_summary.append(summary)
                        
                        st.write("### Story Complete! πŸŽ‰")
                        st.write("#### Your Achievements:")
                        st.write(f"- Total sentences written: {summary['total_sentences']}")
                        st.write(f"- Unique words used: {summary['unique_vocabulary']}")
                        st.write("- Badges earned:")
                        for badge, count in summary['badges_earned'].items():
                            st.write(f"  * {badge}: {count} πŸ†")
                        
                        # Download options
                        story_text = "\n".join([entry['text'] for entry in st.session_state.story])
                        st.download_button(
                            label="Download Story",
                            data=story_text,
                            file_name="my_story.txt",
                            mime="text/plain"
                        )
                        
                        # Download progress report for parents/teachers
                        progress_report = {
                            "student_level": st.session_state.current_level,
                            "story_summary": summary,
                            "learning_progress": st.session_state.learning_summary
                        }
                        st.download_button(
                            label="Download Progress Report",
                            data=json.dumps(progress_report, indent=2),
                            file_name="progress_report.json",
                            mime="application/json"
                        )
                        
                        # Reset session state
                        if st.button("Start New Story"):
                            st.session_state.story = []
                            st.session_state.story_started = False
                            st.session_state.vocabulary_used = set()
                            st.rerun()

if __name__ == "__main__":
    main()