Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -134,84 +134,26 @@ data = {
|
|
| 134 |
]
|
| 135 |
}
|
| 136 |
|
| 137 |
-
# Function to
|
| 138 |
-
def
|
| 139 |
-
|
| 140 |
-
num_rows = (len(items) + cols - 1) // cols
|
| 141 |
-
|
| 142 |
-
# Create empty rows
|
| 143 |
-
rows = [["" for _ in range(cols)] for _ in range(num_rows)]
|
| 144 |
-
|
| 145 |
-
# Fill in the values
|
| 146 |
-
for i, item in enumerate(items):
|
| 147 |
-
row_idx = i // cols
|
| 148 |
-
col_idx = i % cols
|
| 149 |
-
rows[row_idx][col_idx] = f"{item['emoji']} {item['name']}"
|
| 150 |
-
|
| 151 |
-
return rows
|
| 152 |
|
| 153 |
-
# Function to
|
| 154 |
-
def handle_selection(category, value):
|
| 155 |
-
# Extract the name without emoji
|
| 156 |
-
parts = value.split(' ', 1)
|
| 157 |
-
if len(parts) < 2:
|
| 158 |
-
return
|
| 159 |
-
|
| 160 |
-
emoji = parts[0]
|
| 161 |
-
name = parts[1]
|
| 162 |
-
|
| 163 |
-
# Find the item in the data
|
| 164 |
-
for item in data[f"{category}s"]:
|
| 165 |
-
if item['name'] == name:
|
| 166 |
-
st.session_state.selections[category] = item
|
| 167 |
-
return
|
| 168 |
-
|
| 169 |
-
# If not found (shouldn't happen)
|
| 170 |
-
st.session_state.selections[category] = {"name": name, "emoji": emoji}
|
| 171 |
-
|
| 172 |
-
# Function to create dataframe with click handler
|
| 173 |
def create_dataframe_category(category, title, emoji_prefix):
|
| 174 |
-
# Create the dataframe content
|
| 175 |
-
df_rows = create_dataframe_rows(data[f"{category}s"], cols=4)
|
| 176 |
-
df = pd.DataFrame(df_rows)
|
| 177 |
-
|
| 178 |
-
# Display the category header
|
| 179 |
st.markdown(f"<div class='category-header'>{emoji_prefix} {title}</div>", unsafe_allow_html=True)
|
|
|
|
| 180 |
|
| 181 |
-
#
|
| 182 |
-
st.
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
width="small",
|
| 193 |
-
) for i in range(df.shape[1])
|
| 194 |
-
},
|
| 195 |
-
disabled=True,
|
| 196 |
-
use_container_width=True,
|
| 197 |
-
on_click=lambda e: handle_selection(category, e.current)
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
# Check if there's a selected value to highlight
|
| 201 |
-
if st.session_state.selections[category]:
|
| 202 |
-
selected_value = f"{st.session_state.selections[category]['emoji']} {st.session_state.selections[category]['name']}"
|
| 203 |
-
# Since we can't style specific cells in the dataframe editor, we can indicate the selection another way
|
| 204 |
-
selected_index = None
|
| 205 |
-
for i, row in df.iterrows():
|
| 206 |
-
for j, cell in enumerate(row):
|
| 207 |
-
if cell == selected_value:
|
| 208 |
-
selected_index = (i, j)
|
| 209 |
-
break
|
| 210 |
-
if selected_index:
|
| 211 |
-
break
|
| 212 |
-
|
| 213 |
-
if selected_index:
|
| 214 |
-
st.markdown(f"Selected: **{selected_value}**", unsafe_allow_html=True)
|
| 215 |
|
| 216 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 217 |
|
|
@@ -219,12 +161,10 @@ def create_dataframe_category(category, title, emoji_prefix):
|
|
| 219 |
def generate_prompt():
|
| 220 |
sel = st.session_state.selections
|
| 221 |
|
| 222 |
-
# Check if all required fields are selected
|
| 223 |
if not all([sel['role'], sel['tone'], sel['instruction'], sel['length'],
|
| 224 |
sel['content_type'], sel['audience'], sel['format'], sel['about']]):
|
| 225 |
return "Please select all required components and provide a topic."
|
| 226 |
|
| 227 |
-
# Generate the prompt
|
| 228 |
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']}.
|
| 229 |
|
| 230 |
It should be about {sel['about']}."""
|
|
@@ -249,7 +189,6 @@ st.markdown("<h2 style='text-align: center; font-size: 1.3rem; margin-bottom: 1r
|
|
| 249 |
col1, col2 = st.columns([3, 1])
|
| 250 |
|
| 251 |
with col1:
|
| 252 |
-
# Create dataframes for each category
|
| 253 |
create_dataframe_category("role", "Choose a Role", "๐ค")
|
| 254 |
create_dataframe_category("tone", "Select a Tone", "๐ญ")
|
| 255 |
create_dataframe_category("instruction", "Select an Instruction", "๐")
|
|
@@ -258,7 +197,6 @@ with col1:
|
|
| 258 |
create_dataframe_category("audience", "Select Target Audience", "๐ฅ")
|
| 259 |
create_dataframe_category("format", "Select Format", "๐")
|
| 260 |
|
| 261 |
-
# Text input fields
|
| 262 |
st.markdown("<div class='category-header'>๐ Additional Details</div>", unsafe_allow_html=True)
|
| 263 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 264 |
|
|
@@ -285,17 +223,14 @@ with col1:
|
|
| 285 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 286 |
|
| 287 |
with col2:
|
| 288 |
-
# Generate the prompt
|
| 289 |
prompt = generate_prompt()
|
| 290 |
|
| 291 |
-
# Display the prompt
|
| 292 |
st.markdown("<div class='category-header'>๐ฎ Generated Prompt</div>", unsafe_allow_html=True)
|
| 293 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 294 |
st.markdown("<div class='prompt-display'>", unsafe_allow_html=True)
|
| 295 |
st.write(prompt)
|
| 296 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 297 |
|
| 298 |
-
# Action buttons
|
| 299 |
btn1, btn2, btn3 = st.columns(3)
|
| 300 |
with btn1:
|
| 301 |
if st.button("๐ Copy", type="primary", use_container_width=True):
|
|
@@ -308,21 +243,19 @@ with col2:
|
|
| 308 |
st.session_state.selections[key] = ""
|
| 309 |
else:
|
| 310 |
st.session_state.selections[key] = None
|
| 311 |
-
st.experimental_rerun()
|
| 312 |
|
| 313 |
with btn3:
|
| 314 |
if st.button("๐ฒ Random", type="secondary", use_container_width=True):
|
| 315 |
for category in ['role', 'tone', 'instruction', 'length', 'content_type', 'audience', 'format']:
|
| 316 |
st.session_state.selections[category] = random.choice(data[category+'s'])
|
| 317 |
-
st.
|
| 318 |
|
| 319 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 320 |
|
| 321 |
-
# Sample prompts section
|
| 322 |
st.markdown("<div class='category-header'>๐ Examples</div>", unsafe_allow_html=True)
|
| 323 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 324 |
|
| 325 |
-
# Example 1
|
| 326 |
st.markdown("""
|
| 327 |
<div style="background-color: #e6f3ff; border: 1px solid #b8daff; border-radius: 4px; padding: 6px; margin-bottom: 6px; font-size: 0.8em;">
|
| 328 |
<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.
|
|
@@ -335,7 +268,6 @@ Return as ๐ Markdown.</pre>
|
|
| 335 |
</div>
|
| 336 |
""", unsafe_allow_html=True)
|
| 337 |
|
| 338 |
-
# Example 2
|
| 339 |
st.markdown("""
|
| 340 |
<div style="background-color: #e6ffed; border: 1px solid #b8e6cc; border-radius: 4px; padding: 6px; margin-bottom: 6px; font-size: 0.8em;">
|
| 341 |
<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 ๐ฉโ๐ผ Executives.
|
|
@@ -348,7 +280,6 @@ Return as ๐ Plain Text.</pre>
|
|
| 348 |
</div>
|
| 349 |
""", unsafe_allow_html=True)
|
| 350 |
|
| 351 |
-
# Prompt structure guide
|
| 352 |
st.markdown("""
|
| 353 |
<div style="background-color: #f1f8ff; border-radius: 4px; padding: 6px; margin-top: 6px; font-size: 0.8em;">
|
| 354 |
<b>Prompt Structure:</b><br>
|
|
|
|
| 134 |
]
|
| 135 |
}
|
| 136 |
|
| 137 |
+
# Function to handle selection
|
| 138 |
+
def handle_selection(category, item):
|
| 139 |
+
st.session_state.selections[category] = item
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
# Function to create category selection with buttons
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
def create_dataframe_category(category, title, emoji_prefix):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
st.markdown(f"<div class='category-header'>{emoji_prefix} {title}</div>", unsafe_allow_html=True)
|
| 144 |
+
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 145 |
|
| 146 |
+
# Create 4 columns for layout
|
| 147 |
+
cols = st.columns(4)
|
| 148 |
+
for i, item in enumerate(data[f"{category}s"]):
|
| 149 |
+
col = cols[i % 4]
|
| 150 |
+
label = f"{item['emoji']} {item['name']}"
|
| 151 |
+
is_selected = st.session_state.selections[category] == item
|
| 152 |
+
with col:
|
| 153 |
+
if st.button(label, key=f"{category}_{i}",
|
| 154 |
+
type="primary" if is_selected else "secondary",
|
| 155 |
+
use_container_width=True):
|
| 156 |
+
handle_selection(category, item)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 159 |
|
|
|
|
| 161 |
def generate_prompt():
|
| 162 |
sel = st.session_state.selections
|
| 163 |
|
|
|
|
| 164 |
if not all([sel['role'], sel['tone'], sel['instruction'], sel['length'],
|
| 165 |
sel['content_type'], sel['audience'], sel['format'], sel['about']]):
|
| 166 |
return "Please select all required components and provide a topic."
|
| 167 |
|
|
|
|
| 168 |
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']}.
|
| 169 |
|
| 170 |
It should be about {sel['about']}."""
|
|
|
|
| 189 |
col1, col2 = st.columns([3, 1])
|
| 190 |
|
| 191 |
with col1:
|
|
|
|
| 192 |
create_dataframe_category("role", "Choose a Role", "๐ค")
|
| 193 |
create_dataframe_category("tone", "Select a Tone", "๐ญ")
|
| 194 |
create_dataframe_category("instruction", "Select an Instruction", "๐")
|
|
|
|
| 197 |
create_dataframe_category("audience", "Select Target Audience", "๐ฅ")
|
| 198 |
create_dataframe_category("format", "Select Format", "๐")
|
| 199 |
|
|
|
|
| 200 |
st.markdown("<div class='category-header'>๐ Additional Details</div>", unsafe_allow_html=True)
|
| 201 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 202 |
|
|
|
|
| 223 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 224 |
|
| 225 |
with col2:
|
|
|
|
| 226 |
prompt = generate_prompt()
|
| 227 |
|
|
|
|
| 228 |
st.markdown("<div class='category-header'>๐ฎ Generated Prompt</div>", unsafe_allow_html=True)
|
| 229 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 230 |
st.markdown("<div class='prompt-display'>", unsafe_allow_html=True)
|
| 231 |
st.write(prompt)
|
| 232 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 233 |
|
|
|
|
| 234 |
btn1, btn2, btn3 = st.columns(3)
|
| 235 |
with btn1:
|
| 236 |
if st.button("๐ Copy", type="primary", use_container_width=True):
|
|
|
|
| 243 |
st.session_state.selections[key] = ""
|
| 244 |
else:
|
| 245 |
st.session_state.selections[key] = None
|
| 246 |
+
st.rerun() # Updated from st.experimental_rerun() to st.rerun() for newer Streamlit versions
|
| 247 |
|
| 248 |
with btn3:
|
| 249 |
if st.button("๐ฒ Random", type="secondary", use_container_width=True):
|
| 250 |
for category in ['role', 'tone', 'instruction', 'length', 'content_type', 'audience', 'format']:
|
| 251 |
st.session_state.selections[category] = random.choice(data[category+'s'])
|
| 252 |
+
st.rerun()
|
| 253 |
|
| 254 |
st.markdown("</div>", unsafe_allow_html=True)
|
| 255 |
|
|
|
|
| 256 |
st.markdown("<div class='category-header'>๐ Examples</div>", unsafe_allow_html=True)
|
| 257 |
st.markdown("<div class='dataframe-container'>", unsafe_allow_html=True)
|
| 258 |
|
|
|
|
| 259 |
st.markdown("""
|
| 260 |
<div style="background-color: #e6f3ff; border: 1px solid #b8daff; border-radius: 4px; padding: 6px; margin-bottom: 6px; font-size: 0.8em;">
|
| 261 |
<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.
|
|
|
|
| 268 |
</div>
|
| 269 |
""", unsafe_allow_html=True)
|
| 270 |
|
|
|
|
| 271 |
st.markdown("""
|
| 272 |
<div style="background-color: #e6ffed; border: 1px solid #b8e6cc; border-radius: 4px; padding: 6px; margin-bottom: 6px; font-size: 0.8em;">
|
| 273 |
<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 ๐ฉโ๐ผ Executives.
|
|
|
|
| 280 |
</div>
|
| 281 |
""", unsafe_allow_html=True)
|
| 282 |
|
|
|
|
| 283 |
st.markdown("""
|
| 284 |
<div style="background-color: #f1f8ff; border-radius: 4px; padding: 6px; margin-top: 6px; font-size: 0.8em;">
|
| 285 |
<b>Prompt Structure:</b><br>
|