sunbal7 commited on
Commit
2084430
·
verified ·
1 Parent(s): 36fb405

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -7,9 +7,10 @@ import subprocess
7
  import tempfile
8
  import time
9
 
 
10
  load_dotenv()
11
 
12
- # Initialize Novita API client
13
  class StoryToCodeConverter:
14
  def __init__(self):
15
  api_key = os.getenv("NOVITA_API_KEY")
@@ -26,11 +27,9 @@ Story: {story}
26
 
27
  Only return the Python code wrapped in triple backticks like ```python ... ```
28
  """
29
- response = self.client.chat_completions.create(
30
- model="gpt-4",
31
- messages=[{"role": "user", "content": prompt}]
32
- )
33
- content = response.choices[0].message.content
34
  return self._extract_code(content)
35
 
36
  def _extract_code(self, content):
@@ -39,11 +38,11 @@ Only return the Python code wrapped in triple backticks like ```python ... ```
39
  return content
40
 
41
 
42
- # Safe execution
43
  class CodeExecutor:
44
  def execute_safely(self, code):
45
  try:
46
- ast.parse(code) # Safety check
47
  with tempfile.NamedTemporaryFile(delete=False, suffix=".py") as tmp:
48
  tmp.write(code.encode("utf-8"))
49
  tmp_path = tmp.name
@@ -54,7 +53,7 @@ class CodeExecutor:
54
  return False
55
 
56
 
57
- # ---------- Streamlit UI ----------
58
  st.set_page_config(page_title="CodeTales", page_icon="✨")
59
 
60
  with st.sidebar:
@@ -78,8 +77,8 @@ if st.button("✨ Bake My Story Cake!"):
78
  st.warning("Please write a story first!")
79
  else:
80
  with st.spinner("Baking your story cake... 🍰"):
81
- converter = StoryToCodeConverter()
82
  try:
 
83
  code = converter.generate_code(story)
84
  except Exception as e:
85
  st.error(f"AI oven broke down! {e}")
 
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")
 
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):
 
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
 
53
  return False
54
 
55
 
56
+ # --- Streamlit Interface ---
57
  st.set_page_config(page_title="CodeTales", page_icon="✨")
58
 
59
  with st.sidebar:
 
77
  st.warning("Please write a story first!")
78
  else:
79
  with st.spinner("Baking your story cake... 🍰"):
 
80
  try:
81
+ converter = StoryToCodeConverter()
82
  code = converter.generate_code(story)
83
  except Exception as e:
84
  st.error(f"AI oven broke down! {e}")