demetz commited on
Commit
90f08e2
·
verified ·
1 Parent(s): 8c25b40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -37
app.py CHANGED
@@ -167,10 +167,9 @@ def respond(message, history, name, char_class, char_alignment):
167
  {
168
  "role": "system",
169
  "content": (
170
- f"You are a chatbot that helps users create characters for role-playing games."
171
- f"The user might also provide the following information, please include their inputs in your response: \n"
172
- f"Character name: {name}, Character Class: {char_class}, Character Alignment: {char_alignment}."
173
- f"Use the following knowledge to inform your answers:\n\n{context}\n\n"
174
  "Keep your answers under 300 words."
175
  )
176
  }
@@ -194,20 +193,19 @@ def respond(message, history, name, char_class, char_alignment):
194
  response += token
195
  yield response
196
 
197
- # === Character alignment setup ===
198
  alignment_choices = [
199
  "Lawful Good", "Neutral Good", "Chaotic Good",
200
  "Lawful Neutral", "True Neutral", "Chaotic Neutral",
201
  "Lawful Evil", "Neutral Evil", "Chaotic Evil"
202
  ]
203
 
204
- # === CSS for buttons ===
205
  chat_css = """
206
  #alignment_container {
207
  display: grid;
208
  grid-template-columns: repeat(3, 1fr);
209
  width: 300px;
210
- gap: 0px;
211
  }
212
  .alignment-btn {
213
  aspect-ratio: 1 / 1;
@@ -217,7 +215,6 @@ chat_css = """
217
  margin: 0;
218
  padding: 0;
219
  cursor: pointer;
220
- transition: background-color 0.2s, color 0.2s;
221
  }
222
  .alignment-btn.selected {
223
  background-color: red !important;
@@ -226,16 +223,15 @@ chat_css = """
226
  }
227
  """
228
 
 
 
 
 
229
  # === GUI ===
230
  with gr.Blocks(css=chat_css) as chatbot:
231
- with gr.Row(scale=1):
232
  with gr.Column(scale=1):
233
- gr.Image(
234
- value="frog.png",
235
- show_label=False,
236
- show_share_button=False,
237
- show_download_button=False
238
- )
239
  with gr.Column(scale=1):
240
  character_name = gr.Textbox(label="Character Name", placeholder="Type your name here…", info="optional")
241
  character_class = gr.CheckboxGroup(
@@ -246,31 +242,13 @@ with gr.Blocks(css=chat_css) as chatbot:
246
  selected_alignment = gr.State("")
247
 
248
  with gr.Row(elem_id="alignment_container"):
249
- buttons = []
250
- for choice in alignment_choices:
251
- btn = gr.Button(choice, elem_classes=["alignment-btn"])
252
- buttons.append(btn)
253
 
254
- # Alignment button logic
255
  for btn, choice in zip(buttons, alignment_choices):
256
  btn.click(
257
- fn=lambda c=choice: c,
258
- inputs=None,
259
- outputs=selected_alignment
260
- ).then(
261
- fn=None,
262
- inputs=None,
263
- outputs=None,
264
- _js=f"""
265
- () => {{
266
- document.querySelectorAll('.alignment-btn').forEach(b => b.classList.remove('selected'));
267
- document.querySelectorAll('.alignment-btn').forEach(b => {{
268
- if (b.innerText === "{choice}") {{
269
- b.classList.add('selected');
270
- }}
271
- }});
272
- }}
273
- """
274
  )
275
 
276
  gr.ChatInterface(
 
167
  {
168
  "role": "system",
169
  "content": (
170
+ f"You are a chatbot that helps users create characters for role-playing games.\n"
171
+ f"The user might provide: Character name: {name}, Class: {char_class}, Alignment: {char_alignment}.\n"
172
+ f"Use the following knowledge:\n\n{context}\n\n"
 
173
  "Keep your answers under 300 words."
174
  )
175
  }
 
193
  response += token
194
  yield response
195
 
196
+ # === Alignment UI Setup ===
197
  alignment_choices = [
198
  "Lawful Good", "Neutral Good", "Chaotic Good",
199
  "Lawful Neutral", "True Neutral", "Chaotic Neutral",
200
  "Lawful Evil", "Neutral Evil", "Chaotic Evil"
201
  ]
202
 
 
203
  chat_css = """
204
  #alignment_container {
205
  display: grid;
206
  grid-template-columns: repeat(3, 1fr);
207
  width: 300px;
208
+ gap: 0;
209
  }
210
  .alignment-btn {
211
  aspect-ratio: 1 / 1;
 
215
  margin: 0;
216
  padding: 0;
217
  cursor: pointer;
 
218
  }
219
  .alignment-btn.selected {
220
  background-color: red !important;
 
223
  }
224
  """
225
 
226
+ # Function to update button classes
227
+ def update_alignment(choice):
228
+ return [gr.Button.update(variant="secondary", elem_classes=["alignment-btn", "selected"] if choice == c else ["alignment-btn"]) for c in alignment_choices], choice
229
+
230
  # === GUI ===
231
  with gr.Blocks(css=chat_css) as chatbot:
232
+ with gr.Row():
233
  with gr.Column(scale=1):
234
+ gr.Image("frog.png", show_label=False)
 
 
 
 
 
235
  with gr.Column(scale=1):
236
  character_name = gr.Textbox(label="Character Name", placeholder="Type your name here…", info="optional")
237
  character_class = gr.CheckboxGroup(
 
242
  selected_alignment = gr.State("")
243
 
244
  with gr.Row(elem_id="alignment_container"):
245
+ buttons = [gr.Button(value=c, elem_classes=["alignment-btn"]) for c in alignment_choices]
 
 
 
246
 
 
247
  for btn, choice in zip(buttons, alignment_choices):
248
  btn.click(
249
+ fn=update_alignment,
250
+ inputs=[gr.State(choice)],
251
+ outputs=[buttons, selected_alignment]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
  )
253
 
254
  gr.ChatInterface(