awacke1 commited on
Commit
cab89dd
Β·
1 Parent(s): 61877a2

Update backup.py

Browse files
Files changed (1) hide show
  1. backup.py +46 -28
backup.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
@@ -106,40 +108,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
- # Step 4: Write files based on type
110
- if ext in ['.txt', '.htm', '.md']:
111
- # Create Prompt file
112
- with open(f"{base_filename}-Prompt.txt", 'w') as file:
113
- file.write(prompt)
114
-
115
- # Create Response file
116
- with open(f"{base_filename}-Response.md", 'w') as file:
117
- file.write(response)
118
 
119
- # Create Code file if Python code is present
120
- if has_python_code:
121
- # Extract Python code from the response
122
- python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
123
 
124
- with open(f"{base_filename}-Code.py", 'w') as file:
125
- file.write(python_code)
126
 
 
 
 
 
 
 
 
 
 
 
127
 
128
- def create_file_old(filename, prompt, response, should_save=True):
129
- if not should_save:
130
- return
131
- if filename.endswith(".txt"):
132
- with open(filename, 'w') as file:
133
- file.write(f"{prompt}\n{response}")
134
- elif filename.endswith(".htm"):
135
- with open(filename, 'w') as file:
136
- file.write(f"{prompt} {response}")
137
- elif filename.endswith(".md"):
138
- with open(filename, 'w') as file:
139
- file.write(f"{prompt}\n\n{response}")
 
 
140
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  def truncate_document(document, length):
142
  return document[:length]
 
143
  def divide_document(document, max_length):
144
  return [document[i:i+max_length] for i in range(0, len(document), max_length)]
145
 
@@ -225,7 +243,7 @@ def readitaloud(result):
225
  </html>
226
  '''
227
 
228
- components.html(documentHTML5, width=1280, height=1024)
229
  #return result
230
 
231
  def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):
 
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
 
108
  # Step 3: Check if the response contains Python code
109
  has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
110
 
111
+ # Step 4: Initialize the combined content
112
+ combined_content = ""
 
 
 
 
 
 
 
113
 
114
+ # Add Prompt with markdown title and emoji
115
+ combined_content += "# Prompt πŸ“\n" + prompt + "\n\n"
 
 
116
 
117
+ # Add Response with markdown title and emoji
118
+ combined_content += "# Response πŸ’¬\n" + response + "\n\n"
119
 
120
+ # Check for Python code or other resources and add them with markdown title and emoji
121
+ resources = re.findall(r"```([\s\S]*?)```", response)
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
 
131
+ # Capture standard output
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
+ realtimeEvalResponse = "# Code Results πŸš€\n" + "```" + code_output + "```\n\n"
141
+ st.write(realtimeEvalResponse)
142
+
143
+ except Exception as e:
144
+ combined_content += f"```python\nError executing Python code: {e}\n```\n\n"
145
 
146
+ # Restore the original standard output
147
+ sys.stdout = original_stdout
148
+ else:
149
+ # Add Resource title with markdown and emoji for non-Python resources
150
+ combined_content += "# Resource πŸ› οΈ\n" + "```" + resource + "```\n\n"
151
+
152
+ # Write the combined content into one file
153
+ with open(f"{base_filename}-Combined.md", 'w') as file:
154
+ file.write(combined_content)
155
+
156
+
157
+
158
  def truncate_document(document, length):
159
  return document[:length]
160
+
161
  def divide_document(document, max_length):
162
  return [document[i:i+max_length] for i in range(0, len(document), max_length)]
163
 
 
243
  </html>
244
  '''
245
 
246
+ components.html(documentHTML5, width=800, height=300)
247
  #return result
248
 
249
  def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):