Spaces:
Sleeping
Sleeping
Jonathan Bejarano
commited on
Commit
·
d310432
1
Parent(s):
75dae40
fix conditional dropdown logic
Browse files
app.py
CHANGED
|
@@ -167,43 +167,54 @@ with gr.Blocks() as demo:
|
|
| 167 |
gr.Markdown("# Geography AI Tutor")
|
| 168 |
gr.Markdown(description)
|
| 169 |
|
| 170 |
-
# Game
|
| 171 |
-
game.mode_dropdown = gr.Dropdown(
|
| 172 |
-
choices=[game.MODE_STATES, game.MODE_COUNTRIES],
|
| 173 |
-
value=game.MODE_STATES,
|
| 174 |
-
label="Game Mode",
|
| 175 |
-
info="Choose what type of location to guess"
|
| 176 |
-
)
|
| 177 |
-
|
| 178 |
game.type_dropdown = gr.Dropdown(
|
| 179 |
choices=[game.TYPE_GEOGRAPHY_BEE, game.TYPE_TWENTY_QUESTIONS],
|
| 180 |
value=game.TYPE_GEOGRAPHY_BEE,
|
| 181 |
label="Game Type",
|
| 182 |
info="Choose what type of game to play"
|
| 183 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
# ChatInterface without built-in examples
|
| 186 |
chatbot = gr.ChatInterface(
|
| 187 |
custom_respond,
|
| 188 |
type="messages",
|
| 189 |
cache_examples=False,
|
| 190 |
-
additional_inputs=[game.
|
| 191 |
)
|
| 192 |
|
| 193 |
# Add examples separately using Dataset which can be updated
|
| 194 |
with gr.Row():
|
| 195 |
-
gr.Markdown("### Example Questions (click to use)")
|
| 196 |
|
| 197 |
examples_dataset = gr.Dataset(
|
| 198 |
components=[chatbot.textbox],
|
| 199 |
samples=examples_states,
|
| 200 |
-
type="index"
|
|
|
|
| 201 |
)
|
| 202 |
|
| 203 |
# Update examples when mode changes
|
| 204 |
def update_examples(mode):
|
| 205 |
return gr.Dataset(samples=get_examples_for_mode(mode))
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
# When an example is clicked, populate the textbox
|
| 208 |
def load_example(index, mode):
|
| 209 |
examples = get_examples_for_mode(mode)
|
|
@@ -217,6 +228,12 @@ with gr.Blocks() as demo:
|
|
| 217 |
outputs=[examples_dataset]
|
| 218 |
)
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
examples_dataset.select(
|
| 221 |
fn=load_example,
|
| 222 |
inputs=[examples_dataset, game.mode_dropdown],
|
|
|
|
| 167 |
gr.Markdown("# Geography AI Tutor")
|
| 168 |
gr.Markdown(description)
|
| 169 |
|
| 170 |
+
# Game type selection at the top
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
game.type_dropdown = gr.Dropdown(
|
| 172 |
choices=[game.TYPE_GEOGRAPHY_BEE, game.TYPE_TWENTY_QUESTIONS],
|
| 173 |
value=game.TYPE_GEOGRAPHY_BEE,
|
| 174 |
label="Game Type",
|
| 175 |
info="Choose what type of game to play"
|
| 176 |
)
|
| 177 |
+
|
| 178 |
+
game.mode_dropdown = gr.Dropdown(
|
| 179 |
+
choices=[game.MODE_STATES, game.MODE_COUNTRIES],
|
| 180 |
+
value=game.MODE_STATES,
|
| 181 |
+
label="Game Mode",
|
| 182 |
+
info="Choose what type of location to guess",
|
| 183 |
+
visible=False
|
| 184 |
+
)
|
| 185 |
|
| 186 |
# ChatInterface without built-in examples
|
| 187 |
chatbot = gr.ChatInterface(
|
| 188 |
custom_respond,
|
| 189 |
type="messages",
|
| 190 |
cache_examples=False,
|
| 191 |
+
additional_inputs=[game.type_dropdown, game.mode_dropdown],
|
| 192 |
)
|
| 193 |
|
| 194 |
# Add examples separately using Dataset which can be updated
|
| 195 |
with gr.Row():
|
| 196 |
+
examples_header = gr.Markdown("### Example Questions (click to use)", visible=False)
|
| 197 |
|
| 198 |
examples_dataset = gr.Dataset(
|
| 199 |
components=[chatbot.textbox],
|
| 200 |
samples=examples_states,
|
| 201 |
+
type="index",
|
| 202 |
+
visible=False
|
| 203 |
)
|
| 204 |
|
| 205 |
# Update examples when mode changes
|
| 206 |
def update_examples(mode):
|
| 207 |
return gr.Dataset(samples=get_examples_for_mode(mode))
|
| 208 |
|
| 209 |
+
# Update visibility when game type changes
|
| 210 |
+
def update_examples_visibility(game_type):
|
| 211 |
+
is_visible = (game_type == game.TYPE_TWENTY_QUESTIONS)
|
| 212 |
+
return (
|
| 213 |
+
gr.Dropdown(visible=is_visible), # mode_dropdown
|
| 214 |
+
gr.Markdown(visible=is_visible), # examples_header
|
| 215 |
+
gr.Dataset(visible=is_visible) # examples_dataset
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
# When an example is clicked, populate the textbox
|
| 219 |
def load_example(index, mode):
|
| 220 |
examples = get_examples_for_mode(mode)
|
|
|
|
| 228 |
outputs=[examples_dataset]
|
| 229 |
)
|
| 230 |
|
| 231 |
+
game.type_dropdown.change(
|
| 232 |
+
fn=update_examples_visibility,
|
| 233 |
+
inputs=[game.type_dropdown],
|
| 234 |
+
outputs=[game.mode_dropdown, examples_header, examples_dataset]
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
examples_dataset.select(
|
| 238 |
fn=load_example,
|
| 239 |
inputs=[examples_dataset, game.mode_dropdown],
|