Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -98,6 +98,7 @@ def save_and_play_audio(audio_recorder):
|
|
| 98 |
return filename
|
| 99 |
return None
|
| 100 |
|
|
|
|
| 101 |
def create_file(filename, prompt, response, should_save=True):
|
| 102 |
if not should_save:
|
| 103 |
return
|
|
@@ -117,6 +118,70 @@ def create_file(filename, prompt, response, should_save=True):
|
|
| 117 |
# Add Response with markdown title and emoji
|
| 118 |
combined_content += "# Response π¬\n" + response + "\n\n"
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
# Check for Python code or other resources and add them with markdown title and emoji
|
| 121 |
resources = re.findall(r"```([\s\S]*?)```", response)
|
| 122 |
for resource in resources:
|
|
|
|
| 98 |
return filename
|
| 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 |
+
for resource in resources:
|
| 124 |
+
# Check if the resource contains Python code
|
| 125 |
+
if "python" in resource.lower():
|
| 126 |
+
# Remove the word 'python' from the beginning of the code block
|
| 127 |
+
cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
|
| 128 |
+
|
| 129 |
+
# Capture standard output
|
| 130 |
+
original_stdout = sys.stdout
|
| 131 |
+
sys.stdout = io.StringIO()
|
| 132 |
+
|
| 133 |
+
# Execute cleaned Python code and capture the output
|
| 134 |
+
try:
|
| 135 |
+
exec(cleaned_code)
|
| 136 |
+
code_output = sys.stdout.getvalue()
|
| 137 |
+
combined_content += "# Code Results π\n" + "```" + code_output + "```\n\n"
|
| 138 |
+
except Exception as exec_exception:
|
| 139 |
+
# If execution fails, write the Python code to a file and execute it as a separate process
|
| 140 |
+
python_filename = f"{base_filename}.py"
|
| 141 |
+
with open(python_filename, 'w') as python_file:
|
| 142 |
+
python_file.write(cleaned_code)
|
| 143 |
+
|
| 144 |
+
# Run the Python file and capture its output
|
| 145 |
+
try:
|
| 146 |
+
code_output = subprocess.check_output(['python', python_filename], stderr=subprocess.STDOUT, text=True)
|
| 147 |
+
pythonFileExecResponse = "# Python File Results π\n" + "```" + code_output + "```\n\n"
|
| 148 |
+
combined_content += pythonFileExecResponse
|
| 149 |
+
except subprocess.CalledProcessError as e:
|
| 150 |
+
combined_content += f"```python\nError executing Python file: {e.output}\n```\n\n"
|
| 151 |
+
|
| 152 |
+
# Restore the original standard output
|
| 153 |
+
sys.stdout = original_stdout
|
| 154 |
+
else:
|
| 155 |
+
# Add Resource title with markdown and emoji for non-Python resources
|
| 156 |
+
combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
|
| 157 |
+
|
| 158 |
+
# Write the combined content into one file
|
| 159 |
+
combined_filename = f"{base_filename}-Combined.md"
|
| 160 |
+
with open(combined_filename, 'w') as file:
|
| 161 |
+
file.write(combined_content)
|
| 162 |
+
|
| 163 |
+
return combined_filename, python_filename if 'python_filename' in locals() else None
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def create_file_old(filename, prompt, response, should_save=True):
|
| 167 |
+
if not should_save:
|
| 168 |
+
return
|
| 169 |
+
|
| 170 |
+
# Step 2: Extract base filename without extension
|
| 171 |
+
base_filename, ext = os.path.splitext(filename)
|
| 172 |
+
|
| 173 |
+
# Step 3: Check if the response contains Python code
|
| 174 |
+
has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
|
| 175 |
+
|
| 176 |
+
# Step 4: Initialize the combined content
|
| 177 |
+
combined_content = ""
|
| 178 |
+
|
| 179 |
+
# Add Prompt with markdown title and emoji
|
| 180 |
+
combined_content += "# Prompt π\n" + prompt + "\n\n"
|
| 181 |
+
|
| 182 |
+
# Add Response with markdown title and emoji
|
| 183 |
+
combined_content += "# Response π¬\n" + response + "\n\n"
|
| 184 |
+
|
| 185 |
# Check for Python code or other resources and add them with markdown title and emoji
|
| 186 |
resources = re.findall(r"```([\s\S]*?)```", response)
|
| 187 |
for resource in resources:
|