Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import random | |
| import json | |
| import os | |
| from datetime import datetime | |
| import base64 | |
| # Set page configuration | |
| st.set_page_config(page_title="PromptWizardry", page_icon="๐ง ", layout="wide") | |
| # Custom CSS for styling | |
| st.markdown(""" | |
| <style> | |
| .main {background-color: #f8f9fa;} | |
| div[data-testid="stVerticalBlock"] {gap: 0.2rem;} | |
| div[data-testid="stHorizontalBlock"] {gap: 0.3rem;} | |
| .stTextArea textarea, .stTextInput input { | |
| padding: 0.3rem; | |
| font-size: 0.85em; | |
| } | |
| h1, h2, h3 {margin-top: 0; margin-bottom: 0.2rem;} | |
| p, div {margin-bottom: 0.1rem;} | |
| .category-header { | |
| font-weight: bold; | |
| font-size: 1rem; | |
| margin-bottom: 0.2rem; | |
| color: #333; | |
| background-color: #f1f8ff; | |
| padding: 3px 8px; | |
| border-radius: 4px; | |
| } | |
| .prompt-display { | |
| background-color: #ffffff; | |
| border-radius: 4px; | |
| padding: 12px; | |
| border: 1px solid #e9ecef; | |
| min-height: 150px; | |
| white-space: pre-wrap; | |
| font-size: 0.85em; | |
| } | |
| .st-emotion-cache-1nv1tyf { | |
| font-size: 0.85rem; | |
| } | |
| .dataframe-container { | |
| background-color: white; | |
| border: 1px solid #e6e6e6; | |
| border-radius: 4px; | |
| padding: 8px; | |
| margin-bottom: 8px; | |
| } | |
| .selected-item { | |
| background-color: #007bff !important; | |
| color: white !important; | |
| font-weight: bold; | |
| padding: 2px 6px; | |
| border-radius: 4px; | |
| } | |
| .unselected-item { | |
| background-color: #f8f9fa; | |
| padding: 2px 6px; | |
| border-radius: 4px; | |
| cursor: pointer; | |
| } | |
| .unselected-item:hover { | |
| background-color: #e9ecef; | |
| } | |
| table { | |
| font-size: 0.85rem; | |
| } | |
| .clickable { | |
| cursor: pointer; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Initialize session state with default topic | |
| if 'selections' not in st.session_state: | |
| st.session_state.selections = { | |
| 'role': None, 'tone': None, 'instruction': None, 'length': None, | |
| 'content_type': None, 'audience': None, 'format': None, | |
| 'about': "DistillKit, MergeKit, Spectrum for ML Model Building from input Spreadsheets using torch, transformers and Streamlit", | |
| 'inclusion': "", 'exclusion': "", 'input_data': "" | |
| } | |
| if 'prompt_history' not in st.session_state: | |
| st.session_state.prompt_history = [] | |
| # Data sets with emoji and names | |
| data = { | |
| 'roles': [ | |
| {"name": "Professional", "emoji": "๐"}, {"name": "Expert", "emoji": "๐ง "}, | |
| {"name": "Friend", "emoji": "๐ค"}, {"name": "Copywriter", "emoji": "โ๏ธ"}, | |
| {"name": "Creative Writer", "emoji": "๐๏ธ"}, {"name": "Sales Coach", "emoji": "๐ผ"}, | |
| {"name": "Marketing Coach", "emoji": "๐"}, {"name": "Tech Consultant", "emoji": "๐ป"}, | |
| {"name": "Life Coach", "emoji": "๐ง"}, {"name": "Data Analyst", "emoji": "๐"}, | |
| {"name": "Influencer", "emoji": "๐ฑ"}, {"name": "Language Tutor", "emoji": "๐ฃ๏ธ"}, | |
| {"name": "Fitness Trainer", "emoji": "๐ช"}, {"name": "Teacher", "emoji": "๐จโ๐ซ"}, | |
| {"name": "Therapist", "emoji": "๐ง"}, {"name": "Detective", "emoji": "๐"} | |
| ], | |
| 'tones': [ | |
| {"name": "Informative", "emoji": "โน๏ธ"}, {"name": "Inspirational", "emoji": "โจ"}, | |
| {"name": "Humorous", "emoji": "๐"}, {"name": "Friendly", "emoji": "๐"}, | |
| {"name": "Professional", "emoji": "๐"}, {"name": "Casual", "emoji": "๐"}, | |
| {"name": "Persuasive", "emoji": "๐ค"}, {"name": "Encouraging", "emoji": "๐"}, | |
| {"name": "Empathetic", "emoji": "๐ค"}, {"name": "Serious", "emoji": "๐"}, | |
| {"name": "Enthusiastic", "emoji": "๐คฉ"}, {"name": "Thoughtful", "emoji": "๐ญ"} | |
| ], | |
| 'instructions': [ | |
| {"name": "Create", "emoji": "๐จ"}, {"name": "Suggest", "emoji": "๐ก"}, | |
| {"name": "Write", "emoji": "โ๏ธ"}, {"name": "Compose", "emoji": "๐"}, | |
| {"name": "Analyze", "emoji": "๐"}, {"name": "Explain", "emoji": "๐"}, | |
| {"name": "Describe", "emoji": "๐"}, {"name": "Summarize", "emoji": "๐"}, | |
| {"name": "Compare", "emoji": "โ๏ธ"}, {"name": "Outline", "emoji": "๐"}, | |
| {"name": "Evaluate", "emoji": "โญ"}, {"name": "List", "emoji": "๐"} | |
| ], | |
| 'lengths': [ | |
| {"name": "300 Words", "emoji": "๐"}, {"name": "500 Words", "emoji": "๐"}, | |
| {"name": "Short", "emoji": "๐ฉณ"}, {"name": "Medium", "emoji": "๐"}, | |
| {"name": "Long", "emoji": "๐"}, {"name": "Brief", "emoji": "๐จ"}, | |
| {"name": "Detailed", "emoji": "๐"}, {"name": "Comprehensive", "emoji": "๐"} | |
| ], | |
| 'content_types': [ | |
| {"name": "Article", "emoji": "๐ฐ"}, {"name": "Blog post", "emoji": "๐"}, | |
| {"name": "Guide", "emoji": "๐"}, {"name": "Email", "emoji": "๐ง"}, | |
| {"name": "Summary", "emoji": "๐"}, {"name": "Story", "emoji": "๐"}, | |
| {"name": "Essay", "emoji": "๐"}, {"name": "Review", "emoji": "โญ"}, | |
| {"name": "Tutorial", "emoji": "๐จโ๐ซ"}, {"name": "Report", "emoji": "๐"}, | |
| {"name": "Plan", "emoji": "๐"}, {"name": "Script", "emoji": "๐ฌ"} | |
| ], | |
| 'audiences': [ | |
| {"name": "Beginners", "emoji": "๐ฑ"}, {"name": "Experts", "emoji": "๐ง "}, | |
| {"name": "Students", "emoji": "๐"}, {"name": "Professionals", "emoji": "๐"}, | |
| {"name": "Business Owners", "emoji": "๐ผ"}, {"name": "General Public", "emoji": "๐ฅ"}, | |
| {"name": "Developers", "emoji": "๐ป"}, {"name": "Children", "emoji": "๐ถ"} | |
| ], | |
| 'formats': [ | |
| {"name": "Markdown", "emoji": "๐"}, {"name": "HTML", "emoji": "๐"}, | |
| {"name": "Plain Text", "emoji": "๐"}, {"name": "JSON", "emoji": "๐"}, | |
| {"name": "PDF", "emoji": "๐"}, {"name": "Python Code", "emoji": "๐"}, | |
| {"name": "JavaScript", "emoji": "๐"}, {"name": "SQL Query", "emoji": "๐พ"}, | |
| {"name": "Image Gen Prompt", "emoji": "๐ผ๏ธ"}, {"name": "Video Gen Prompt", "emoji": "๐ฅ"}, | |
| {"name": "Song Gen Prompt", "emoji": "๐ต"}, {"name": "Story Gen Prompt", "emoji": "๐"} | |
| ] | |
| } | |
| # Function to handle selection | |
| def handle_selection(category, item): | |
| st.session_state.selections[category] = item | |
| # Function to create category selection with buttons | |
| def create_dataframe_category(category, title, emoji_prefix): | |
| st.markdown(f"<div class='category-header'>{emoji_prefix} {title}</div>", unsafe_allow_html=True) | |
| st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True) | |
| cols = st.columns(4) | |
| for i, item in enumerate(data[f"{category}s"]): | |
| col = cols[i % 4] | |
| label = f"{item['emoji']} {item['name']}" | |
| is_selected = st.session_state.selections[category] == item | |
| with col: | |
| if st.button(label, key=f"{category}_{i}", | |
| type="primary" if is_selected else "secondary", | |
| use_container_width=True): | |
| handle_selection(category, item) | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| # Function to generate the final prompt | |
| def generate_prompt(): | |
| sel = st.session_state.selections | |
| if not all([sel['role'], sel['tone'], sel['instruction'], sel['length'], | |
| sel['content_type'], sel['audience'], sel['format']]): | |
| return "Please select all required components." | |
| prompt = f"""Act as a {sel['role']['emoji']} {sel['role']['name']}, use {sel['tone']['emoji']} {sel['tone']['name']} tone, {sel['instruction']['emoji']} {sel['instruction']['name']} a {sel['length']['emoji']} {sel['length']['name']} {sel['content_type']['emoji']} {sel['content_type']['name']} for {sel['audience']['emoji']} {sel['audience']['name']}. | |
| It should be about {sel['about']}.""" | |
| if sel['inclusion']: | |
| prompt += f"\nInclude {sel['inclusion']}." | |
| if sel['exclusion']: | |
| prompt += f"\nExclude {sel['exclusion']}." | |
| prompt += f"\n\nReturn the output as {sel['format']['emoji']} {sel['format']['name']}." | |
| if sel['input_data']: | |
| prompt += f"\nUse the following information: {sel['input_data']}" | |
| return prompt | |
| # Function to save prompt as markdown file and return filename | |
| def save_prompt_as_md(prompt): | |
| sel = st.session_state.selections | |
| if not prompt.startswith("Please"): | |
| components = [ | |
| sel['role']['name'], sel['tone']['name'], sel['instruction']['name'], | |
| sel['length']['name'], sel['content_type']['name'], sel['audience']['name'], | |
| sel['format']['name'] | |
| ] | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| filename = f"prompt_{'_'.join(comp.lower().replace(' ', '_') for comp in components)}_{timestamp}.md" | |
| os.makedirs("prompts", exist_ok=True) | |
| with open(os.path.join("prompts", filename), "w") as f: | |
| f.write(f"```markdown\n{prompt}\n```") | |
| return filename | |
| return None | |
| # Function to get list of saved .md files | |
| def get_saved_md_files(): | |
| os.makedirs("prompts", exist_ok=True) | |
| return [f for f in os.listdir("prompts") if f.endswith(".md")] | |
| # Function to generate Base64 download link | |
| def get_base64_download_link(filepath): | |
| with open(filepath, "rb") as f: | |
| data = f.read() | |
| b64 = base64.b64encode(data).decode() | |
| filename = os.path.basename(filepath) | |
| return f'<a href="data:application/octet-stream;base64,{b64}" download="{filename}">Download {filename}</a>' | |
| # Sidebar with Saved Prompts and Feature Outline | |
| with st.sidebar: | |
| st.markdown("### ๐ Saved Prompts") | |
| md_files = get_saved_md_files() | |
| if md_files: | |
| for md_file in sorted(md_files, reverse=True): | |
| filepath = os.path.join("prompts", md_file) | |
| st.markdown(f"- {get_base64_download_link(filepath)}", unsafe_allow_html=True) | |
| else: | |
| st.write("No saved prompts yet.") | |
| st.markdown("### ๐ PromptWizardry Magic") | |
| st.markdown(""" | |
| - ๐ **Prompt Alchemy**: Craft with roles, tones, & more! | |
| - ๐ช **Actions**: Copy, Reset, Random, Run! | |
| - ๐พ **Scroll Vault**: Save & download `.md` spells! | |
| - โณ **Chronicles**: Relive your magic history! | |
| - ๐ **Extras**: Sample spells & dazzling UI! | |
| """) | |
| # Header | |
| st.markdown("<h2 style='text-align: center; font-size: 1.3rem; margin-bottom: 1rem;'>โจ PromptWizardry</h2>", unsafe_allow_html=True) | |
| # Main layout with two columns | |
| col1, col2 = st.columns([3, 1]) | |
| with col1: | |
| create_dataframe_category("role", "Choose a Role", "๐ค") | |
| create_dataframe_category("tone", "Select a Tone", "๐ญ") | |
| create_dataframe_category("instruction", "Select an Instruction", "๐") | |
| create_dataframe_category("length", "Select Length", "๐") | |
| create_dataframe_category("content_type", "Select Content Type", "๐") | |
| create_dataframe_category("audience", "Select Target Audience", "๐ฅ") | |
| create_dataframe_category("format", "Select Format", "๐") | |
| st.markdown("<div class='category-header'>๐ Additional Details</div>", unsafe_allow_html=True) | |
| st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True) | |
| st.session_state.selections['about'] = st.text_input("๐ฌ Topic", | |
| value=st.session_state.selections['about'], | |
| placeholder="Enter what the content should be about") | |
| include_exclude_cols = st.columns(2) | |
| with include_exclude_cols[0]: | |
| st.session_state.selections['inclusion'] = st.text_input("โ Include", | |
| value=st.session_state.selections['inclusion'], | |
| placeholder="What to include in the content") | |
| with include_exclude_cols[1]: | |
| st.session_state.selections['exclusion'] = st.text_input("โ Exclude", | |
| value=st.session_state.selections['exclusion'], | |
| placeholder="What to exclude from the content") | |
| st.session_state.selections['input_data'] = st.text_area("๐ Input Data", | |
| value=st.session_state.selections['input_data'], | |
| placeholder="Enter any specific information to use", | |
| height=100) | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| with col2: | |
| prompt = generate_prompt() | |
| st.markdown("<div class='category-header'>๐ฎ Generated Prompt</div>", unsafe_allow_html=True) | |
| st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True) | |
| st.text_area("", value=prompt, height=150, key="generated_prompt", disabled=True, | |
| help="Your magical prompt appears here!") | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| button_cols = st.columns(4) | |
| with button_cols[0]: | |
| if st.button("๐ Copy", type="secondary", use_container_width=True): | |
| st.code(prompt, language="") | |
| with button_cols[1]: | |
| if st.button("๐ Reset", type="secondary", use_container_width=True): | |
| for key in st.session_state.selections: | |
| if key in ['about', 'inclusion', 'exclusion', 'input_data']: | |
| st.session_state.selections[key] = "" if key != 'about' else "DistillKit, MergeKit, Spectrum for ML Model Building from input Spreadsheets using torch, transformers and Streamlit" | |
| else: | |
| st.session_state.selections[key] = None | |
| st.session_state.prompt_history = [] | |
| st.rerun() | |
| with button_cols[2]: | |
| if st.button("๐ฒ Random", type="secondary", use_container_width=True): | |
| for category in ['role', 'tone', 'instruction', 'length', 'content_type', 'audience', 'format']: | |
| st.session_state.selections[category] = random.choice(data[category+'s']) | |
| st.rerun() | |
| with button_cols[3]: | |
| if st.button("๐ Run", type="primary", use_container_width=True): | |
| if not prompt.startswith("Please"): | |
| st.code(f"```markdown\n{prompt}\n```", language="markdown") | |
| filename = save_prompt_as_md(prompt) | |
| if filename: | |
| st.success(f"Saved as {filename}") | |
| st.session_state.prompt_history.append({ | |
| "prompt": prompt, | |
| "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
| "filename": filename | |
| }) | |
| st.markdown("<div class='category-header'>๐ Live Prompt Code</div>", unsafe_allow_html=True) | |
| st.code(f"```markdown\n{prompt}\n```", language="markdown") | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| st.markdown("<div class='category-header'>๐ Prompt History</div>", unsafe_allow_html=True) | |
| st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True) | |
| if st.session_state.prompt_history: | |
| for i, entry in enumerate(reversed(st.session_state.prompt_history)): | |
| st.markdown(f"**Prompt {len(st.session_state.prompt_history) - i} ({entry['timestamp']})**") | |
| st.code(f"```markdown\n{entry['prompt']}\n```", language="markdown") | |
| st.write(f"Saved as: `{entry['filename']}`") | |
| else: | |
| st.write("No prompts generated yet.") | |
| st.markdown("</div>", unsafe_allow_html=True) | |
| st.markdown("<div class='category-header'>๐ Examples</div>", unsafe_allow_html=True) | |
| st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True) | |
| st.markdown(""" | |
| <div style="background-color: #e6f3ff; border: 1px solid #b8daff; border-radius: 4px; padding: 6px; margin-bottom: 6px; font-size: 0.8em;"> | |
| <b>๐จโ๐ซ Teaching</b><pre style="white-space: pre-wrap; font-size: 0.8em; margin: 3px 0px;">Act as a ๐จโ๐ซ Teacher, use ๐ Informative tone, Create a ๐ Guide for ๐ฑ Beginners. | |
| It should be about Git. | |
| Include practical examples. | |
| Exclude advanced techniques. | |
| Return as ๐ Markdown.</pre> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| st.markdown(""" | |
| <div style="background-color: #e6ffed; border: 1px solid #b8e6cc; border-radius: 4px; padding: 6px; margin-bottom: 6px; font-size: 0.8em;"> | |
| <b>๐ผ Business</b><pre style="white-space: pre-wrap; font-size: 0.8em; margin: 3px 0px;">Act as a ๐ Professional, use ๐ค Persuasive tone, Write a ๐ง Email for ๐ผ Business Owners. | |
| It should be about a product launch. | |
| Include ROI metrics. | |
| Exclude technical details. | |
| Return as ๐ Plain Text.</pre> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| st.markdown(""" | |
| <div style="background-color: #f1f8ff; border-radius: 4px; padding: 6px; margin-top: 6px; font-size: 0.8em;"> | |
| <b>Prompt Structure:</b><br> | |
| Act as [<span style="color:blue">ROLE</span>], use [<span style="color:green">TONE</span>] tone, [<span style="color:red">INSTRUCTION</span>] a [<span style="color:purple">LENGTH</span>] [<span style="color:orange">CONTENT TYPE</span>] for [<span style="color:pink">AUDIENCE</span>].<br> | |
| It should be about [TOPIC].<br> | |
| Include [INCLUSION]. Exclude [EXCLUSION].<br> | |
| Return as [FORMAT]. | |
| </div> | |
| """, unsafe_allow_html=True) | |
| st.markdown("</div>", unsafe_allow_html=True) |