awacke1 commited on
Commit
34a42c9
Β·
1 Parent(s): efe9bd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -3,11 +3,13 @@ import openai
3
  import os
4
  import base64
5
  import glob
 
6
  import json
7
  import mistune
8
  import pytz
9
  import math
10
  import requests
 
11
  import time
12
  import re
13
  import textract
@@ -96,6 +98,7 @@ def save_and_play_audio(audio_recorder):
96
  return filename
97
  return None
98
 
 
99
  def create_file(filename, prompt, response, should_save=True):
100
  if not should_save:
101
  return
@@ -106,6 +109,56 @@ def create_file(filename, prompt, response, should_save=True):
106
  # Step 3: Check if the response contains Python code
107
  has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  # Step 4: Write files based on type
111
  if ext in ['.txt', '.htm', '.md']:
 
3
  import os
4
  import base64
5
  import glob
6
+ import io
7
  import json
8
  import mistune
9
  import pytz
10
  import math
11
  import requests
12
+ import sys
13
  import time
14
  import re
15
  import textract
 
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
 
109
  # Step 3: Check if the response contains Python code
110
  has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
111
 
112
+ # Step 4: Initialize the combined content
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 is Python code
125
+ if "python" in resource.lower():
126
+ # Add Code Results title with markdown and emoji
127
+ combined_content += "# Code Results πŸš€\n"
128
+
129
+ # Capture standard output
130
+ original_stdout = sys.stdout
131
+ sys.stdout = io.StringIO()
132
+
133
+ # Execute Python code and capture the output
134
+ try:
135
+ exec(resource)
136
+ code_output = sys.stdout.getvalue()
137
+ combined_content += f"```\n{code_output}\n```\n\n"
138
+ except Exception as e:
139
+ combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
140
+
141
+ # Restore the original standard output
142
+ sys.stdout = original_stdout
143
+ else:
144
+ # Add Resource title with markdown and emoji for non-Python resources
145
+ combined_content += "# Resource πŸ› οΈ\n" + "```" + resource + "```\n\n"
146
+
147
+ # Write the combined content into one file
148
+ with open(f"{base_filename}-Combined.md", 'w') as file:
149
+ file.write(combined_content)
150
+
151
+
152
+ def create_file_old(filename, prompt, response, should_save=True):
153
+ if not should_save:
154
+ return
155
+
156
+ # Step 2: Extract base filename without extension
157
+ base_filename, ext = os.path.splitext(filename)
158
+
159
+ # Step 3: Check if the response contains Python code
160
+ has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
161
+
162
 
163
  # Step 4: Write files based on type
164
  if ext in ['.txt', '.htm', '.md']: