fix gradio.exceptions.Error: "Value: ' 」 、 《 》 、' (type: <class 'str'>) is not in the list of choices: []"
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# app.py
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from functools import lru_cache
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 6 |
|
|
@@ -25,7 +26,7 @@ def get_pipeline(model_name):
|
|
| 25 |
@spaces.GPU
|
| 26 |
def suggest_next(text, model_name, k, m):
|
| 27 |
"""
|
| 28 |
-
使用 Beam Search 產生 M
|
| 29 |
"""
|
| 30 |
gen_pipe = get_pipeline(model_name)
|
| 31 |
outs = gen_pipe(
|
|
@@ -36,8 +37,9 @@ def suggest_next(text, model_name, k, m):
|
|
| 36 |
do_sample=False,
|
| 37 |
early_stopping=True
|
| 38 |
)
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
def append_suggestion(current, choice):
|
| 43 |
return current + choice
|
|
|
|
| 1 |
# app.py
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
+
from gradio import update
|
| 5 |
from functools import lru_cache
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 7 |
|
|
|
|
| 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(
|
|
|
|
| 37 |
do_sample=False,
|
| 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
|