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