Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -135,10 +135,65 @@ MODEL = "gpt-4o-2024-05-13"
|
|
135 |
|
136 |
|
137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
|
140 |
# 5. Auto name generated output files from time and content
|
141 |
-
def
|
142 |
central = pytz.timezone('US/Central')
|
143 |
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
144 |
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
@@ -147,7 +202,7 @@ def generate_filename(prompt, file_type):
|
|
147 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
148 |
|
149 |
|
150 |
-
def
|
151 |
"""
|
152 |
Combines file name generation and file creation into one function.
|
153 |
If the file is a markdown file, extracts the title from the content (if available) and uses it for the filename.
|
|
|
135 |
|
136 |
|
137 |
|
138 |
+
# 5. Auto name generated output files from time and content
|
139 |
+
def generate_filename(prompt, file_type):
|
140 |
+
"""
|
141 |
+
Generates a safe filename using the prompt and file type.
|
142 |
+
It allows Unicode characters, including emojis, and replaces unsafe characters with spaces.
|
143 |
+
"""
|
144 |
+
# Get current time in the US/Central timezone
|
145 |
+
central = pytz.timezone('US/Central')
|
146 |
+
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
147 |
+
|
148 |
+
# Replace any unsafe characters with spaces, allow emojis and Unicode characters
|
149 |
+
replaced_prompt = re.sub(r'[<>:"/\\|?*\n]', ' ', prompt)
|
150 |
+
|
151 |
+
# Strip extra spaces from the start and end, and collapse multiple spaces
|
152 |
+
safe_prompt = re.sub(r'\s+', ' ', replaced_prompt).strip()[:240] # Limit length for filename safety
|
153 |
+
|
154 |
+
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
155 |
+
|
156 |
+
|
157 |
+
def create_and_save_file(content, file_type="md", prompt=None, is_image=False, should_save=True):
|
158 |
+
"""
|
159 |
+
Combines file name generation and file creation into one function.
|
160 |
+
If the file is a markdown file, extracts the title from the content (if available) and uses it for the filename.
|
161 |
+
"""
|
162 |
+
if not should_save:
|
163 |
+
return None
|
164 |
+
|
165 |
+
# Step 1: Generate filename based on the prompt or content
|
166 |
+
filename = generate_filename(prompt if prompt else content, file_type)
|
167 |
+
|
168 |
+
# Step 2: If it's a markdown file, check if it has a title (e.g., # Heading in markdown)
|
169 |
+
if file_type == "md":
|
170 |
+
title_from_content = extract_markdown_title(content)
|
171 |
+
if title_from_content:
|
172 |
+
filename = generate_filename(title_from_content, file_type)
|
173 |
+
|
174 |
+
# Step 3: Save the file
|
175 |
+
with open(filename, "w", encoding="utf-8") as f:
|
176 |
+
if is_image:
|
177 |
+
f.write(content)
|
178 |
+
else:
|
179 |
+
f.write(prompt + "\n\n" + content)
|
180 |
+
|
181 |
+
return filename
|
182 |
+
|
183 |
+
|
184 |
+
def extract_markdown_title(content):
|
185 |
+
"""
|
186 |
+
Extracts the first markdown title (line starting with '#') from the content.
|
187 |
+
"""
|
188 |
+
# Use regex to find the first line that starts with '#'
|
189 |
+
title_match = re.search(r'^\s*#\s*(.+)', content, re.MULTILINE)
|
190 |
+
if title_match:
|
191 |
+
return title_match.group(1).strip()
|
192 |
+
return None
|
193 |
|
194 |
|
195 |
# 5. Auto name generated output files from time and content
|
196 |
+
def generate_filename_old2(prompt, file_type):
|
197 |
central = pytz.timezone('US/Central')
|
198 |
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
199 |
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
|
|
202 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
203 |
|
204 |
|
205 |
+
def create_and_save_file_old2(content, file_type="md", prompt=None, is_image=False, should_save=True):
|
206 |
"""
|
207 |
Combines file name generation and file creation into one function.
|
208 |
If the file is a markdown file, extracts the title from the content (if available) and uses it for the filename.
|