awacke1 commited on
Commit
1e25101
·
1 Parent(s): 0d4196c

Update backupapp.py

Browse files
Files changed (1) hide show
  1. backupapp.py +37 -3
backupapp.py CHANGED
@@ -32,14 +32,20 @@ from templates import css, bot_template, user_template
32
 
33
  # page config and sidebar declares up front allow all other functions to see global class variables
34
  st.set_page_config(page_title="GPT Streamlit Document Reasoner", layout="wide")
35
- should_save = st.sidebar.checkbox("💾 Save")
36
 
37
- def generate_filename(prompt, file_type):
38
  central = pytz.timezone('US/Central')
39
  safe_date_time = datetime.now(central).strftime("%m%d_%H%M") # Date and time DD-HHMM
40
  safe_prompt = "".join(x for x in prompt if x.isalnum())[:90] # Limit file name size and trim whitespace
41
  return f"{safe_date_time}_{safe_prompt}.{file_type}" # Return a safe file name
42
 
 
 
 
 
 
 
43
 
44
  def transcribe_audio(openai_key, file_path, model):
45
  OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
@@ -77,6 +83,35 @@ def save_and_play_audio(audio_recorder):
77
  return None
78
 
79
  def create_file(filename, prompt, response, should_save=True):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  if not should_save:
81
  return
82
  if filename.endswith(".txt"):
@@ -484,4 +519,3 @@ with st.sidebar:
484
  filename = generate_filename(raw, 'txt')
485
  create_file(filename, raw, '', should_save)
486
  #create_file(filename, raw, '')
487
-
 
32
 
33
  # page config and sidebar declares up front allow all other functions to see global class variables
34
  st.set_page_config(page_title="GPT Streamlit Document Reasoner", layout="wide")
35
+ should_save = st.sidebar.checkbox("💾 Save", value=True)
36
 
37
+ def generate_filename_old(prompt, file_type):
38
  central = pytz.timezone('US/Central')
39
  safe_date_time = datetime.now(central).strftime("%m%d_%H%M") # Date and time DD-HHMM
40
  safe_prompt = "".join(x for x in prompt if x.isalnum())[:90] # Limit file name size and trim whitespace
41
  return f"{safe_date_time}_{safe_prompt}.{file_type}" # Return a safe file name
42
 
43
+ def generate_filename(prompt, file_type):
44
+ central = pytz.timezone('US/Central')
45
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
46
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
47
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
48
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
49
 
50
  def transcribe_audio(openai_key, file_path, model):
51
  OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
 
83
  return None
84
 
85
  def create_file(filename, prompt, response, should_save=True):
86
+ if not should_save:
87
+ return
88
+
89
+ # Step 2: Extract base filename without extension
90
+ base_filename, ext = os.path.splitext(filename)
91
+
92
+ # Step 3: Check if the response contains Python code
93
+ has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
94
+
95
+ # Step 4: Write files based on type
96
+ if ext in ['.txt', '.htm', '.md']:
97
+ # Create Prompt file
98
+ with open(f"{base_filename}-Prompt.txt", 'w') as file:
99
+ file.write(prompt)
100
+
101
+ # Create Response file
102
+ with open(f"{base_filename}-Response.md", 'w') as file:
103
+ file.write(response)
104
+
105
+ # Create Code file if Python code is present
106
+ if has_python_code:
107
+ # Extract Python code from the response
108
+ python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
109
+
110
+ with open(f"{base_filename}-Code.py", 'w') as file:
111
+ file.write(python_code)
112
+
113
+
114
+ def create_file_old(filename, prompt, response, should_save=True):
115
  if not should_save:
116
  return
117
  if filename.endswith(".txt"):
 
519
  filename = generate_filename(raw, 'txt')
520
  create_file(filename, raw, '', should_save)
521
  #create_file(filename, raw, '')