Spaces:
Runtime error
Runtime error
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
|
4 |
+
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)#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 |
+
|
13 |
+
history_keyword_text = st.text_input("Enter users's history keywords (optional, i.e., 'Gates')", value="Gates")
|
14 |
+
|
15 |
+
text = st.text_input("Enter a text for auto completion...", value='Where is Bill')
|
16 |
+
|
17 |
+
model = st.selectbox("choose a model", ["roberta-base", "bert-base-uncased", "gpt2", "t5"])
|
18 |
+
|
19 |
+
data_load_state = st.text('Loading model...')
|
20 |
+
nlp = get_model(model)
|
21 |
+
|
22 |
+
if text:
|
23 |
+
data_load_state = st.text('Inference to model...')
|
24 |
+
result = nlp(text+' '+nlp.tokenizer.mask_token)
|
25 |
+
data_load_state.text('')
|
26 |
+
for index, r in enumerate(result):
|
27 |
+
if r['token_str'].lower().strip() in history_keyword_text.lower().strip():
|
28 |
+
#found from history, then increase the score of tokens
|
29 |
+
result[index]['score']*=HISTORY_WEIGHT
|
30 |
+
|
31 |
+
#sort the results
|
32 |
+
df=pd.DataFrame(result).sort_values(by='score', ascending=False)
|
33 |
+
#show the results as a table
|
34 |
+
st.table(df)
|