Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -99,71 +99,8 @@ 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 |
-
|
| 113 |
-
combined_content = ""
|
| 114 |
-
|
| 115 |
-
# Add Prompt with markdown title and emoji
|
| 116 |
-
combined_content += "# Prompt π\n" + prompt + "\n\n"
|
| 117 |
-
|
| 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 |
|
|
@@ -187,6 +124,7 @@ def create_file_old(filename, prompt, response, should_save=True):
|
|
| 187 |
for resource in resources:
|
| 188 |
# Check if the resource contains Python code
|
| 189 |
if "python" in resource.lower():
|
|
|
|
| 190 |
# Remove the word 'python' from the beginning of the code block
|
| 191 |
cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
|
| 192 |
|
|
@@ -199,14 +137,21 @@ def create_file_old(filename, prompt, response, should_save=True):
|
|
| 199 |
|
| 200 |
# Execute cleaned Python code and capture the output
|
| 201 |
try:
|
|
|
|
|
|
|
| 202 |
exec(cleaned_code)
|
| 203 |
code_output = sys.stdout.getvalue()
|
| 204 |
combined_content += f"```\n{code_output}\n```\n\n"
|
| 205 |
realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"
|
| 206 |
-
st.write(realtimeEvalResponse)
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
except Exception as e:
|
| 209 |
combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
|
|
|
|
| 210 |
|
| 211 |
# Restore the original standard output
|
| 212 |
sys.stdout = original_stdout
|
|
|
|
| 99 |
return None
|
| 100 |
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
def create_file(filename, prompt, response, should_save=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
if not should_save:
|
| 105 |
return
|
| 106 |
|
|
|
|
| 124 |
for resource in resources:
|
| 125 |
# Check if the resource contains Python code
|
| 126 |
if "python" in resource.lower():
|
| 127 |
+
st.markdown('# Running python.. ')
|
| 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 |
|
|
|
|
| 137 |
|
| 138 |
# Execute cleaned Python code and capture the output
|
| 139 |
try:
|
| 140 |
+
st.markdown('# Running exec.. ')
|
| 141 |
+
|
| 142 |
exec(cleaned_code)
|
| 143 |
code_output = sys.stdout.getvalue()
|
| 144 |
combined_content += f"```\n{code_output}\n```\n\n"
|
| 145 |
realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"
|
|
|
|
| 146 |
|
| 147 |
+
st.write(realtimeEvalResponse)
|
| 148 |
+
|
| 149 |
+
st.markdown('# Completed exec.. ')
|
| 150 |
+
|
| 151 |
+
|
| 152 |
except Exception as e:
|
| 153 |
combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
|
| 154 |
+
st.markdown('# Error in exec.. ' + combined_content)
|
| 155 |
|
| 156 |
# Restore the original standard output
|
| 157 |
sys.stdout = original_stdout
|