sunbal7 commited on
Commit
0647136
ยท
verified ยท
1 Parent(s): 75f839c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +142 -9
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # app.py - Final Version with AI-Powered Animations
2
  # Fix for huggingface_hub compatibility
3
  import huggingface_hub
4
  if not hasattr(huggingface_hub, 'cached_download'):
@@ -178,6 +178,22 @@ st.markdown("""
178
  background: white;
179
  margin: 20px 0;
180
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  </style>
182
  """, unsafe_allow_html=True)
183
 
@@ -188,35 +204,40 @@ CONCEPTS = {
188
  "emoji": "๐Ÿ”„",
189
  "description": "Loops repeat actions multiple times",
190
  "example": "for i in range(5):\n print('Hello!')",
191
- "color": "#FF9E6D"
 
192
  },
193
  "conditional": {
194
  "name": "Conditional",
195
  "emoji": "โ“",
196
  "description": "Conditionals make decisions in code",
197
  "example": "if sunny:\n go_outside()\nelse:\n stay_inside()",
198
- "color": "#4ECDC4"
 
199
  },
200
  "function": {
201
  "name": "Function",
202
  "emoji": "โœจ",
203
  "description": "Functions are reusable blocks of code",
204
  "example": "def greet(name):\n print(f'Hello {name}!')",
205
- "color": "#FFD166"
 
206
  },
207
  "variable": {
208
  "name": "Variable",
209
  "emoji": "๐Ÿ“ฆ",
210
  "description": "Variables store information",
211
  "example": "score = 10\nplayer = 'Alex'",
212
- "color": "#FF6B6B"
 
213
  },
214
  "list": {
215
  "name": "List",
216
  "emoji": "๐Ÿ“",
217
  "description": "Lists store collections of items",
218
  "example": "fruits = ['apple', 'banana', 'orange']",
219
- "color": "#1A535C"
 
220
  }
221
  }
222
 
@@ -472,10 +493,41 @@ def create_interactive_animation(story, concept):
472
  st.error(f"Interactive animation error: {str(e)}")
473
  return None
474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475
  def main():
476
  """Main application function"""
477
  st.title("๐Ÿง™โ€โ™‚๏ธ StoryCoder - Learn Python Through Stories!")
478
- st.subheader("Turn your story into an animation and discover coding secrets!")
479
 
480
  # Initialize session state
481
  if 'story' not in st.session_state:
@@ -496,6 +548,10 @@ def main():
496
  st.session_state.audio_file = None
497
  if 'active_tab' not in st.session_state:
498
  st.session_state.active_tab = "story"
 
 
 
 
499
 
500
  # Create tabs
501
  tabs = st.empty()
@@ -522,13 +578,15 @@ def main():
522
  st.session_state.code_description = ""
523
  st.session_state.visual_cue = ""
524
  st.session_state.audio_file = None
 
 
525
  st.session_state.active_tab = "story"
526
 
527
  # Story creation tab
528
  if st.session_state.active_tab == "story":
529
  with st.container():
530
  st.header("๐Ÿ“– Create Your Story")
531
- st.write("Write a short story (2-5 sentences) and I'll turn it into an animation!")
532
 
533
  story = st.text_area(
534
  "Your story:",
@@ -574,6 +632,14 @@ def main():
574
  audio_text, ai_models
575
  )
576
 
 
 
 
 
 
 
 
 
577
  st.session_state.active_tab = "animation"
578
  st.rerun()
579
 
@@ -644,11 +710,44 @@ def main():
644
  audio_bytes = open(st.session_state.audio_file, 'rb').read()
645
  st.audio(audio_bytes, format='audio/wav')
646
 
647
- if st.button("โ–ถ๏ธ Play Automatically"):
648
  autoplay_audio(st.session_state.audio_file)
649
  else:
650
  st.info("Audio narration would play automatically here")
651
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
652
  st.success("โœจ Animation created successfully!")
653
  st.caption("This animation was generated with Python code based on your story!")
654
 
@@ -675,6 +774,7 @@ def main():
675
  </div>
676
  <p>{details['description']}</p>
677
  <pre style="background:#f0f0f0; padding:10px; border-radius:8px;">{details['example']}</pre>
 
678
  </div>
679
  """, unsafe_allow_html=True)
680
 
@@ -683,6 +783,15 @@ def main():
683
  caption=f"{details['name']} Animation Example",
684
  use_container_width=True)
685
 
 
 
 
 
 
 
 
 
 
686
  if st.button("See the Magic Code!", use_container_width=True):
687
  st.session_state.active_tab = "code"
688
  st.rerun()
@@ -722,6 +831,30 @@ def main():
722
  st.image(create_animation_preview("", concept),
723
  caption="This is similar to what your animation would look like",
724
  use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
725
  else:
726
  st.warning("No code generated yet!")
727
 
 
1
+ # app.py - Final Version with AI-Powered Animations and Voice Explanations
2
  # Fix for huggingface_hub compatibility
3
  import huggingface_hub
4
  if not hasattr(huggingface_hub, 'cached_download'):
 
178
  background: white;
179
  margin: 20px 0;
180
  }
181
+
182
+ .explanation-box {
183
+ background: linear-gradient(135deg, #e3f2fd, #bbdefb);
184
+ border-radius: 15px;
185
+ padding: 20px;
186
+ margin: 20px 0;
187
+ border-left: 5px solid var(--primary);
188
+ box-shadow: 0 4px 12px rgba(0,0,0,0.1);
189
+ }
190
+
191
+ .explanation-header {
192
+ color: var(--dark);
193
+ display: flex;
194
+ align-items: center;
195
+ gap: 10px;
196
+ }
197
  </style>
198
  """, unsafe_allow_html=True)
