awacke1 commited on
Commit
3b411be
ยท
verified ยท
1 Parent(s): 8bc23f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -87
app.py CHANGED
@@ -134,84 +134,26 @@ data = {
134
  ]
135
  }
136
 
137
- # Function to convert data to dataframe rows
138
- def create_dataframe_rows(items, cols=4):
139
- # Calculate how many rows we need based on items and columns
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 handle cell click events
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
- # Display in a container
182
- st.markdown(f"<div class='dataframe-container'>", unsafe_allow_html=True)
183
-
184
- # Use an edited dataframe
185
- edited_df = st.data_editor(
186
- df,
187
- key=f"df_{category}",
188
- hide_index=True,
189
- column_config={
190
- i: st.column_config.Column(
191
- label=f"Option {i+1}",
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.experimental_rerun()
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>