|
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() |
|
|