sunbal7 commited on
Commit
8a8602f
ยท
verified ยท
1 Parent(s): 2084430

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -43
app.py CHANGED
@@ -1,80 +1,82 @@
1
  import os
2
  import streamlit as st
3
  from dotenv import load_dotenv
4
- from novita_client import NovitaClient
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 = NovitaClient(api_key)
 
 
 
20
 
21
- def generate_code(self, story):
22
  prompt = f"""
23
  Convert this children's story into Python animation code using turtle graphics.
24
- Add code comments that explain the basic concepts to kids, like loops, movement, and colors.
25
 
26
- Story: {story}
 
27
 
28
- Only return the Python code wrapped in triple backticks like ```python ... ```
29
  """
30
- # Using Novita's correct `generate` method
31
- response = self.client.generate(prompt=prompt, model="gpt-4")
32
- content = response.get("text", "") # Adjust if response structure changes
33
- return self._extract_code(content)
34
-
35
- def _extract_code(self, content):
 
 
 
 
 
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) # Basic safety check
46
  with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as tmp:
47
  tmp.write(code.encode("utf-8"))
48
- tmp_path = tmp.name
49
- subprocess.Popen(["python", tmp_path])
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. ๐Ÿ“ Kids write a story (like a recipe!)
63
- 2. ๐Ÿ”ฎ AI turns it into **Python code**
64
- 3. ๐ŸŽฎ Animation appears like magic!
65
- 4. ๐Ÿค– Robot Teacher explains the code!
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 Your Story Recipe ๐Ÿ“", height=200,
73
- placeholder="Once upon a time, a rocket zoomed past rainbow stars and met an alien...")
74
 
75
  if st.button("โœจ Bake My Story Cake!"):
76
  if not story.strip():
77
- st.warning("Please write a story first!")
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='python')
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)` โ€“ Moves the rocket forward ๐Ÿš€
96
- - `turtle.color("red")` โ€“ Paints with colors ๐ŸŽจ
97
- - `for loop` โ€“ Repeats an action like magic ๐Ÿ”
98
- """)
99
- st.success("๐ŸŽ‰ Your story came to life!")
100
  else:
101
- st.error("Oops! Couldn't bake the animation.")
102
- else:
103
- st.caption("Turn your words into animated code with magic ๐Ÿช„")
104
 
105
- st.markdown("---")
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")