Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -122,6 +122,9 @@ def create_file(filename, prompt, response, should_save=True):
|
|
122 |
for resource in resources:
|
123 |
# Check if the resource contains Python code
|
124 |
if "python" in resource.lower():
|
|
|
|
|
|
|
125 |
# Add Code Results title with markdown and emoji
|
126 |
combined_content += "# Code Results π\n"
|
127 |
|
@@ -129,9 +132,9 @@ def create_file(filename, prompt, response, should_save=True):
|
|
129 |
original_stdout = sys.stdout
|
130 |
sys.stdout = io.StringIO()
|
131 |
|
132 |
-
# Execute Python code and capture the output
|
133 |
try:
|
134 |
-
exec(
|
135 |
code_output = sys.stdout.getvalue()
|
136 |
combined_content += f"```\n{code_output}\n```\n\n"
|
137 |
except Exception as e:
|
@@ -145,58 +148,13 @@ def create_file(filename, prompt, response, should_save=True):
|
|
145 |
|
146 |
# Write the combined content into one file
|
147 |
with open(f"{base_filename}-Combined.md", 'w') as file:
|
148 |
-
file.write(combined_content)
|
149 |
-
|
150 |
-
|
151 |
-
def create_file_old(filename, prompt, response, should_save=True):
|
152 |
-
if not should_save:
|
153 |
-
return
|
154 |
-
|
155 |
-
# Step 2: Extract base filename without extension
|
156 |
-
base_filename, ext = os.path.splitext(filename)
|
157 |
-
|
158 |
-
# Step 3: Check if the response contains Python code
|
159 |
-
has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
|
160 |
|
161 |
-
|
162 |
-
# Step 4: Write files based on type
|
163 |
-
if ext in ['.txt', '.htm', '.md']:
|
164 |
-
# Initialize the combined content
|
165 |
-
combined_content = ""
|
166 |
-
|
167 |
-
# Add Prompt with markdown title and emoji
|
168 |
-
combined_content += "# Prompt π\n" + prompt + "\n\n"
|
169 |
-
|
170 |
-
# Add Response with markdown title and emoji
|
171 |
-
combined_content += "# Response π¬\n" + response + "\n\n"
|
172 |
-
|
173 |
-
# Check for Python code or other resources and add them with markdown title and emoji
|
174 |
-
resources = re.findall(r"```([\s\S]*?)```", response)
|
175 |
-
for resource in resources:
|
176 |
-
# Add Resource title with markdown and emoji
|
177 |
-
combined_content += "# Resource π οΈ\n" + "```" + resource + "```\n\n"
|
178 |
-
|
179 |
-
# Write the combined content into one file
|
180 |
-
with open(f"{base_filename}-Combined.md", 'w') as file:
|
181 |
-
file.write(combined_content)
|
182 |
|
183 |
|
184 |
-
|
185 |
-
def create_file_old(filename, prompt, response, should_save=True):
|
186 |
-
if not should_save:
|
187 |
-
return
|
188 |
-
if filename.endswith(".txt"):
|
189 |
-
with open(filename, 'w') as file:
|
190 |
-
file.write(f"{prompt}\n{response}")
|
191 |
-
elif filename.endswith(".htm"):
|
192 |
-
with open(filename, 'w') as file:
|
193 |
-
file.write(f"{prompt} {response}")
|
194 |
-
elif filename.endswith(".md"):
|
195 |
-
with open(filename, 'w') as file:
|
196 |
-
file.write(f"{prompt}\n\n{response}")
|
197 |
-
|
198 |
def truncate_document(document, length):
|
199 |
return document[:length]
|
|
|
200 |
def divide_document(document, max_length):
|
201 |
return [document[i:i+max_length] for i in range(0, len(document), max_length)]
|
202 |
|
|
|
122 |
for resource in resources:
|
123 |
# Check if the resource contains Python code
|
124 |
if "python" in resource.lower():
|
125 |
+
# Remove the word 'python' from the beginning of the code block
|
126 |
+
cleaned_code = re.sub(r'^\s*python', '', resource, flags=re.IGNORECASE | re.MULTILINE)
|
127 |
+
|
128 |
# Add Code Results title with markdown and emoji
|
129 |
combined_content += "# Code Results π\n"
|
130 |
|
|
|
132 |
original_stdout = sys.stdout
|
133 |
sys.stdout = io.StringIO()
|
134 |
|
135 |
+
# Execute cleaned Python code and capture the output
|
136 |
try:
|
137 |
+
exec(cleaned_code)
|
138 |
code_output = sys.stdout.getvalue()
|
139 |
combined_content += f"```\n{code_output}\n```\n\n"
|
140 |
except Exception as e:
|
|
|
148 |
|
149 |
# Write the combined content into one file
|
150 |
with open(f"{base_filename}-Combined.md", 'w') as file:
|
151 |
+
file.write(combined_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
def truncate_document(document, length):
|
156 |
return document[:length]
|
157 |
+
|
158 |
def divide_document(document, max_length):
|
159 |
return [document[i:i+max_length] for i in range(0, len(document), max_length)]
|
160 |
|