File size: 3,880 Bytes
ac1b60e 4d1d1c2 ac1b60e 77d4990 ac1b60e 4d1d1c2 3f83d10 5d37f70 3f83d10 ac1b60e 3f83d10 5d37f70 ac1b60e 3f83d10 5d37f70 3f83d10 5d37f70 8bdd76e 5d37f70 8bdd76e 5d37f70 4d1d1c2 3f83d10 4d1d1c2 3f83d10 ac1b60e 5d37f70 ac1b60e 3f83d10 5d37f70 8bdd76e 5d37f70 8bdd76e 5d37f70 8bdd76e 5d37f70 3f83d10 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
import gradio as gr
import random
# λ¨μ΄ 리μ€νΈ
words = {
"μμλ€": "was",
"λ§νλ€": "said",
"μ§μΌλ΄€λ€": "watched",
"μκ°μ λ°μλ€": "inspired",
"νννλ€": "exploring",
"μ°μ°ν λ°κ²¬νλ€": "stumbled",
"κ²°μ νλ€": "decided",
"μ€λΉνλ€": "prepared",
"μλ΄νλ€": "guided",
"λμ°©νλ€": "reached",
"λ°κ²¬νλ€": "discovered",
"μμ§μλ€": "moved",
"μ΄μλ€": "opened",
"μ°Ύμλ€": "found",
"λμλ€": "became"
}
korean_words = list(words.keys())
english_words = list(words.values())
random.shuffle(korean_words)
random.shuffle(english_words)
state = {
"selected_korean": None,
"selected_english": None,
"matched_pairs": []
}
def select_word(word, lang):
if lang == "korean":
state["selected_korean"] = word
else:
state["selected_english"] = word
result = "Select the matching word."
matched = False
if state["selected_korean"] and state["selected_english"]:
if words[state["selected_korean"]] == state["selected_english"]:
state["matched_pairs"].append((state["selected_korean"], state["selected_english"]))
result = f"Matched! {state['matched_pairs']}"
matched = True
else:
result = "Not Matched! Try again."
state["selected_korean"] = None
state["selected_english"] = None
return result, state["matched_pairs"], matched
def update_buttons(matched_pairs):
korean_buttons = []
english_buttons = []
for k in korean_words:
btn = gr.Button(k, elem_id=f"k_{k}")
if any(pair[0] == k for pair in matched_pairs):
btn.style = {"background-color": "lightgreen"}
korean_buttons.append(btn)
for e in english_words:
btn = gr.Button(e, elem_id=f"e_{e}")
if any(pair[1] == e for pair in matched_pairs):
btn.style = {"background-color": "lightgreen"}
english_buttons.append(btn)
return korean_buttons, english_buttons
css = """
.button-green {
background-color: lightgreen !important;
}
.button-red {
background-color: lightcoral !important;
}
"""
with gr.Blocks(css=css) as demo:
gr.Markdown("### νκΈ λ¨μ΄μ μμ΄ λ¨μ΄λ₯Ό λ§€μΉνμΈμ")
with gr.Row() as row:
korean_buttons, english_buttons = update_buttons(state["matched_pairs"])
korean_col = gr.Column(korean_buttons)
english_col = gr.Column(english_buttons)
output = gr.Textbox(label="κ²°κ³Ό", elem_id="result")
def on_click(word, lang):
result, matched_pairs, matched = select_word(word, lang)
updated_korean_buttons, updated_english_buttons = update_buttons(matched_pairs)
if matched:
for btn in updated_korean_buttons:
if btn.value == word:
btn.style = {"background-color": "lightgreen"}
for btn in updated_english_buttons:
if btn.value == word:
btn.style = {"background-color": "lightgreen"}
else:
if lang == "korean":
for btn in updated_korean_buttons:
if btn.value == word:
btn.style = {"background-color": "lightcoral"}
else:
for btn in updated_english_buttons:
if btn.value == word:
btn.style = {"background-color": "lightcoral"}
with row:
gr.update(korean_col, children=updated_korean_buttons)
gr.update(english_col, children=updated_english_buttons)
return result
for btn in korean_buttons:
btn.click(on_click, inputs=[btn, gr.State("korean")], outputs=output)
for btn in english_buttons:
btn.click(on_click, inputs=[btn, gr.State("english")], outputs=output)
demo.launch()
|