awacke1 commited on
Commit
7fd1236
Β·
1 Parent(s): 6fa8042

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -99,17 +99,17 @@ def save_and_play_audio(audio_recorder):
99
  return None
100
 
101
 
 
 
 
102
  def create_file(filename, prompt, response, should_save=True):
103
  if not should_save:
104
  return
105
 
106
- # Step 2: Extract base filename without extension
107
  base_filename, ext = os.path.splitext(filename)
108
 
109
- # Step 3: Check if the response contains Python code
110
- has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
111
-
112
- # Step 4: Initialize the combined content
113
  combined_content = ""
114
 
115
  # Add Prompt with markdown title and emoji
@@ -118,31 +118,28 @@ def create_file(filename, prompt, response, should_save=True):
118
  # Add Response with markdown title and emoji
119
  combined_content += "# Response πŸ’¬\n" + response + "\n\n"
120
 
121
- # Check for Python code or other resources and add them with markdown title and emoji
122
  resources = re.findall(r"```([\s\S]*?)```", response)
123
- # Create a dictionary to store exec() context
124
- exec_context = {}
125
  for resource in resources:
126
  # Check if the resource contains Python code
127
  if "python" in resource.lower():
128
- # Remove the word 'python' from the beginning of the code block
129
  cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
130
 
131
  # Add Code Results title with markdown and emoji
132
  combined_content += "# Code Results πŸš€\n"
133
 
134
- # Capture standard output
135
  original_stdout = sys.stdout
136
  sys.stdout = io.StringIO()
137
 
138
- # Execute cleaned Python code and capture the output within exec_context
139
  try:
140
- exec(cleaned_code, exec_context)
141
  code_output = sys.stdout.getvalue()
142
  combined_content += f"```\n{code_output}\n```\n\n"
143
- # Use the exec_context here to access any defined functions or variables
144
- # For example, if 'cleaned_code' defined a function 'foo', you can call it:
145
- # exec_context['foo']()
146
 
147
  except Exception as e:
148
  combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
@@ -150,12 +147,13 @@ def create_file(filename, prompt, response, should_save=True):
150
  # Restore the original standard output
151
  sys.stdout = original_stdout
152
  else:
153
- # Add Resource title with markdown and emoji for non-Python resources
154
  combined_content += "# Resource πŸ› οΈ\n" + "```" + resource + "```\n\n"
155
 
156
- # Write the combined content into one file
157
- with open(f"{base_filename}-Combined.md", 'w') as file:
158
- file.write(combined_content)
 
159
 
160
 
161
  def create_file_old2(filename, prompt, response, should_save=True):
 
99
  return None
100
 
101
 
102
+ # Define a context dictionary to maintain the state between exec calls
103
+ context = {}
104
+
105
  def create_file(filename, prompt, response, should_save=True):
106
  if not should_save:
107
  return
108
 
109
+ # Extract base filename without extension
110
  base_filename, ext = os.path.splitext(filename)
111
 
112
+ # Initialize the combined content
 
 
 
113
  combined_content = ""
114
 
115
  # Add Prompt with markdown title and emoji
 
118
  # Add Response with markdown title and emoji
119
  combined_content += "# Response πŸ’¬\n" + response + "\n\n"
120
 
121
+ # Check for code blocks in the response
122
  resources = re.findall(r"```([\s\S]*?)```", response)
 
 
123
  for resource in resources:
124
  # Check if the resource contains Python code
125
  if "python" in resource.lower():
126
+ # Remove the 'python' keyword from the code block
127
  cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
128
 
129
  # Add Code Results title with markdown and emoji
130
  combined_content += "# Code Results πŸš€\n"
131
 
132
+ # Redirect standard output to capture it
133
  original_stdout = sys.stdout
134
  sys.stdout = io.StringIO()
135
 
136
+ # Execute the cleaned Python code within the context
137
  try:
138
+ exec(cleaned_code, context)
139
  code_output = sys.stdout.getvalue()
140
  combined_content += f"```\n{code_output}\n```\n\n"
141
+ realtimeEvalResponse = "# Code Results πŸš€\n" + "```" + code_output + "```\n\n"
142
+ st.write(realtimeEvalResponse)
 
143
 
144
  except Exception as e:
145
  combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
 
147
  # Restore the original standard output
148
  sys.stdout = original_stdout
149
  else:
150
+ # Add non-Python resources with markdown and emoji
151
  combined_content += "# Resource πŸ› οΈ\n" + "```" + resource + "```\n\n"
152
 
153
+ # Save the combined content to a Markdown file
154
+ if should_save:
155
+ with open(f"{base_filename}-Combined.md", 'w') as file:
156
+ file.write(combined_content)
157
 
158
 
159
  def create_file_old2(filename, prompt, response, should_save=True):