Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import random
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
# Set page configuration
|
| 9 |
st.set_page_config(page_title="ChatGPT Prompt Generator", page_icon="π§ ", layout="wide")
|
|
@@ -185,11 +186,10 @@ It should be about {sel['about']}."""
|
|
| 185 |
|
| 186 |
return prompt
|
| 187 |
|
| 188 |
-
# Function to save prompt as markdown file
|
| 189 |
def save_prompt_as_md(prompt):
|
| 190 |
sel = st.session_state.selections
|
| 191 |
if not prompt.startswith("Please"):
|
| 192 |
-
# Create an autoname based on components
|
| 193 |
components = [
|
| 194 |
sel['role']['name'], sel['tone']['name'], sel['instruction']['name'],
|
| 195 |
sel['length']['name'], sel['content_type']['name'], sel['audience']['name'],
|
|
@@ -198,15 +198,37 @@ def save_prompt_as_md(prompt):
|
|
| 198 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 199 |
filename = f"prompt_{'_'.join(comp.lower().replace(' ', '_') for comp in components)}_{timestamp}.md"
|
| 200 |
|
| 201 |
-
# Ensure directory exists
|
| 202 |
os.makedirs("prompts", exist_ok=True)
|
| 203 |
|
| 204 |
-
# Save the file
|
| 205 |
with open(os.path.join("prompts", filename), "w") as f:
|
| 206 |
f.write(f"```markdown\n{prompt}\n```")
|
| 207 |
return filename
|
| 208 |
return None
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
# Header
|
| 211 |
st.markdown("<h2 style='text-align: center; font-size: 1.3rem; margin-bottom: 1rem;'>π§ ChatGPT Prompt Generator</h2>", unsafe_allow_html=True)
|
| 212 |
|
|
@@ -255,8 +277,7 @@ with col2:
|
|
| 255 |
st.write(prompt)
|
| 256 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 257 |
|
| 258 |
-
|
| 259 |
-
button_cols = st.columns(4) # Increased to 4 columns to accommodate Run button
|
| 260 |
with button_cols[0]:
|
| 261 |
if st.button("π Copy", type="secondary", use_container_width=True):
|
| 262 |
st.code(prompt, language="")
|
|
@@ -277,28 +298,21 @@ with col2:
|
|
| 277 |
with button_cols[3]:
|
| 278 |
if st.button("π Run", type="primary", use_container_width=True):
|
| 279 |
if not prompt.startswith("Please"):
|
| 280 |
-
# Display as Markdown code block
|
| 281 |
st.code(f"```markdown\n{prompt}\n```", language="markdown")
|
| 282 |
-
|
| 283 |
-
# Save to file
|
| 284 |
filename = save_prompt_as_md(prompt)
|
| 285 |
if filename:
|
| 286 |
st.success(f"Saved as {filename}")
|
| 287 |
-
|
| 288 |
-
# Add to history
|
| 289 |
st.session_state.prompt_history.append({
|
| 290 |
"prompt": prompt,
|
| 291 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 292 |
"filename": filename
|
| 293 |
})
|
| 294 |
|
| 295 |
-
# Live updating copy-paste code block
|
| 296 |
st.markdown("<div class='category-header'>π Live Prompt Code</div>", unsafe_allow_html=True)
|
| 297 |
st.code(f"```markdown\n{prompt}\n```", language="markdown")
|
| 298 |
|
| 299 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 300 |
|
| 301 |
-
# Prompt History
|
| 302 |
st.markdown("<div class='category-header'>π Prompt History</div>", unsafe_allow_html=True)
|
| 303 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 304 |
if st.session_state.prompt_history:
|
|
@@ -310,7 +324,6 @@ with col2:
|
|
| 310 |
st.write("No prompts generated yet.")
|
| 311 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 312 |
|
| 313 |
-
# Examples Section
|
| 314 |
st.markdown("<div class='category-header'>π Examples</div>", unsafe_allow_html=True)
|
| 315 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 316 |
|
|
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
# Set page configuration
|
| 10 |
st.set_page_config(page_title="ChatGPT Prompt Generator", page_icon="π§ ", layout="wide")
|
|
|
|
| 186 |
|
| 187 |
return prompt
|
| 188 |
|
| 189 |
+
# Function to save prompt as markdown file and return filename
|
| 190 |
def save_prompt_as_md(prompt):
|
| 191 |
sel = st.session_state.selections
|
| 192 |
if not prompt.startswith("Please"):
|
|
|
|
| 193 |
components = [
|
| 194 |
sel['role']['name'], sel['tone']['name'], sel['instruction']['name'],
|
| 195 |
sel['length']['name'], sel['content_type']['name'], sel['audience']['name'],
|
|
|
|
| 198 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 199 |
filename = f"prompt_{'_'.join(comp.lower().replace(' ', '_') for comp in components)}_{timestamp}.md"
|
| 200 |
|
|
|
|
| 201 |
os.makedirs("prompts", exist_ok=True)
|
| 202 |
|
|
|
|
| 203 |
with open(os.path.join("prompts", filename), "w") as f:
|
| 204 |
f.write(f"```markdown\n{prompt}\n```")
|
| 205 |
return filename
|
| 206 |
return None
|
| 207 |
|
| 208 |
+
# Function to get list of saved .md files
|
| 209 |
+
def get_saved_md_files():
|
| 210 |
+
os.makedirs("prompts", exist_ok=True)
|
| 211 |
+
return [f for f in os.listdir("prompts") if f.endswith(".md")]
|
| 212 |
+
|
| 213 |
+
# Function to generate Base64 download link
|
| 214 |
+
def get_base64_download_link(filepath):
|
| 215 |
+
with open(filepath, "rb") as f:
|
| 216 |
+
data = f.read()
|
| 217 |
+
b64 = base64.b64encode(data).decode()
|
| 218 |
+
filename = os.path.basename(filepath)
|
| 219 |
+
return f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">Download {filename}</a>'
|
| 220 |
+
|
| 221 |
+
# Sidebar for File History
|
| 222 |
+
with st.sidebar:
|
| 223 |
+
st.markdown("### π Saved Prompts")
|
| 224 |
+
md_files = get_saved_md_files()
|
| 225 |
+
if md_files:
|
| 226 |
+
for md_file in sorted(md_files, reverse=True): # Newest first
|
| 227 |
+
filepath = os.path.join("prompts", md_file)
|
| 228 |
+
st.markdown(f"- {get_base64_download_link(filepath)}", unsafe_allow_html=True)
|
| 229 |
+
else:
|
| 230 |
+
st.write("No saved prompts yet.")
|
| 231 |
+
|
| 232 |
# Header
|
| 233 |
st.markdown("<h2 style='text-align: center; font-size: 1.3rem; margin-bottom: 1rem;'>π§ ChatGPT Prompt Generator</h2>", unsafe_allow_html=True)
|
| 234 |
|
|
|
|
| 277 |
st.write(prompt)
|
| 278 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 279 |
|
| 280 |
+
button_cols = st.columns(4)
|
|
|
|
| 281 |
with button_cols[0]:
|
| 282 |
if st.button("π Copy", type="secondary", use_container_width=True):
|
| 283 |
st.code(prompt, language="")
|
|
|
|
| 298 |
with button_cols[3]:
|
| 299 |
if st.button("π Run", type="primary", use_container_width=True):
|
| 300 |
if not prompt.startswith("Please"):
|
|
|
|
| 301 |
st.code(f"```markdown\n{prompt}\n```", language="markdown")
|
|
|
|
|
|
|
| 302 |
filename = save_prompt_as_md(prompt)
|
| 303 |
if filename:
|
| 304 |
st.success(f"Saved as {filename}")
|
|
|
|
|
|
|
| 305 |
st.session_state.prompt_history.append({
|
| 306 |
"prompt": prompt,
|
| 307 |
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 308 |
"filename": filename
|
| 309 |
})
|
| 310 |
|
|
|
|
| 311 |
st.markdown("<div class='category-header'>π Live Prompt Code</div>", unsafe_allow_html=True)
|
| 312 |
st.code(f"```markdown\n{prompt}\n```", language="markdown")
|
| 313 |
|
| 314 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 315 |
|
|
|
|
| 316 |
st.markdown("<div class='category-header'>π Prompt History</div>", unsafe_allow_html=True)
|
| 317 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 318 |
if st.session_state.prompt_history:
|
|
|
|
| 324 |
st.write("No prompts generated yet.")
|
| 325 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 326 |
|
|
|
|
| 327 |
st.markdown("<div class='category-header'>π Examples</div>", unsafe_allow_html=True)
|
| 328 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 329 |
|