add auto prediciton
Browse files
app.py
CHANGED
|
@@ -61,6 +61,14 @@ def append_suggestion(current, choice):
|
|
| 61 |
# 模擬輸入法候選選中
|
| 62 |
return current + choice
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# 自訂 CSS:模擬經典中文輸入法候選欄樣式
|
| 65 |
custom_css = """
|
| 66 |
#suggestions-bar .candidate-list {
|
|
@@ -91,7 +99,8 @@ custom_css = """
|
|
| 91 |
with gr.Blocks(css=custom_css) as demo:
|
| 92 |
# 標題和說明
|
| 93 |
gr.Markdown(
|
| 94 |
-
"## 🇹🇼 繁體中文輸入法加速器
|
|
|
|
| 95 |
"結合小型語言模型與 ZeroGPU,即時 IME 風格候選條。"
|
| 96 |
)
|
| 97 |
|
|
@@ -101,12 +110,13 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 101 |
elem_id="suggestions-bar", elem_classes="candidate-list"
|
| 102 |
)
|
| 103 |
|
| 104 |
-
# 輸入區與按鈕:單行輸入框 + 小按鈕
|
| 105 |
with gr.Row():
|
| 106 |
input_text = gr.Textbox(
|
| 107 |
label="", placeholder="請輸入拼音或文字…", lines=1, max_lines=1
|
| 108 |
)
|
| 109 |
gpu_button = gr.Button("建議")
|
|
|
|
| 110 |
|
| 111 |
# 進階參數設定(可折疊)
|
| 112 |
with gr.Accordion("進階設定", open=False):
|
|
@@ -126,10 +136,15 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 126 |
inputs=[input_text, model_selector, k_slider, m_slider],
|
| 127 |
outputs=suggestions,
|
| 128 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
suggestions.change(
|
| 130 |
fn=append_suggestion,
|
| 131 |
inputs=[input_text, suggestions],
|
| 132 |
outputs=input_text,
|
| 133 |
)
|
| 134 |
|
| 135 |
-
demo.launch()
|
|
|
|
| 61 |
# 模擬輸入法候選選中
|
| 62 |
return current + choice
|
| 63 |
|
| 64 |
+
def suggest_on_change(text, model_name, k, m, auto_flag):
|
| 65 |
+
"""
|
| 66 |
+
當輸入框內容改變時,如果自動預測開啟,則觸發建議生成;否則清空候選。
|
| 67 |
+
"""
|
| 68 |
+
if auto_flag:
|
| 69 |
+
return suggest_next(text, model_name, k, m)
|
| 70 |
+
return update(choices=[], value=None)
|
| 71 |
+
|
| 72 |
# 自訂 CSS:模擬經典中文輸入法候選欄樣式
|
| 73 |
custom_css = """
|
| 74 |
#suggestions-bar .candidate-list {
|
|
|
|
| 99 |
with gr.Blocks(css=custom_css) as demo:
|
| 100 |
# 標題和說明
|
| 101 |
gr.Markdown(
|
| 102 |
+
"## 🇹🇼 繁體中文輸入法加速器
|
| 103 |
+
"
|
| 104 |
"結合小型語言模型與 ZeroGPU,即時 IME 風格候選條。"
|
| 105 |
)
|
| 106 |
|
|
|
|
| 110 |
elem_id="suggestions-bar", elem_classes="candidate-list"
|
| 111 |
)
|
| 112 |
|
| 113 |
+
# 輸入區與按鈕:單行輸入框 + 小按鈕 + 自動預測開關
|
| 114 |
with gr.Row():
|
| 115 |
input_text = gr.Textbox(
|
| 116 |
label="", placeholder="請輸入拼音或文字…", lines=1, max_lines=1
|
| 117 |
)
|
| 118 |
gpu_button = gr.Button("建議")
|
| 119 |
+
auto_predict = gr.Checkbox(value=True, label="自動預測 (輸入框內容變更時觸發)")
|
| 120 |
|
| 121 |
# 進階參數設定(可折疊)
|
| 122 |
with gr.Accordion("進階設定", open=False):
|
|
|
|
| 136 |
inputs=[input_text, model_selector, k_slider, m_slider],
|
| 137 |
outputs=suggestions,
|
| 138 |
)
|
| 139 |
+
input_text.change(
|
| 140 |
+
fn=suggest_on_change,
|
| 141 |
+
inputs=[input_text, model_selector, k_slider, m_slider, auto_predict],
|
| 142 |
+
outputs=suggestions,
|
| 143 |
+
)
|
| 144 |
suggestions.change(
|
| 145 |
fn=append_suggestion,
|
| 146 |
inputs=[input_text, suggestions],
|
| 147 |
outputs=input_text,
|
| 148 |
)
|
| 149 |
|
| 150 |
+
demo.launch()
|