Spaces:
Runtime error
Runtime error
Update app.py
Browse files63 unmodified lines64656667686970677172737475707172737677787576777879808182838485868788899091929394957980818283848586878889909192939495969798991001011021031041051061071081099811011111210110210310410511311411511611711811912063 unmodified lines # Extract base filename without extension base_filename, ext = os.path.splitext(filename)
# Initialize the combined content combined_content = ""
# Add Prompt with markdown title and emoji combined_content = "# Prompt π\n" + prompt + "\n\n" combined_content += "# Prompt π\n" + prompt + "\n\n"
# Add Response with markdown title and emoji combined_content += "# Response π¬\n" + response + "\n\n"
# Extract Python code blocks from the response allcodeandcodeexec = '' resources = re.findall(r"```python([\s\S]*?)```", response) # Check for code blocks in the response resources = re.findall(r"```([\s\S]*?)```", response) for resource in resources: combined_content += "# Code Results π\n"
# Execute the Python code and capture output with 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 += realtimeEvalResponse exec(cleaned_code.strip()) code_output = buf.getvalue() realtimeEvalResponse = "## Executed Code Result π\n" + "```" + code_output + "```\n\n" # st.write(realtimeEvalResponse) combined_content += realtimeEvalResponse except Exception as e: realtimeEvalResponse = "## Executed Code Error π\n" + f"```python\nError executing Python code: {e}\n```\n\n" combined_content += realtimeEvalResponse
st.markdown(combined_content) # Check if the resource contains Python code if "python" in resource.lower(): # Remove the 'python' keyword from the code block cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE) # Add Code Results title with markdown and emoji combined_content += "# Code Results π\n"
# Redirect standard output to capture it original_stdout = sys.stdout sys.stdout = io.StringIO() # Execute the cleaned Python code within the context try: 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 output sys.stdout = original_stdout else: # Add non-Python resources with markdown and emoji combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
# Save the combined content to a Markdown file if should_save: with open(filename, 'w') as file: with open(f"{base_filename}.md", 'w') as file: file.write(combined_content)
# Create a Base64 encoded link for the file with 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) # Create a Base64 encoded link for the file with 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