Spaces:
Running
on
Zero
Running
on
Zero
update layout
Browse files
app.py
CHANGED
@@ -26,7 +26,7 @@ def get_pipeline(model_name):
|
|
26 |
@spaces.GPU
|
27 |
def suggest_next(text, model_name, k, m):
|
28 |
"""
|
29 |
-
使用 Beam Search 產生 M
|
30 |
"""
|
31 |
gen_pipe = get_pipeline(model_name)
|
32 |
outs = gen_pipe(
|
@@ -38,21 +38,24 @@ def suggest_next(text, model_name, k, m):
|
|
38 |
early_stopping=True
|
39 |
)
|
40 |
suggestions = [out["generated_text"][len(text):] for out in outs]
|
41 |
-
#
|
42 |
-
return update(choices=suggestions, value=
|
43 |
|
44 |
def append_suggestion(current, choice):
|
45 |
return current + choice
|
46 |
|
47 |
with gr.Blocks() as demo:
|
48 |
gr.Markdown(
|
49 |
-
"## 🇹🇼
|
50 |
-
"結合小型語言模型與 ZeroGPU
|
51 |
)
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
56 |
|
57 |
with gr.Row():
|
58 |
model_selector = gr.Dropdown(
|
@@ -65,14 +68,16 @@ with gr.Blocks() as demo:
|
|
65 |
minimum=1, maximum=10, step=1, value=5, label="M(建議數量 / Beam 數)"
|
66 |
)
|
67 |
|
68 |
-
|
69 |
-
|
70 |
|
|
|
71 |
gpu_button.click(
|
72 |
fn=suggest_next,
|
73 |
inputs=[input_text, model_selector, k_slider, m_slider],
|
74 |
outputs=suggestions,
|
75 |
)
|
|
|
76 |
suggestions.change(
|
77 |
fn=append_suggestion,
|
78 |
inputs=[input_text, suggestions],
|
|
|
26 |
@spaces.GPU
|
27 |
def suggest_next(text, model_name, k, m):
|
28 |
"""
|
29 |
+
使用 Beam Search 產生 M 條最可能的下段建議,並一次更新可選項清單。
|
30 |
"""
|
31 |
gen_pipe = get_pipeline(model_name)
|
32 |
outs = gen_pipe(
|
|
|
38 |
early_stopping=True
|
39 |
)
|
40 |
suggestions = [out["generated_text"][len(text):] for out in outs]
|
41 |
+
# 更新 Radio choices,預設不選中任何項
|
42 |
+
return update(choices=suggestions, value=None)
|
43 |
|
44 |
def append_suggestion(current, choice):
|
45 |
return current + choice
|
46 |
|
47 |
with gr.Blocks() as demo:
|
48 |
gr.Markdown(
|
49 |
+
"## 🇹🇼 台灣中文下段預測 \n"
|
50 |
+
"結合小型語言模型與 ZeroGPU,一次展開所有建議,快速點選並拼接文字。"
|
51 |
)
|
52 |
|
53 |
+
# 將輸入框與生成按鈕並排
|
54 |
+
with gr.Row():
|
55 |
+
input_text = gr.TextArea(
|
56 |
+
label="輸入文字", lines=4, placeholder="請在此輸入起始片段…"
|
57 |
+
)
|
58 |
+
gpu_button = gr.Button("使用 GPU 生成建議")
|
59 |
|
60 |
with gr.Row():
|
61 |
model_selector = gr.Dropdown(
|
|
|
68 |
minimum=1, maximum=10, step=1, value=5, label="M(建議數量 / Beam 數)"
|
69 |
)
|
70 |
|
71 |
+
# 直接展開的建議清單(Radio)
|
72 |
+
suggestions = gr.Radio([], label="建議清單")
|
73 |
|
74 |
+
# 連結生成按鈕到推理函式
|
75 |
gpu_button.click(
|
76 |
fn=suggest_next,
|
77 |
inputs=[input_text, model_selector, k_slider, m_slider],
|
78 |
outputs=suggestions,
|
79 |
)
|
80 |
+
# 點選建議後立刻拼接到文字框
|
81 |
suggestions.change(
|
82 |
fn=append_suggestion,
|
83 |
inputs=[input_text, suggestions],
|