Spaces:
Runtime error
Runtime error
Update app.py
Browse files63 unmodified lines64656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710863 unmodified lines# Extract base filename without extensionbase_filename, ext = os.path.splitext(filename)combined_content = "# Prompt π\n" + prompt + "\n\n"combined_content += "# Response π¬\n" + response + "\n\n"# Extract Python code blocks from the responseallcodeandcodeexec = ''resources = re.findall(r"```python([\s\S]*?)```", response)for resource in resources:combined_content += "# Code Results π\n"# Execute the Python code and capture outputwith io.StringIO() as buf:with redirect_stdout(buf):try:cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)realtimeEvalResponse = "## Executing Code Block π\n" + "```" + cleaned_code + "```\n\n"# st.write(realtimeEvalResponse)combined_content += realtimeEvalResponseexec(cleaned_code.strip())code_output = buf.getvalue()realtimeEvalResponse = "## Executed Code Result π\n" + "```" + code_output + "```\n\n"# st.write(realtimeEvalResponse)combined_content += realtimeEvalResponseexcept Exception as e:realtimeEvalResponse = "## Executed Code Error π\n" + f"```python\nError executing Python code: {e}\n```\n\n"combined_content += realtimeEvalResponsest.markdown(combined_content)# Save the combined content to a Markdown fileif should_save:with open(filename, 'w') as file:file.write(combined_content)# Create a Base64 encoded link for the filewith open(filename, 'rb') as file:encoded_file = base64.b64encode(file.read()).decode()href = f'<a href="data:file/markdown;base64,{encoded_file}" download="{filename}">Download File π</a>'st.markdown(href, unsafe_allow_html=True)# Read it aloud63 unmodified lines64656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912063 unmodified lines# Extract base filename without extensionbase_filename, ext = os.path.splitext(filename)# Initialize the combined contentcombined_content = ""# Add Prompt with markdown title and emojicombined_content += "# Prompt π\n" + prompt + "\n\n"# Add Response with markdown title and emojicombined_content += "# Response π¬\n" + response + "\n\n"# Check for code blocks in the responseresources = re.findall(r"```([\s\S]*?)```", response)for resource in resources:# Check if the resource contains Python codeif "python" in resource.lower():# Remove the 'python' keyword from the code blockcleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)# Add Code Results title with markdown and emojicombined_content += "# Code Results π\n"# Redirect standard output to capture itoriginal_stdout = sys.stdoutsys.stdout = io.StringIO()# Execute the cleaned Python code within the contexttry:exec(cleaned_code, context)code_output = sys.stdout.getvalue()combined_content += f"```\n{code_output}\n```\n\n"realtimeEvalResponse = "# Code Results π\n" + "```" + code_output + "```\n\n"st.write(realtimeEvalResponse)except Exception as e:combined_content += f"```python\nError executing Python code: {e}\n```\n\n"# Restore the original standard outputsys.stdout = original_stdoutelse:# Add non-Python resources with markdown and emojicombined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"# Save the combined content to a Markdown fileif should_save:with open(f"{base_filename}.md", 'w') as file:file.write(combined_content)# Create a Base64 encoded link for the filewith open(f"{base_filename}.md", 'rb') as file:encoded_file = base64.b64encode(file.read()).decode()href = f'<a href="data:file/markdown;base64,{encoded_file}" download="{filename}">Download File π</a>'st.markdown(href, unsafe_allow_html=True)# Read it aloud
app.py+44-32