Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,82 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
-
from
|
| 5 |
import ast
|
| 6 |
import subprocess
|
| 7 |
import tempfile
|
| 8 |
-
import time
|
| 9 |
|
| 10 |
-
# Load API key
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
-
# --- Code Converter ---
|
| 14 |
class StoryToCodeConverter:
|
| 15 |
def __init__(self):
|
| 16 |
api_key = os.getenv("NOVITA_API_KEY")
|
| 17 |
if not api_key:
|
| 18 |
raise ValueError("NOVITA_API_KEY not found in environment")
|
| 19 |
-
self.client =
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
def generate_code(self, story):
|
| 22 |
prompt = f"""
|
| 23 |
Convert this children's story into Python animation code using turtle graphics.
|
| 24 |
-
|
| 25 |
|
| 26 |
-
Story:
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if "```python" in content:
|
| 37 |
return content.split("```python")[1].split("```")[0]
|
| 38 |
return content
|
| 39 |
|
| 40 |
-
|
| 41 |
-
# --- Code Executor ---
|
| 42 |
class CodeExecutor:
|
| 43 |
-
def execute_safely(self, code):
|
| 44 |
try:
|
| 45 |
-
ast.parse(code)
|
| 46 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as tmp:
|
| 47 |
tmp.write(code.encode("utf-8"))
|
| 48 |
-
|
| 49 |
-
subprocess.Popen(["python",
|
| 50 |
return True
|
| 51 |
except Exception as e:
|
| 52 |
st.error(f"Robot Chef burned the cake: {e}")
|
| 53 |
return False
|
| 54 |
|
| 55 |
-
|
| 56 |
# --- Streamlit Interface ---
|
| 57 |
st.set_page_config(page_title="CodeTales", page_icon="โจ")
|
| 58 |
-
|
| 59 |
with st.sidebar:
|
| 60 |
st.header("๐ฐ How It Works")
|
| 61 |
st.markdown("""
|
| 62 |
-
1.
|
| 63 |
-
2.
|
| 64 |
-
3.
|
| 65 |
-
4.
|
| 66 |
-
|
| 67 |
-
Made for ages 6โ12. Fun, visual, and interactive!
|
| 68 |
""")
|
| 69 |
|
| 70 |
st.title("โจ CodeTales: Storytime + Coding Magic")
|
| 71 |
|
| 72 |
-
story = st.text_area("Write
|
| 73 |
-
placeholder="
|
| 74 |
|
| 75 |
if st.button("โจ Bake My Story Cake!"):
|
| 76 |
if not story.strip():
|
| 77 |
-
st.warning("Please
|
| 78 |
else:
|
| 79 |
with st.spinner("Baking your story cake... ๐ฐ"):
|
| 80 |
try:
|
|
@@ -85,22 +87,19 @@ if st.button("โจ Bake My Story Cake!"):
|
|
| 85 |
st.stop()
|
| 86 |
|
| 87 |
st.subheader("๐ Your Code Recipe")
|
| 88 |
-
st.code(code, language=
|
| 89 |
|
| 90 |
executor = CodeExecutor()
|
| 91 |
if executor.execute_safely(code):
|
| 92 |
st.balloons()
|
| 93 |
with st.expander("๐ค Robot Teacher Says:"):
|
| 94 |
st.markdown("""
|
| 95 |
-
- `turtle.forward(100)`
|
| 96 |
-
- `turtle.color("red")`
|
| 97 |
-
- `for
|
| 98 |
-
|
| 99 |
-
st.success("๐ Your
|
| 100 |
else:
|
| 101 |
-
st.error("Oops
|
| 102 |
-
else:
|
| 103 |
-
st.caption("Turn your words into animated code with magic ๐ช")
|
| 104 |
|
| 105 |
-
st.
|
| 106 |
-
st.caption("Made with โค๏ธ for young coders | Powered by Novita + Python Turtle")
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
from openai import OpenAI # Novita-compatible OpenAI interface
|
| 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")
|
| 15 |
if not api_key:
|
| 16 |
raise ValueError("NOVITA_API_KEY not found in environment")
|
| 17 |
+
self.client = OpenAI(
|
| 18 |
+
api_key=api_key,
|
| 19 |
+
base_url="https://api.novita.ai/v3/openai"
|
| 20 |
+
)
|
| 21 |
|
| 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 explaining loops, movement, colorsโeasy for kids.
|
| 26 |
|
| 27 |
+
Story:
|
| 28 |
+
\"\"\"{story}\"\"\"
|
| 29 |
|
| 30 |
+
Wrap the code in triple backticks like ```python ... ```
|
| 31 |
"""
|
| 32 |
+
response = self.client.chat.completions.create(
|
| 33 |
+
model="meta-llama/Llama-4-Scout-17B-16E-Instruct", # or another supported model
|
| 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 |
+
|
| 42 |
+
def _extract_code(self, content: str) -> str:
|
| 43 |
if "```python" in content:
|
| 44 |
return content.split("```python")[1].split("```")[0]
|
| 45 |
return content
|
| 46 |
|
| 47 |
+
# --- Safe Code Executor ---
|
|
|
|
| 48 |
class CodeExecutor:
|
| 49 |
+
def execute_safely(self, code: str) -> bool:
|
| 50 |
try:
|
| 51 |
+
ast.parse(code)
|
| 52 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as tmp:
|
| 53 |
tmp.write(code.encode("utf-8"))
|
| 54 |
+
path = tmp.name
|
| 55 |
+
subprocess.Popen(["python", path])
|
| 56 |
return True
|
| 57 |
except Exception as e:
|
| 58 |
st.error(f"Robot Chef burned the cake: {e}")
|
| 59 |
return False
|
| 60 |
|
|
|
|
| 61 |
# --- Streamlit Interface ---
|
| 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. AI (Novita) turns it into Python-turtle code.
|
| 68 |
+
3. You see it! ๐ฎ
|
| 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 walks forward, draws a rainbow circle...")
|
| 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 cake... ๐ฐ"):
|
| 82 |
try:
|
|
|
|
| 87 |
st.stop()
|
| 88 |
|
| 89 |
st.subheader("๐ Your Code Recipe")
|
| 90 |
+
st.code(code, language="python")
|
| 91 |
|
| 92 |
executor = CodeExecutor()
|
| 93 |
if executor.execute_safely(code):
|
| 94 |
st.balloons()
|
| 95 |
with st.expander("๐ค Robot Teacher Says:"):
|
| 96 |
st.markdown("""
|
| 97 |
+
- `turtle.forward(100)`: moves forward
|
| 98 |
+
- `turtle.color("red")`: changes color
|
| 99 |
+
- `for` loops: repeat actions
|
| 100 |
+
""")
|
| 101 |
+
st.success("๐ Your animation came to life!")
|
| 102 |
else:
|
| 103 |
+
st.error("Oops, something went wrong.")
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
st.caption("Made with โค๏ธ for kids | Powered by Novita AI + Python Turtle")
|
|
|