layout improvement
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
from gradio import update
|
|
@@ -29,7 +28,7 @@ def get_pipeline(model_name):
|
|
| 29 |
@spaces.GPU
|
| 30 |
def suggest_next(text, model_name, k, m):
|
| 31 |
"""
|
| 32 |
-
使用 Beam Search 產生 M
|
| 33 |
"""
|
| 34 |
gen_pipe = get_pipeline(model_name)
|
| 35 |
outs = gen_pipe(
|
|
@@ -43,45 +42,72 @@ def suggest_next(text, model_name, k, m):
|
|
| 43 |
suggestions = [out["generated_text"][len(text):].strip() for out in outs]
|
| 44 |
suggestions = [s for s in suggestions if s]
|
| 45 |
|
| 46 |
-
#
|
| 47 |
return update(choices=suggestions, value=None)
|
| 48 |
|
| 49 |
def append_suggestion(current, choice):
|
| 50 |
if choice is None:
|
| 51 |
return current
|
|
|
|
| 52 |
return current + choice
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
gr.Markdown(
|
| 56 |
"## 🇹🇼 台灣中文輸入法加速器 \n"
|
| 57 |
-
"結合小型語言模型與 ZeroGPU,即時 IME
|
| 58 |
)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
suggestions = gr.Radio(
|
| 62 |
-
[], label="
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
with gr.Row():
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
)
|
| 72 |
-
with gr.Column(scale=1, min_width=80):
|
| 73 |
-
gpu_button = gr.Button("使用 GPU 生成建議")
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
with gr.
|
| 77 |
model_selector = gr.Dropdown(
|
| 78 |
-
MODEL_LIST, value=MODEL_LIST[0], label="
|
| 79 |
)
|
| 80 |
k_slider = gr.Slider(
|
| 81 |
-
minimum=1, maximum=50, step=1, value=1, label="K
|
| 82 |
)
|
| 83 |
m_slider = gr.Slider(
|
| 84 |
-
minimum=1, maximum=30, step=1, value=10, label="M
|
| 85 |
)
|
| 86 |
|
| 87 |
# 事件綁定
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
from gradio import update
|
|
|
|
| 28 |
@spaces.GPU
|
| 29 |
def suggest_next(text, model_name, k, m):
|
| 30 |
"""
|
| 31 |
+
使用 Beam Search 產生 M 條最可能的下段建議,並一次更新候選列表。
|
| 32 |
"""
|
| 33 |
gen_pipe = get_pipeline(model_name)
|
| 34 |
outs = gen_pipe(
|
|
|
|
| 42 |
suggestions = [out["generated_text"][len(text):].strip() for out in outs]
|
| 43 |
suggestions = [s for s in suggestions if s]
|
| 44 |
|
| 45 |
+
# 更新候選條
|
| 46 |
return update(choices=suggestions, value=None)
|
| 47 |
|
| 48 |
def append_suggestion(current, choice):
|
| 49 |
if choice is None:
|
| 50 |
return current
|
| 51 |
+
# 模擬輸入法候選選中
|
| 52 |
return current + choice
|
| 53 |
|
| 54 |
+
# 自定義 CSS:模擬經典中文輸入法候選欄樣式
|
| 55 |
+
custom_css = """
|
| 56 |
+
#suggestions-bar .candidate-list {
|
| 57 |
+
display: flex;
|
| 58 |
+
gap: 12px;
|
| 59 |
+
background: #ffffff;
|
| 60 |
+
border: 1px solid #ccc;
|
| 61 |
+
border-radius: 4px;
|
| 62 |
+
padding: 6px;
|
| 63 |
+
}
|
| 64 |
+
#suggestions-bar .candidate-list input[type=radio] {
|
| 65 |
+
display: none;
|
| 66 |
+
}
|
| 67 |
+
#suggestions-bar .candidate-list label {
|
| 68 |
+
cursor: pointer;
|
| 69 |
+
padding: 2px 6px;
|
| 70 |
+
border-radius: 4px;
|
| 71 |
+
}
|
| 72 |
+
#suggestions-bar .candidate-list label:hover {
|
| 73 |
+
background: #f0f0f0;
|
| 74 |
+
}
|
| 75 |
+
#suggestions-bar .candidate-list input[type=radio]:checked + label {
|
| 76 |
+
background: #e0e0e0;
|
| 77 |
+
border: 1px solid #888;
|
| 78 |
+
}
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 82 |
+
# 標題和說明
|
| 83 |
gr.Markdown(
|
| 84 |
"## 🇹🇼 台灣中文輸入法加速器 \n"
|
| 85 |
+
"結合小型語言模型與 ZeroGPU,即時 IME 風格候選條。"
|
| 86 |
)
|
| 87 |
|
| 88 |
+
# 經典候選欄:水平排列
|
| 89 |
suggestions = gr.Radio(
|
| 90 |
+
[], label="", interactive=True, type="value",
|
| 91 |
+
elem_id="suggestions-bar", elem_classes="candidate-list"
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# 輸入區與按鈕:單行輸入框 + 小按鈕
|
| 95 |
with gr.Row():
|
| 96 |
+
input_text = gr.Textbox(
|
| 97 |
+
label="", placeholder="請輸入拼音或文字…", lines=1, max_lines=1
|
| 98 |
+
)
|
| 99 |
+
gpu_button = gr.Button("建議")
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
# 進階參數設定(可折疊)
|
| 102 |
+
with gr.Accordion("進階設定", open=False):
|
| 103 |
model_selector = gr.Dropdown(
|
| 104 |
+
MODEL_LIST, value=MODEL_LIST[0], label="模型"
|
| 105 |
)
|
| 106 |
k_slider = gr.Slider(
|
| 107 |
+
minimum=1, maximum=50, step=1, value=1, label="K(最大新詞元數)"
|
| 108 |
)
|
| 109 |
m_slider = gr.Slider(
|
| 110 |
+
minimum=1, maximum=30, step=1, value=10, label="M(建議數/Beam 數)"
|
| 111 |
)
|
| 112 |
|
| 113 |
# 事件綁定
|