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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -1
app.py CHANGED
@@ -99,7 +99,6 @@ def save_and_play_audio(audio_recorder):
99
  return None
100
 
101
 
102
-
103
  def create_file(filename, prompt, response, should_save=True):
104
  if not should_save:
105
  return
@@ -119,6 +118,65 @@ def create_file(filename, prompt, response, should_save=True):
119
  # Add Response with markdown title and emoji
120
  combined_content += "# Response πŸ’¬\n" + response + "\n\n"
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  # Check for Python code or other resources and add them with markdown title and emoji
123
  resources = re.findall(r"```([\s\S]*?)```", response)
124
  for resource in resources:
 
99
  return None
100
 
101
 
 
102
  def create_file(filename, prompt, response, should_save=True):
103
  if not should_save:
104
  return
 
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"
149
+
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):
162
+ if not should_save:
163
+ return
164
+
165
+ # Step 2: Extract base filename without extension
166
+ base_filename, ext = os.path.splitext(filename)
167
+
168
+ # Step 3: Check if the response contains Python code
169
+ has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
170
+
171
+ # Step 4: Initialize the combined content
172
+ combined_content = ""
173
+
174
+ # Add Prompt with markdown title and emoji
175
+ combined_content += "# Prompt πŸ“\n" + prompt + "\n\n"
176
+
177
+ # Add Response with markdown title and emoji
178
+ combined_content += "# Response πŸ’¬\n" + response + "\n\n"
179
+
180
  # Check for Python code or other resources and add them with markdown title and emoji
181
  resources = re.findall(r"```([\s\S]*?)```", response)
182
  for resource in resources: