Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
import json; import streamlit as st; import requests as req; from transformers import pipeline
|
2 |
WIKI_URL = 'https://en.wikipedia.org/w/api.php'; WIKI_BERT = "&titles=BERT_(language_model)"
|
3 |
WIKI_QUERY = "?format=json&action=query&prop=extracts&explaintext=1"; WIKI_METHOD = 'GET'
|
4 |
-
|
|
|
5 |
st.title('Question Answering example')
|
6 |
st.subheader('1. A simple question (extractive, closed domain)')
|
7 |
response = req.request(WIKI_METHOD, f'{WIKI_URL}{WIKI_QUERY}{WIKI_BERT}')
|
@@ -17,37 +18,20 @@ query_text = 'Question used for QA (you can also edit, and experiment with the a
|
|
17 |
written_question = st.text_input(query_text, question)
|
18 |
if written_question:
|
19 |
question = written_question
|
20 |
-
QA_URL = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"; QA_METHOD = 'POST'
|
21 |
if st.button('Run QA inference (get answer prediction)'):
|
22 |
if paragraph and question:
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
if "answer" in answer_dict.keys():
|
31 |
-
answer_span, answer_score = answer_dict["answer"], answer_dict["score"]
|
32 |
st.write(f'Answer: **{answer_span}**')
|
33 |
-
start_par, stop_para = max(0,
|
34 |
answer_context = paragraph[start_par:stop_para].replace(answer_span, f'**{answer_span}**')
|
35 |
st.write(f'Answer context (and score): ... _{answer_context}_ ... (score: {format(answer_score, ".3f")})')
|
36 |
-
|
37 |
-
|
38 |
-
try:
|
39 |
-
qa_result = pipe_exqa(question=question, context=paragraph)
|
40 |
-
except Exception as e:
|
41 |
-
qa_result = str(e)
|
42 |
-
|
43 |
-
if "answer" in qa_result.keys():
|
44 |
-
answer_span, answer_score = qa_result["answer"], qa_result["score"]
|
45 |
-
st.write(f'Answer: **{answer_span}**')
|
46 |
-
start_par, stop_para = max(0, qa_result["start"]-86), min(qa_result["end"]+90, len(paragraph))
|
47 |
-
answer_context = paragraph[start_par:stop_para].replace(answer_span, f'**{answer_span}**')
|
48 |
-
st.write(f'Answer context (and score): ... _{answer_context}_ ... (score: {format(answer_score, ".3f")})')
|
49 |
-
|
50 |
-
st.write(f'Answer JSON: '); st.write(qa_result)
|
51 |
else:
|
52 |
st.write('Write some passage of text and a question'); st.stop()
|
53 |
-
# x = st.slider('Select a value'); st.write(x, 'squared is', x * x)
|
|
|
1 |
import json; import streamlit as st; import requests as req; from transformers import pipeline
|
2 |
WIKI_URL = 'https://en.wikipedia.org/w/api.php'; WIKI_BERT = "&titles=BERT_(language_model)"
|
3 |
WIKI_QUERY = "?format=json&action=query&prop=extracts&explaintext=1"; WIKI_METHOD = 'GET'
|
4 |
+
DPR_MODEL = "deepset/roberta-base-squad2" #, model="distilbert-base-cased-distilled-squad"
|
5 |
+
pipe_exqa = pipeline("question-answering", model=DPR_MODEL)
|
6 |
st.title('Question Answering example')
|
7 |
st.subheader('1. A simple question (extractive, closed domain)')
|
8 |
response = req.request(WIKI_METHOD, f'{WIKI_URL}{WIKI_QUERY}{WIKI_BERT}')
|
|
|
18 |
written_question = st.text_input(query_text, question)
|
19 |
if written_question:
|
20 |
question = written_question
|
|
|
21 |
if st.button('Run QA inference (get answer prediction)'):
|
22 |
if paragraph and question:
|
23 |
+
try:
|
24 |
+
qa_result = pipe_exqa(question=question, context=paragraph)
|
25 |
+
except Exception as e:
|
26 |
+
qa_result = str(e)
|
27 |
+
|
28 |
+
if "answer" in qa_result.keys():
|
29 |
+
answer_span, answer_score = qa_result["answer"], qa_result["score"]
|
|
|
|
|
30 |
st.write(f'Answer: **{answer_span}**')
|
31 |
+
start_par, stop_para = max(0, qa_result["start"]-86), min(qa_result["end"]+90, len(paragraph))
|
32 |
answer_context = paragraph[start_par:stop_para].replace(answer_span, f'**{answer_span}**')
|
33 |
st.write(f'Answer context (and score): ... _{answer_context}_ ... (score: {format(answer_score, ".3f")})')
|
34 |
+
|
35 |
+
st.write(f'Answer JSON: '); st.write(qa_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
else:
|
37 |
st.write('Write some passage of text and a question'); st.stop()
|
|