awacke1 commited on
Commit
156a7e8
Β·
1 Parent(s): 62e9061

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -32
app.py CHANGED
@@ -64,45 +64,57 @@ def create_file(filename, prompt, response, should_save=True):
64
  # Extract base filename without extension
65
  base_filename, ext = os.path.splitext(filename)
66
 
67
- combined_content = "# Prompt πŸ“\n" + prompt + "\n\n"
 
 
 
 
 
 
68
  combined_content += "# Response πŸ’¬\n" + response + "\n\n"
69
 
70
- # Extract Python code blocks from the response
71
- allcodeandcodeexec = ''
72
-
73
- resources = re.findall(r"```python([\s\S]*?)```", response)
74
  for resource in resources:
75
- combined_content += "# Code Results πŸš€\n"
76
-
77
- # Execute the Python code and capture output
78
- with io.StringIO() as buf:
79
- with redirect_stdout(buf):
80
- try:
81
- cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
82
- realtimeEvalResponse = "## Executing Code Block πŸš€\n" + "```" + cleaned_code + "```\n\n"
83
- # st.write(realtimeEvalResponse)
84
- combined_content += realtimeEvalResponse
85
- exec(cleaned_code.strip())
86
- code_output = buf.getvalue()
87
- realtimeEvalResponse = "## Executed Code Result πŸš€\n" + "```" + code_output + "```\n\n"
88
- # st.write(realtimeEvalResponse)
89
- combined_content += realtimeEvalResponse
90
- except Exception as e:
91
- realtimeEvalResponse = "## Executed Code Error πŸš€\n" + f"```python\nError executing Python code: {e}\n```\n\n"
92
- combined_content += realtimeEvalResponse
93
-
94
- st.markdown(combined_content)
95
-
 
 
 
 
 
 
 
 
96
  # Save the combined content to a Markdown file
97
  if should_save:
98
- with open(filename, 'w') as file:
99
  file.write(combined_content)
100
 
101
- # Create a Base64 encoded link for the file
102
- with open(filename, 'rb') as file:
103
- encoded_file = base64.b64encode(file.read()).decode()
104
- href = f'<a href="data:file/markdown;base64,{encoded_file}" download="{filename}">Download File πŸ“„</a>'
105
- st.markdown(href, unsafe_allow_html=True)
106
 
107
 
108
  # Read it aloud
 
64
  # Extract base filename without extension
65
  base_filename, ext = os.path.splitext(filename)
66
 
67
+ # Initialize the combined content
68
+ combined_content = ""
69
+
70
+ # Add Prompt with markdown title and emoji
71
+ combined_content += "# Prompt πŸ“\n" + prompt + "\n\n"
72
+
73
+ # Add Response with markdown title and emoji
74
  combined_content += "# Response πŸ’¬\n" + response + "\n\n"
75
 
76
+ # Check for code blocks in the response
77
+ resources = re.findall(r"```([\s\S]*?)```", response)
 
 
78
  for resource in resources:
79
+ # Check if the resource contains Python code
80
+ if "python" in resource.lower():
81
+ # Remove the 'python' keyword from the code block
82
+ cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
83
+
84
+ # Add Code Results title with markdown and emoji
85
+ combined_content += "# Code Results πŸš€\n"
86
+
87
+ # Redirect standard output to capture it
88
+ original_stdout = sys.stdout
89
+ sys.stdout = io.StringIO()
90
+
91
+ # Execute the cleaned Python code within the context
92
+ try:
93
+ exec(cleaned_code, context)
94
+ code_output = sys.stdout.getvalue()
95
+ combined_content += f"```\n{code_output}\n```\n\n"
96
+ realtimeEvalResponse = "# Code Results πŸš€\n" + "```" + code_output + "```\n\n"
97
+ st.write(realtimeEvalResponse)
98
+
99
+ except Exception as e:
100
+ combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
101
+
102
+ # Restore the original standard output
103
+ sys.stdout = original_stdout
104
+ else:
105
+ # Add non-Python resources with markdown and emoji
106
+ combined_content += "# Resource πŸ› οΈ\n" + "```" + resource + "```\n\n"
107
+
108
  # Save the combined content to a Markdown file
109
  if should_save:
110
+ with open(f"{base_filename}.md", 'w') as file:
111
  file.write(combined_content)
112
 
113
+ # Create a Base64 encoded link for the file
114
+ with open(f"{base_filename}.md", 'rb') as file:
115
+ encoded_file = base64.b64encode(file.read()).decode()
116
+ href = f'<a href="data:file/markdown;base64,{encoded_file}" download="{filename}">Download File πŸ“„</a>'
117
+ st.markdown(href, unsafe_allow_html=True)
118
 
119
 
120
  # Read it aloud