199
 
 
204
  "emoji": "๐Ÿ”„",
205
  "description": "Loops repeat actions multiple times",
206
  "example": "for i in range(5):\n print('Hello!')",
207
+ "color": "#FF9E6D",
208
+ "explanation": "A loop is like a magic spell that makes something happen again and again. In programming, we use loops when we want to repeat an action multiple times without writing the same code over and over."
209
  },
210
  "conditional": {
211
  "name": "Conditional",
212
  "emoji": "โ“",
213
  "description": "Conditionals make decisions in code",
214
  "example": "if sunny:\n go_outside()\nelse:\n stay_inside()",
215
+ "color": "#4ECDC4",
216
+ "explanation": "Conditionals are like crossroads in a story where you choose which path to take. In programming, we use 'if' statements to make decisions based on certain conditions, just like choosing what to wear based on the weather."
217
  },
218
  "function": {
219
  "name": "Function",
220
  "emoji": "โœจ",
221
  "description": "Functions are reusable blocks of code",
222
  "example": "def greet(name):\n print(f'Hello {name}!')",
223
+ "color": "#FFD166",
224
+ "explanation": "Functions are like magic spells you can create and reuse whenever you need them. They help us organize code into reusable blocks so we don't have to write the same thing multiple times."
225
  },
226
  "variable": {
227
  "name": "Variable",
228
  "emoji": "๐Ÿ“ฆ",
229
  "description": "Variables store information",
230
  "example": "score = 10\nplayer = 'Alex'",
231
+ "color": "#FF6B6B",
232
+ "explanation": "Variables are like labeled boxes where you can store information. They help us remember values and use them later in our code, just like remembering a character's name in a story."
233
  },
234
  "list": {
235
  "name": "List",
236
  "emoji": "๐Ÿ“",
237
  "description": "Lists store collections of items",
238
  "example": "fruits = ['apple', 'banana', 'orange']",
239
+ "color": "#1A535C",
240
+ "explanation": "Lists are like magical scrolls that can hold multiple items. In programming, we use lists to store collections of related things, like a wizard's inventory of potions."
241
  }
242
  }
243
 
 
493
  st.error(f"Interactive animation error: {str(e)}")
494
  return None
495
 
496
+ def generate_concept_explanation(story, concept, code_description, visual_cue):
497
+ """Generate a detailed explanation of the programming concept with examples"""
498
+ concept_info = CONCEPTS.get(concept, {})
499
+
500
+ explanation = f"""
501
+ Let me explain what's happening in your story and how it relates to programming!
502
+
503
+ Your story: "{story[:100]}..."
504
+
505
+ This story demonstrates the programming concept of: {concept_info.get('name', 'Programming')}
506
+
507
+ {concept_info.get('explanation', 'This is a fundamental programming concept.')}
508
+
509
+ In our animation:
510
+ - {visual_cue}
511
+ - This represents: {code_description}
512
+
513
+ How it works in code:
514
+ We use {concept_info.get('name', 'this concept')} like this:
515
+ {concept_info.get('example', 'Example code will appear here')}
516
+
517
+ In real life, you might use this concept when:
518
+ - Creating games with repeating actions (loops)
519
+ - Making decisions in apps (conditionals)
520
+ - Building reusable components (functions)
521
+
522
+ Try to spot this concept in other stories you create!
523
+ """
524
+
525
+ return explanation
526
+
527
  def main():
528
  """Main application function"""
529
  st.title("๐Ÿง™โ€โ™‚๏ธ StoryCoder - Learn Python Through Stories!")
530
+ st.subheader("Turn your story into an animation and discover coding secrets with voice explanations!")
531
 
532
  # Initialize session state
533
  if 'story' not in st.session_state:
 
548
  st.session_state.audio_file = None
549
  if 'active_tab' not in st.session_state:
550
  st.session_state.active_tab = "story"
551
+ if 'explanation_audio' not in st.session_state:
552
+ st.session_state.explanation_audio = None
553
+ if 'concept_explanation' not in st.session_state:
554
+ st.session_state.concept_explanation = ""
555
 
556
  # Create tabs
557
  tabs = st.empty()
 
578
  st.session_state.code_description = ""
579
  st.session_state.visual_cue = ""
580
  st.session_state.audio_file = None
581
+ st.session_state.explanation_audio = None
582
+ st.session_state.concept_explanation = ""
583
  st.session_state.active_tab = "story"
584
 
585
  # Story creation tab
586
  if st.session_state.active_tab == "story":
587
  with st.container():
