Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
-
from openai import OpenAI
|
| 5 |
import ast
|
| 6 |
import subprocess
|
| 7 |
import tempfile
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
# --- Story-to-Code Converter ---
|
| 12 |
class StoryToCodeConverter:
|
| 13 |
def __init__(self):
|
| 14 |
api_key = os.getenv("NOVITA_API_KEY")
|
|
@@ -22,7 +21,7 @@ class StoryToCodeConverter:
|
|
| 22 |
def generate_code(self, story: str) -> str:
|
| 23 |
prompt = f"""
|
| 24 |
Convert this children's story into Python animation code using turtle graphics.
|
| 25 |
-
Include comments
|
| 26 |
|
| 27 |
Story:
|
| 28 |
\"\"\"{story}\"\"\"
|
|
@@ -30,12 +29,12 @@ Story:
|
|
| 30 |
Wrap the code in triple backticks like ```python ... ```
|
| 31 |
"""
|
| 32 |
response = self.client.chat.completions.create(
|
| 33 |
-
model="meta-llama/
|
| 34 |
messages=[
|
| 35 |
-
{"role": "system", "content": "You are a friendly teacher for kids."},
|
| 36 |
{"role": "user", "content": prompt}
|
| 37 |
],
|
| 38 |
-
max_tokens=512
|
| 39 |
)
|
| 40 |
return self._extract_code(response.choices[0].message.content)
|
| 41 |
|
|
@@ -44,7 +43,7 @@ Wrap the code in triple backticks like ```python ... ```
|
|
| 44 |
return content.split("```python")[1].split("```")[0]
|
| 45 |
return content
|
| 46 |
|
| 47 |
-
|
| 48 |
class CodeExecutor:
|
| 49 |
def execute_safely(self, code: str) -> bool:
|
| 50 |
try:
|
|
@@ -58,27 +57,26 @@ class CodeExecutor:
|
|
| 58 |
st.error(f"Robot Chef burned the cake: {e}")
|
| 59 |
return False
|
| 60 |
|
| 61 |
-
|
| 62 |
st.set_page_config(page_title="CodeTales", page_icon="✨")
|
| 63 |
with st.sidebar:
|
| 64 |
st.header("🍰 How It Works")
|
| 65 |
st.markdown("""
|
| 66 |
-
1. Kids write a story
|
| 67 |
-
2.
|
| 68 |
-
3.
|
| 69 |
-
4. Robot Teacher explains concepts
|
| 70 |
""")
|
| 71 |
|
| 72 |
st.title("✨ CodeTales: Storytime + Coding Magic")
|
| 73 |
-
|
| 74 |
story = st.text_area("Write your story here:", height=200,
|
| 75 |
-
placeholder="A turtle
|
| 76 |
|
| 77 |
if st.button("✨ Bake My Story Cake!"):
|
| 78 |
if not story.strip():
|
| 79 |
st.warning("Please enter a story!")
|
| 80 |
else:
|
| 81 |
-
with st.spinner("Baking your story
|
| 82 |
try:
|
| 83 |
converter = StoryToCodeConverter()
|
| 84 |
code = converter.generate_code(story)
|
|
@@ -94,12 +92,12 @@ if st.button("✨ Bake My Story Cake!"):
|
|
| 94 |
st.balloons()
|
| 95 |
with st.expander("🤖 Robot Teacher Says:"):
|
| 96 |
st.markdown("""
|
| 97 |
-
- `turtle.forward(100)`
|
| 98 |
-
- `turtle.color("red")`
|
| 99 |
-
- `for` loops
|
| 100 |
""")
|
| 101 |
-
st.success("🎉
|
| 102 |
else:
|
| 103 |
st.error("Oops, something went wrong.")
|
| 104 |
|
| 105 |
-
st.caption("Made with ❤️ for
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
from openai import OpenAI
|
| 5 |
import ast
|
| 6 |
import subprocess
|
| 7 |
import tempfile
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 11 |
class StoryToCodeConverter:
|
| 12 |
def __init__(self):
|
| 13 |
api_key = os.getenv("NOVITA_API_KEY")
|
|
|
|
| 21 |
def generate_code(self, story: str) -> str:
|
| 22 |
prompt = f"""
|
| 23 |
Convert this children's story into Python animation code using turtle graphics.
|
| 24 |
+
Include comments that explain loops, movement, colors—easy for kids.
|
| 25 |
|
| 26 |
Story:
|
| 27 |
\"\"\"{story}\"\"\"
|
|
|
|
| 29 |
Wrap the code in triple backticks like ```python ... ```
|
| 30 |
"""
|
| 31 |
response = self.client.chat.completions.create(
|
| 32 |
+
model="meta-llama/llama-3.1-8b-instruct",
|
| 33 |
messages=[
|
| 34 |
+
{"role": "system", "content": "You are a friendly teacher and coder for kids."},
|
| 35 |
{"role": "user", "content": prompt}
|
| 36 |
],
|
| 37 |
+
max_tokens=512,
|
| 38 |
)
|
| 39 |
return self._extract_code(response.choices[0].message.content)
|
| 40 |
|
|
|
|
| 43 |
return content.split("```python")[1].split("```")[0]
|
| 44 |
return content
|
| 45 |
|
| 46 |
+
|
| 47 |
class CodeExecutor:
|
| 48 |
def execute_safely(self, code: str) -> bool:
|
| 49 |
try:
|
|
|
|
| 57 |
st.error(f"Robot Chef burned the cake: {e}")
|
| 58 |
return False
|
| 59 |
|
| 60 |
+
|
| 61 |
st.set_page_config(page_title="CodeTales", page_icon="✨")
|
| 62 |
with st.sidebar:
|
| 63 |
st.header("🍰 How It Works")
|
| 64 |
st.markdown("""
|
| 65 |
+
1. 📝 Kids write a story
|
| 66 |
+
2. 🔮 Novita AI turns it into Python‑turtle code
|
| 67 |
+
3. 🎮 Animation plays
|
| 68 |
+
4. 🤖 Robot Teacher explains concepts
|
| 69 |
""")
|
| 70 |
|
| 71 |
st.title("✨ CodeTales: Storytime + Coding Magic")
|
|
|
|
| 72 |
story = st.text_area("Write your story here:", height=200,
|
| 73 |
+
placeholder="A turtle draws a rainbow circle...")
|
| 74 |
|
| 75 |
if st.button("✨ Bake My Story Cake!"):
|
| 76 |
if not story.strip():
|
| 77 |
st.warning("Please enter a story!")
|
| 78 |
else:
|
| 79 |
+
with st.spinner("Baking your story code... 🍰"):
|
| 80 |
try:
|
| 81 |
converter = StoryToCodeConverter()
|
| 82 |
code = converter.generate_code(story)
|
|
|
|
| 92 |
st.balloons()
|
| 93 |
with st.expander("🤖 Robot Teacher Says:"):
|
| 94 |
st.markdown("""
|
| 95 |
+
- `turtle.forward(100)` – Moves forward
|
| 96 |
+
- `turtle.color("red")` – Changes color
|
| 97 |
+
- `for` loops – Repeat patterns
|
| 98 |
""")
|
| 99 |
+
st.success("🎉 Animation ready!")
|
| 100 |
else:
|
| 101 |
st.error("Oops, something went wrong.")
|
| 102 |
|
| 103 |
+
st.caption("Made with ❤️ for young coders | Powered by Novita AI + Python Turtle")
|