Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
#
|
| 107 |
base_filename, ext = os.path.splitext(filename)
|
| 108 |
|
| 109 |
-
#
|
| 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
|
| 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
|
| 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 |
-
#
|
| 135 |
original_stdout = sys.stdout
|
| 136 |
sys.stdout = io.StringIO()
|
| 137 |
|
| 138 |
-
# Execute cleaned Python code
|
| 139 |
try:
|
| 140 |
-
exec(cleaned_code,
|
| 141 |
code_output = sys.stdout.getvalue()
|
| 142 |
combined_content += f"```\n{code_output}\n```\n\n"
|
| 143 |
-
|
| 144 |
-
|
| 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
|
| 154 |
combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
|
| 158 |
-
|
|
|
|
| 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):
|