588
  st.header("๐Ÿ“– Create Your Story")
589
+ st.write("Write a short story (2-5 sentences) and I'll turn it into an animation with voice explanations!")
590
 
591
  story = st.text_area(
592
  "Your story:",
 
632
  audio_text, ai_models
633
  )
634
 
635
+ with st.spinner("๐Ÿง  Generating concept explanation..."):
636
+ st.session_state.concept_explanation = generate_concept_explanation(
637
+ story,
638
+ main_concept,
639
+ st.session_state.code_description,
640
+ st.session_state.visual_cue
641
+ )
642
+
643
  st.session_state.active_tab = "animation"
644
  st.rerun()
645
 
 
710
  audio_bytes = open(st.session_state.audio_file, 'rb').read()
711
  st.audio(audio_bytes, format='audio/wav')
712
 
713
+ if st.button("โ–ถ๏ธ Play Story Automatically", use_container_width=True):
714
  autoplay_audio(st.session_state.audio_file)
715
  else:
716
  st.info("Audio narration would play automatically here")
717
 
718
+ # Concept explanation section
719
+ if st.session_state.concepts and st.session_state.concept_explanation:
720
+ st.subheader("๐ŸŽ“ Concept Explanation")
721
+ concept = st.session_state.concepts[0]
722
+ details = CONCEPTS.get(concept, {})
723
+
724
+ st.markdown(f"""
725
+ <div class="explanation-box">
726
+ <div class="explanation-header">
727
+ <span style="font-size:36px;">{details.get('emoji', 'โœจ')}</span>
728
+ <h3>Understanding {details.get('name', 'Programming')} Concept</h3>
729
+ </div>
730
+ <p>{st.session_state.concept_explanation}</p>
731
+ </div>
732
+ """, unsafe_allow_html=True)
733
+
734
+ # Generate explanation audio if not already generated
735
+ if not st.session_state.explanation_audio:
736
+ with st.spinner("๐Ÿ”Š Creating concept explanation audio..."):
737
+ ai_models = load_models()
738
+ st.session_state.explanation_audio = text_to_speech_custom(
739
+ st.session_state.concept_explanation,
740
+ ai_models,
741
+ "concept_explanation.wav"
742
+ )
743
+
744
+ if st.session_state.explanation_audio:
745
+ explanation_bytes = open(st.session_state.explanation_audio, 'rb').read()
746
+ st.audio(explanation_bytes, format='audio/wav')
747
+
748
+ if st.button("โ–ถ๏ธ Play Concept Explanation", use_container_width=True):
749
+ autoplay_audio(st.session_state.explanation_audio)
750
+
751
  st.success("โœจ Animation created successfully!")
752
  st.caption("This animation was generated with Python code based on your story!")
753
 
 
774
  </div>
775
  <p>{details['description']}</p>
776
  <pre style="background:#f0f0f0; padding:10px; border-radius:8px;">{details['example']}</pre>
777
+ <p><strong>Explanation:</strong> {details.get('explanation', 'Learn how this concept works!')}</p>
778
  </div>
779
  """, unsafe_allow_html=True)
780
 
 
783
  caption=f"{details['name']} Animation Example",
784
  use_container_width=True)
785
 
786
+ # Play explanation if available
787
+ if st.session_state.explanation_audio:
788
+ st.subheader("๐Ÿ”Š Concept Explanation")
789
+ explanation_bytes = open(st.session_state.explanation_audio, 'rb').read()
790
+ st.audio(explanation_bytes, format='audio/wav')
791
+
792
+ if st.button("โ–ถ๏ธ Play Full Explanation", use_container_width=True):
793
+ autoplay_audio(st.session_state.explanation_audio)
794
+
795
  if st.button("See the Magic Code!", use_container_width=True):
796
  st.session_state.active_tab = "code"
797
  st.rerun()
 
831
  st.image(create_animation_preview("", concept),
832
  caption="This is similar to what your animation would look like",
833
  use_container_width=True)
834
+
835
+ # Code explanation section
836
+ if st.session_state.concept_explanation:
837
+ st.subheader("๐ŸŽ“ Code Concept Explanation")
838
+ concept = st.session_state.concepts[0] if st.session_state.concepts else "loop"
839
+ details = CONCEPTS.get(concept, {})
840
+
841
+ st.markdown(f"""
842
+ <div class="explanation-box">
843
+ <div class="explanation-header">
844
+ <span style="font-size:36px;">{details.get('emoji', 'โœจ')}</span>
845
+ <h3>Understanding the Code</h3>
846
+ </div>
847
+ <p>{st.session_state.concept_explanation}</p>
848
+ </div>
849
+ """, unsafe_allow_html=True)
850
+
851
+ # Play explanation audio
852
+ if st.session_state.explanation_audio:
853
+ explanation_bytes = open(st.session_state.explanation_audio, 'rb').read()
854
+ st.audio(explanation_bytes, format='audio/wav')
855
+
856
+ if st.button("โ–ถ๏ธ Play Code Explanation", use_container_width=True):
857
+ autoplay_audio(st.session_state.explanation_audio)
858
  else:
859
  st.warning("No code generated yet!")
860