Spaces:
Sleeping
Sleeping
Mel Seto
commited on
Commit
·
a1e849a
1
Parent(s):
e05cd17
use checkbox to switch between traditional and simplified
Browse files- src/app.py +14 -8
src/app.py
CHANGED
|
@@ -46,14 +46,13 @@ def format_explanation(pinyin_text: str, translation: str, meaning: str) -> str:
|
|
| 46 |
# Mock function for UI testing
|
| 47 |
# ======================
|
| 48 |
|
| 49 |
-
def find_idiom_mock():
|
| 50 |
-
idiom = "对症下药"
|
| 51 |
-
trad_idiom = char_converter.convert(idiom)
|
| 52 |
pinyin_text = "duì zhèng xià yào"
|
| 53 |
translation = "To prescribe the right medicine; to take the right approach to a problem."
|
| 54 |
meaning = "add a meaning for the mock"
|
| 55 |
explanation = format_explanation(pinyin_text, translation, meaning)
|
| 56 |
-
idiom_output = f"{idiom}<br>
|
| 57 |
return idiom_output, explanation
|
| 58 |
|
| 59 |
|
|
@@ -127,14 +126,16 @@ Answer:"""
|
|
| 127 |
# ======================
|
| 128 |
# UI Wrapper
|
| 129 |
# ======================
|
| 130 |
-
def update_ui(situation):
|
| 131 |
if USE_MOCK:
|
| 132 |
-
idiom, explanation = find_idiom_mock()
|
| 133 |
else:
|
| 134 |
idiom, explanation = find_idiom(situation)
|
| 135 |
|
|
|
|
|
|
|
| 136 |
return (
|
| 137 |
-
f"<div class='idiom-output'>{
|
| 138 |
f"<div class='explanation-output' style='margin-top: 1px;'>{explanation}</div>",
|
| 139 |
)
|
| 140 |
|
|
@@ -168,13 +169,18 @@ def launch_app():
|
|
| 168 |
)
|
| 169 |
|
| 170 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
idiom_output = gr.HTML(label="Idiom")
|
| 172 |
explanation_output = gr.HTML(label="Explanation")
|
| 173 |
|
| 174 |
# pylint: disable=no-member
|
| 175 |
generate_btn.click(
|
| 176 |
fn=update_ui,
|
| 177 |
-
inputs=[situation],
|
| 178 |
outputs=[idiom_output, explanation_output],
|
| 179 |
)
|
| 180 |
|
|
|
|
| 46 |
# Mock function for UI testing
|
| 47 |
# ======================
|
| 48 |
|
| 49 |
+
def find_idiom_mock(traditional: bool):
|
| 50 |
+
idiom = "對症下藥" if traditional else "对症下药"
|
|
|
|
| 51 |
pinyin_text = "duì zhèng xià yào"
|
| 52 |
translation = "To prescribe the right medicine; to take the right approach to a problem."
|
| 53 |
meaning = "add a meaning for the mock"
|
| 54 |
explanation = format_explanation(pinyin_text, translation, meaning)
|
| 55 |
+
idiom_output = f"{idiom}<br>"
|
| 56 |
return idiom_output, explanation
|
| 57 |
|
| 58 |
|
|
|
|
| 126 |
# ======================
|
| 127 |
# UI Wrapper
|
| 128 |
# ======================
|
| 129 |
+
def update_ui(situation, traditional: bool):
|
| 130 |
if USE_MOCK:
|
| 131 |
+
idiom, explanation = find_idiom_mock(traditional)
|
| 132 |
else:
|
| 133 |
idiom, explanation = find_idiom(situation)
|
| 134 |
|
| 135 |
+
idiom_output = char_converter.convert(idiom.split("<br>")[0]) if traditional else idiom
|
| 136 |
+
|
| 137 |
return (
|
| 138 |
+
f"<div class='idiom-output'>{idiom_output}</div>",
|
| 139 |
f"<div class='explanation-output' style='margin-top: 1px;'>{explanation}</div>",
|
| 140 |
)
|
| 141 |
|
|
|
|
| 169 |
)
|
| 170 |
|
| 171 |
with gr.Column():
|
| 172 |
+
char_mode = gr.Checkbox(
|
| 173 |
+
label="Traditional Characters",
|
| 174 |
+
value=False, # False = Simplified, True = Traditional
|
| 175 |
+
interactive=True
|
| 176 |
+
)
|
| 177 |
idiom_output = gr.HTML(label="Idiom")
|
| 178 |
explanation_output = gr.HTML(label="Explanation")
|
| 179 |
|
| 180 |
# pylint: disable=no-member
|
| 181 |
generate_btn.click(
|
| 182 |
fn=update_ui,
|
| 183 |
+
inputs=[situation, char_mode],
|
| 184 |
outputs=[idiom_output, explanation_output],
|
| 185 |
)
|
| 186 |
|