Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,8 @@ from PIL import Image
|
|
| 13 |
import io
|
| 14 |
import matplotlib.pyplot as plt
|
| 15 |
import requests
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Configure Streamlit page
|
| 18 |
st.set_page_config(
|
|
@@ -147,6 +149,12 @@ st.markdown("""
|
|
| 147 |
background: #f0f8ff;
|
| 148 |
padding: 15px;
|
| 149 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
</style>
|
| 151 |
""", unsafe_allow_html=True)
|
| 152 |
|
|
@@ -319,6 +327,13 @@ plt.show()
|
|
| 319 |
}
|
| 320 |
}
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
def analyze_story(story):
|
| 323 |
"""Analyze story and identify programming concepts"""
|
| 324 |
story_lower = story.lower()
|
|
@@ -533,7 +548,8 @@ def create_interactive_animation(story, concepts):
|
|
| 533 |
method="animate",
|
| 534 |
args=[None, {"frame": {"duration": 100, "redraw": True}}]
|
| 535 |
)]
|
| 536 |
-
|
|
|
|
| 537 |
|
| 538 |
fig.update_traces(
|
| 539 |
textfont_size=40,
|
|
@@ -646,6 +662,14 @@ def create_story_image(story):
|
|
| 646 |
st.error(f"Image generation error: {str(e)}")
|
| 647 |
return None
|
| 648 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
def main():
|
| 650 |
"""Main application function"""
|
| 651 |
st.title("π§ββοΈ StoryCoder - Learn Python Through Stories!")
|
|
@@ -748,15 +772,15 @@ def main():
|
|
| 748 |
with col1:
|
| 749 |
st.caption("Loop Example")
|
| 750 |
st.code('"A dragon breathes fire 5 times at the castle"', language="text")
|
| 751 |
-
st.image("
|
| 752 |
with col2:
|
| 753 |
st.caption("Conditional Example")
|
| 754 |
st.code('"If it rains, the cat stays inside, else it goes out"', language="text")
|
| 755 |
-
st.image("
|
| 756 |
with col3:
|
| 757 |
st.caption("Function Example")
|
| 758 |
st.code('"A wizard casts a spell to make flowers grow"', language="text")
|
| 759 |
-
st.image("
|
| 760 |
|
| 761 |
# Animation tab
|
| 762 |
elif st.session_state.active_tab == "animation":
|
|
@@ -783,7 +807,8 @@ def main():
|
|
| 783 |
st.plotly_chart(st.session_state.interactive_animation, use_container_width=True)
|
| 784 |
else:
|
| 785 |
st.info("Interactive animation preview. The real animation would run on your computer!")
|
| 786 |
-
st.image(
|
|
|
|
| 787 |
|
| 788 |
# Play audio narration
|
| 789 |
st.subheader("π Story Narration")
|
|
@@ -824,6 +849,11 @@ def main():
|
|
| 824 |
<pre style="background:#f0f0f0; padding:10px; border-radius:8px;">{details['example']}</pre>
|
| 825 |
</div>
|
| 826 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 827 |
|
| 828 |
if st.button("See the Magic Code!", use_container_width=True):
|
| 829 |
st.session_state.active_tab = "code"
|
|
@@ -857,6 +887,13 @@ def main():
|
|
| 857 |
""")
|
| 858 |
|
| 859 |
st.success("π When you run this code, you'll see your story come to life!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
else:
|
| 861 |
st.warning("No code generated yet!")
|
| 862 |
|
|
|
|
| 13 |
import io
|
| 14 |
import matplotlib.pyplot as plt
|
| 15 |
import requests
|
| 16 |
+
from io import BytesIO
|
| 17 |
+
import json
|
| 18 |
|
| 19 |
# Configure Streamlit page
|
| 20 |
st.set_page_config(
|
|
|
|
| 149 |
background: #f0f8ff;
|
| 150 |
padding: 15px;
|
| 151 |
}
|
| 152 |
+
|
| 153 |
+
.animation-gif {
|
| 154 |
+
max-width: 100%;
|
| 155 |
+
border-radius: 10px;
|
| 156 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
|
| 157 |
+
}
|
| 158 |
</style>
|
| 159 |
""", unsafe_allow_html=True)
|
| 160 |
|
|
|
|
| 327 |
}
|
| 328 |
}
|
| 329 |
|
| 330 |
+
# Animation examples for different concepts
|
| 331 |
+
ANIMATION_EXAMPLES = {
|
| 332 |
+
"loop": "https://i.imgur.com/7zQY1eE.gif",
|
| 333 |
+
"conditional": "https://i.imgur.com/5X8jYAy.gif",
|
| 334 |
+
"function": "https://i.imgur.com/9zJkQ7P.gif"
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
def analyze_story(story):
|
| 338 |
"""Analyze story and identify programming concepts"""
|
| 339 |
story_lower = story.lower()
|
|
|
|
| 548 |
method="animate",
|
| 549 |
args=[None, {"frame": {"duration": 100, "redraw": True}}]
|
| 550 |
)]
|
| 551 |
+
)]
|
| 552 |
+
)
|
| 553 |
|
| 554 |
fig.update_traces(
|
| 555 |
textfont_size=40,
|
|
|
|
| 662 |
st.error(f"Image generation error: {str(e)}")
|
| 663 |
return None
|
| 664 |
|
| 665 |
+
def generate_animation_preview(concept):
|
| 666 |
+
"""Get a preview GIF for the animation concept"""
|
| 667 |
+
try:
|
| 668 |
+
# Return a sample GIF for the concept
|
| 669 |
+
return ANIMATION_EXAMPLES.get(concept, "https://i.imgur.com/7zQY1eE.gif")
|
| 670 |
+
except:
|
| 671 |
+
return "https://i.imgur.com/7zQY1eE.gif"
|
| 672 |
+
|
| 673 |
def main():
|
| 674 |
"""Main application function"""
|
| 675 |
st.title("π§ββοΈ StoryCoder - Learn Python Through Stories!")
|
|
|
|
| 772 |
with col1:
|
| 773 |
st.caption("Loop Example")
|
| 774 |
st.code('"A dragon breathes fire 5 times at the castle"', language="text")
|
| 775 |
+
st.image(generate_animation_preview("loop"), use_column_width=True, caption="Loop Animation Preview")
|
| 776 |
with col2:
|
| 777 |
st.caption("Conditional Example")
|
| 778 |
st.code('"If it rains, the cat stays inside, else it goes out"', language="text")
|
| 779 |
+
st.image(generate_animation_preview("conditional"), use_column_width=True, caption="Conditional Animation Preview")
|
| 780 |
with col3:
|
| 781 |
st.caption("Function Example")
|
| 782 |
st.code('"A wizard casts a spell to make flowers grow"', language="text")
|
| 783 |
+
st.image(generate_animation_preview("function"), use_column_width=True, caption="Function Animation Preview")
|
| 784 |
|
| 785 |
# Animation tab
|
| 786 |
elif st.session_state.active_tab == "animation":
|
|
|
|
| 807 |
st.plotly_chart(st.session_state.interactive_animation, use_container_width=True)
|
| 808 |
else:
|
| 809 |
st.info("Interactive animation preview. The real animation would run on your computer!")
|
| 810 |
+
st.image(generate_animation_preview(st.session_state.concepts[0] if st.session_state.concepts else "loop"),
|
| 811 |
+
use_container_width=True)
|
| 812 |
|
| 813 |
# Play audio narration
|
| 814 |
st.subheader("π Story Narration")
|
|
|
|
| 849 |
<pre style="background:#f0f0f0; padding:10px; border-radius:8px;">{details['example']}</pre>
|
| 850 |
</div>
|
| 851 |
""", unsafe_allow_html=True)
|
| 852 |
+
|
| 853 |
+
# Show animation preview for the concept
|
| 854 |
+
st.image(generate_animation_preview(concept),
|
| 855 |
+
caption=f"{details['name']} Animation Example",
|
| 856 |
+
use_column_width=True)
|
| 857 |
|
| 858 |
if st.button("See the Magic Code!", use_container_width=True):
|
| 859 |
st.session_state.active_tab = "code"
|
|
|
|
| 887 |
""")
|
| 888 |
|
| 889 |
st.success("π When you run this code, you'll see your story come to life!")
|
| 890 |
+
|
| 891 |
+
# Show what the animation would look like
|
| 892 |
+
st.subheader("π¬ What Your Animation Would Look Like")
|
| 893 |
+
concept = st.session_state.concepts[0] if st.session_state.concepts else "loop"
|
| 894 |
+
st.image(generate_animation_preview(concept),
|
| 895 |
+
caption="This is similar to what your animation would look like",
|
| 896 |
+
use_column_width=True)
|
| 897 |
else:
|
| 898 |
st.warning("No code generated yet!")
|
| 899 |
|