gradio/demo/blocks_flashcards/run.py
aliabid94 da05e59a53
Removing .update and get_config, attempt 2 (#5240)
* changes

* add changeset

* changes

* changes

* Update many-tips-create.md

* notebooks

* chanegs

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changew

* changes

* changes

* changes

* changes

* chanegs

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* changes

* Revert "changes"

This reverts commit 0868c25c56.

* changes

* changes

* changes

* changes

* changes

* changes

* change

* changes

* chaneges

* changes

* chagnes

* changes

* changes

* changes

* changes

* changes

* fix typing

* changes

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
2023-09-18 21:17:06 -07:00

90 lines
2.6 KiB
Python

import random
import gradio as gr
demo = gr.Blocks()
with demo:
gr.Markdown(
"Load the flashcards in the table below, then use the Practice tab to practice."
)
with gr.Tab("Word Bank"):
flashcards_table = gr.Dataframe(headers=["front", "back"], type="array")
with gr.Tab("Practice"):
with gr.Row():
with gr.Column():
front = gr.Textbox(label="Prompt")
with gr.Row():
new_btn = gr.Button("New Card")
flip_btn = gr.Button("Flip Card")
with gr.Column(visible=False) as answer_col:
back = gr.Textbox(label="Answer")
selected_card = gr.State()
with gr.Row():
correct_btn = gr.Button("Correct")
incorrect_btn = gr.Button("Incorrect")
with gr.Tab("Results"):
results = gr.State(value={})
correct_field = gr.Markdown("# Correct: 0")
incorrect_field = gr.Markdown("# Incorrect: 0")
gr.Markdown("Card Statistics: ")
results_table = gr.Dataframe(headers=["Card", "Correct", "Incorrect"])
def load_new_card(flashcards):
card = random.choice(flashcards)
return (
card,
card[0],
gr.Column(visible=False),
)
new_btn.click(
load_new_card,
[flashcards_table],
[selected_card, front, answer_col],
)
def flip_card(card):
return card[1], gr.Column(visible=True)
flip_btn.click(flip_card, [selected_card], [back, answer_col])
def mark_correct(card, results):
if card[0] not in results:
results[card[0]] = [0, 0]
results[card[0]][0] += 1
correct_count = sum(result[0] for result in results.values())
return (
results,
f"# Correct: {correct_count}",
[[front, scores[0], scores[1]] for front, scores in results.items()],
)
def mark_incorrect(card, results):
if card[0] not in results:
results[card[0]] = [0, 0]
results[card[0]][1] += 1
incorrect_count = sum(result[1] for result in results.values())
return (
results,
f"# Inorrect: {incorrect_count}",
[[front, scores[0], scores[1]] for front, scores in results.items()],
)
correct_btn.click(
mark_correct,
[selected_card, results],
[results, correct_field, results_table],
)
incorrect_btn.click(
mark_incorrect,
[selected_card, results],
[results, incorrect_field, results_table],
)
if __name__ == "__main__":
demo.launch()