Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,17 +14,29 @@ def get_model(model):
|
|
14 |
@st.cache(allow_output_mutation=True)
|
15 |
def loading_models(model='roberta-base'):
|
16 |
return get_model(model), SentenceTransformer('all-MiniLM-L6-v2')
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
data_load_state.text('Inference from model...')
|
21 |
-
result =
|
22 |
sem_list=[semantic_text.strip()]
|
23 |
data_load_state.text('Checking similarity...')
|
24 |
if len(semantic_text):
|
25 |
predicted_seq=[rec['sequence'] for rec in result]
|
26 |
-
predicted_embeddings =
|
27 |
-
semantic_history_embeddings = semantic_model.encode(sem_list, convert_to_tensor=True)
|
28 |
cosine_scores = util.cos_sim(predicted_embeddings, semantic_history_embeddings)
|
29 |
data_load_state.text('similarity check completed...')
|
30 |
|
@@ -39,11 +51,7 @@ def main(nlp, semantic_model, data_load_state):
|
|
39 |
|
40 |
#sort the results
|
41 |
df=pd.DataFrame(result).sort_values(by='score', ascending=False)
|
42 |
-
|
43 |
-
# show the results as a table
|
44 |
-
st.table(df)
|
45 |
-
# print(df)
|
46 |
-
data_load_state.text('')
|
47 |
|
48 |
|
49 |
if __name__ == '__main__':
|
@@ -54,21 +62,20 @@ This is an example of an auto-complete approach where the next token suggested b
|
|
54 |
The next token is predicted per probability and a weight if it is appeared in keyword user's history or there is a similarity to semantic user's history
|
55 |
""")
|
56 |
history_keyword_text = st.text_input("Enter users's history <Keywords Match> (optional, i.e., 'Gates')", value="")
|
|
|
57 |
semantic_text = st.text_input("Enter users's history <Semantic> (optional, i.e., 'Microsoft' or 'President')", value="Microsoft")
|
58 |
|
59 |
text = st.text_input("Enter a text for auto completion...", value='Where is Bill')
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
model = st.selectbox("Choose a model", ["roberta-base", "bert-base-uncased"])
|
64 |
|
65 |
data_load_state = st.text('1.Loading model ...')
|
66 |
|
67 |
-
# semantic_model = SentenceTransformer('all-MiniLM-L6-v2')
|
68 |
-
# nlp = get_model(model)
|
69 |
nlp, semantic_model = loading_models(model)
|
70 |
|
71 |
-
main(
|
|
|
|
|
|
|
72 |
else:
|
73 |
sys.argv = ['streamlit', 'run', sys.argv[0]]
|
74 |
sys.exit(stcli.main())
|
|
|
14 |
@st.cache(allow_output_mutation=True)
|
15 |
def loading_models(model='roberta-base'):
|
16 |
return get_model(model), SentenceTransformer('all-MiniLM-L6-v2')
|
17 |
+
|
18 |
+
def infer(text):
|
19 |
+
global nlp
|
20 |
+
return nlp(text+' '+nlp.tokenizer.mask_token)
|
21 |
+
|
22 |
+
def sim(predicted_seq, sem_list):
|
23 |
+
return semantic_model.encode(predicted_seq, convert_to_tensor=True), \
|
24 |
+
semantic_model.encode(sem_list, convert_to_tensor=True)
|
25 |
+
|
26 |
+
def hash_func(inp):
|
27 |
+
#bypass hash function
|
28 |
+
return True
|
29 |
+
|
30 |
+
@st.cache(allow_output_mutation=True, hash_funcs={'tokenizers.Tokenizer': hash_func, 'tokenizers.AddedToken': hash_func})
|
31 |
+
def main(text,semantic_text,history_keyword_text):
|
32 |
+
global semantic_model, data_load_state
|
33 |
data_load_state.text('Inference from model...')
|
34 |
+
result = infer(text)
|
35 |
sem_list=[semantic_text.strip()]
|
36 |
data_load_state.text('Checking similarity...')
|
37 |
if len(semantic_text):
|
38 |
predicted_seq=[rec['sequence'] for rec in result]
|
39 |
+
predicted_embeddings, semantic_history_embeddings = sim(predicted_seq, sem_list)
|
|
|
40 |
cosine_scores = util.cos_sim(predicted_embeddings, semantic_history_embeddings)
|
41 |
data_load_state.text('similarity check completed...')
|
42 |
|
|
|
51 |
|
52 |
#sort the results
|
53 |
df=pd.DataFrame(result).sort_values(by='score', ascending=False)
|
54 |
+
return df
|
|
|
|
|
|
|
|
|
55 |
|
56 |
|
57 |
if __name__ == '__main__':
|
|
|
62 |
The next token is predicted per probability and a weight if it is appeared in keyword user's history or there is a similarity to semantic user's history
|
63 |
""")
|
64 |
history_keyword_text = st.text_input("Enter users's history <Keywords Match> (optional, i.e., 'Gates')", value="")
|
65 |
+
|
66 |
semantic_text = st.text_input("Enter users's history <Semantic> (optional, i.e., 'Microsoft' or 'President')", value="Microsoft")
|
67 |
|
68 |
text = st.text_input("Enter a text for auto completion...", value='Where is Bill')
|
|
|
|
|
|
|
69 |
model = st.selectbox("Choose a model", ["roberta-base", "bert-base-uncased"])
|
70 |
|
71 |
data_load_state = st.text('1.Loading model ...')
|
72 |
|
|
|
|
|
73 |
nlp, semantic_model = loading_models(model)
|
74 |
|
75 |
+
df=main(text,semantic_text,history_keyword_text)
|
76 |
+
#show the results as a table
|
77 |
+
st.table(df)
|
78 |
+
data_load_state.text('')
|
79 |
else:
|
80 |
sys.argv = ['streamlit', 'run', sys.argv[0]]
|
81 |
sys.exit(stcli.main())
|