Luigi commited on
Commit
7e8e5b2
·
1 Parent(s): 2124f23

update layout

Browse files
Files changed (1) hide show
  1. app.py +15 -10
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 條最可能的下段建議,並一次更新下拉選單的 choices & 選值。
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
- # 透過 update 一次設定下拉的 choices 及當前 value
42
- return update(choices=suggestions, value=suggestions[0] if suggestions else "")
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,提供 Beam Search 風格的多條下段建議。"
51
  )
52
 
53
- input_text = gr.TextArea(
54
- label="輸入文字", lines=4, placeholder="請在此輸入起始片段…"
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
- suggestions = gr.Dropdown([], label="建議清單", interactive=True)
69
- gpu_button = gr.Button("使用 GPU 生成建議")
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],