Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,9 @@ model_name = "distilbert-base-cased"
|
|
7 |
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
|
8 |
model = DistilBertForQuestionAnswering.from_pretrained(model_name)
|
9 |
|
|
|
|
|
|
|
10 |
def format_response(start_index, end_index, raw_answer):
|
11 |
answer_tokens = tokenizer.convert_tokens_to_string([tokenizer.convert_ids_to_tokens(i)[0] for i in range(start_index, end_index+1)])
|
12 |
return {'answer': answer_tokens.strip(), 'score': None}
|
@@ -28,16 +31,19 @@ def interactive():
|
|
28 |
while query != st.session_state.get('current_query', ''):
|
29 |
if ('current_query' not in st.session_state) or (st.session_state['current_query'] != query):
|
30 |
st.session_state['current_query'] = query
|
31 |
-
st.text_input("Ask me something or type 'quit' to exit:", "", key='my_input')
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
st.set_page_config(layout="wide")
|
|
|
7 |
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
|
8 |
model = DistilBertForQuestionAnswering.from_pretrained(model_name)
|
9 |
|
10 |
+
def handle_query_change(_, query):
|
11 |
+
update_displayed_query(query)
|
12 |
+
|
13 |
def format_response(start_index, end_index, raw_answer):
|
14 |
answer_tokens = tokenizer.convert_tokens_to_string([tokenizer.convert_ids_to_tokens(i)[0] for i in range(start_index, end_index+1)])
|
15 |
return {'answer': answer_tokens.strip(), 'score': None}
|
|
|
31 |
while query != st.session_state.get('current_query', ''):
|
32 |
if ('current_query' not in st.session_state) or (st.session_state['current_query'] != query):
|
33 |
st.session_state['current_query'] = query
|
34 |
+
st.text_input("Ask me something or type 'quit' to exit:", "", key='my_input', on_change=handle_query_change) # use handle_query_change function here
|
35 |
+
|
36 |
+
if ('shown_query' not in st.session_state) or (st.session_state['shown_query'] != query):
|
37 |
+
st.session_state['shown_query'] = query
|
38 |
+
if len(query) > 0 and query != 'quit':
|
39 |
+
try:
|
40 |
+
context = "The capital of France is Paris."
|
41 |
+
response = get_answers(query, context)
|
42 |
+
st.write(json.dumps(response))
|
43 |
+
except Exception as e:
|
44 |
+
st.write(f"Error occurred: {str(e)}")
|
45 |
+
|
46 |
+
query = st.session_state.get('current_query', '')
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
st.set_page_config(layout="wide")
|