Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,30 +5,31 @@ from transformers import pipeline
|
|
| 5 |
|
| 6 |
@st.cache(allow_output_mutation=True)
|
| 7 |
def get_model(model):
|
| 8 |
-
return pipeline("fill-mask", model=model, top_k=100)
|
| 9 |
|
| 10 |
HISTORY_WEIGHT = 100 # set history weight (if found any keyword from history, it will priorities based on its weight)
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
history_keyword_text = st.text_input("Enter users's history keywords (optional, i.e., 'Gates')", value="Gates")
|
| 15 |
|
| 16 |
text = st.text_input("Enter a text for auto completion...", value='Where is Bill')
|
| 17 |
|
| 18 |
-
model = st.selectbox("choose a model", ["roberta-base", "bert-base-uncased"
|
| 19 |
|
| 20 |
data_load_state = st.text('Loading model...')
|
| 21 |
nlp = get_model(model)
|
| 22 |
|
| 23 |
if text:
|
| 24 |
-
|
| 25 |
data_load_state = st.text('Inference to model...')
|
| 26 |
result = nlp(text+' '+nlp.tokenizer.mask_token)
|
| 27 |
data_load_state.text('')
|
| 28 |
for index, r in enumerate(result):
|
| 29 |
-
if r['token_str'].lower().strip() in history_keyword_text.lower().strip():
|
|
|
|
| 30 |
result[index]['score']*=HISTORY_WEIGHT
|
|
|
|
|
|
|
| 31 |
df=pd.DataFrame(result).sort_values(by='score', ascending=False)
|
| 32 |
-
|
| 33 |
-
#result={k: v for k, v in sorted(result.items(), key=lambda item: item[0])}
|
| 34 |
st.table(df)
|
|
|
|
| 5 |
|
| 6 |
@st.cache(allow_output_mutation=True)
|
| 7 |
def get_model(model):
|
| 8 |
+
return pipeline("fill-mask", model=model, top_k=100)#seto maximum of tokens to be retrieved after each inference to model
|
| 9 |
|
| 10 |
HISTORY_WEIGHT = 100 # set history weight (if found any keyword from history, it will priorities based on its weight)
|
| 11 |
|
| 12 |
+
st.caption("This is a simple auto-completion where the next token is predicted per probability and a weigh if appears in user's history")
|
| 13 |
|
| 14 |
history_keyword_text = st.text_input("Enter users's history keywords (optional, i.e., 'Gates')", value="Gates")
|
| 15 |
|
| 16 |
text = st.text_input("Enter a text for auto completion...", value='Where is Bill')
|
| 17 |
|
| 18 |
+
model = st.selectbox("choose a model", ["roberta-base", "bert-base-uncased"])
|
| 19 |
|
| 20 |
data_load_state = st.text('Loading model...')
|
| 21 |
nlp = get_model(model)
|
| 22 |
|
| 23 |
if text:
|
|
|
|
| 24 |
data_load_state = st.text('Inference to model...')
|
| 25 |
result = nlp(text+' '+nlp.tokenizer.mask_token)
|
| 26 |
data_load_state.text('')
|
| 27 |
for index, r in enumerate(result):
|
| 28 |
+
if r['token_str'].lower().strip() in history_keyword_text.lower().strip() and len(r['token_str'].lower().strip())>1:
|
| 29 |
+
#found from history, then increase the score of tokens
|
| 30 |
result[index]['score']*=HISTORY_WEIGHT
|
| 31 |
+
|
| 32 |
+
#sort the results
|
| 33 |
df=pd.DataFrame(result).sort_values(by='score', ascending=False)
|
| 34 |
+
#show the results as a table
|
|
|
|
| 35 |
st.table(df)
|