|
import os; import json; import requests |
|
import streamlit as st |
|
ES_URL = os.environ.get("ES_URL") |
|
|
|
question = 'What is the capital of Netherlands?' |
|
query_text = 'Query used for keyword search (you can also edit, and experiment with the responses)' |
|
written_question = st.text_input(query_text, question) |
|
if written_question: |
|
question = written_question |
|
if st.button('Run keyword search'): |
|
if question: |
|
try: |
|
|
|
url = f"{ES_URL}/document/_search?pretty" |
|
|
|
payload = json.dumps({"query": { |
|
"more_like_this": { "like": question, |
|
"fields": ["content"], "min_term_freq": 1.9, "min_doc_freq": 4, "max_query_terms": 50 |
|
}}}) |
|
headers = {'Content-Type': 'application/json'} |
|
response = requests.request("GET", url, headers=headers, data=payload) |
|
qa_result = response.json() |
|
|
|
except Exception as e: |
|
qa_result = str(e) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.write(f'Answer JSON: '); st.write(qa_result) |
|
else: |
|
st.write('Write a query to submit your keyword search'); st.stop() |
|
|
|
""" |
|
result_first_two_hits = result['hits']['hits'][:2] # print("First 2 results:") |
|
question_similarity = [ (hit['_score'], hit['_source']['content'][:200]) |
|
for hit in result_first_two_hits ] # print(question_similarity) |
|
|
|
top_hit = result['hits']['hits'][0] |
|
context = top_hit['_source']['content'] |
|
# context = r" Extractive Question Answering is the task of extracting |
|
# an answer from a text given a question. An example of a question |
|
# answering dataset is the SQuAD dataset, which is entirely based |
|
# on that task. If you would like to fine-tune a model on a SQuAD task, |
|
# you may leverage the `examples/pytorch/question-answering/run_squad.py` script." |
|
question = input # "What is extractive question answering?" |
|
# "What is a good example of a question answering dataset?" |
|
print(question) |
|
context = context[:5000] |
|
print(context) |
|
try: |
|
qa_result = pipe_exqa(question=question, context=context) |
|
except Exception as e: |
|
return {"output": str(e)} |
|
|
|
return {"output": str(qa_result)} |
|
|
|
answer = qa_result['answer'] |
|
score = round(qa_result['score'], 4) |
|
span = f"start: {qa_result['start']}, end: {qa_result['end']}" |
|
# st.write(answer); st.write(f"score: {score}"); st.write(f"span: {span}") |
|
output = f"{str(answer)} \n {str(score)} \n {str(span)}" |
|
|
|
return {"output": output} or {"output": str(question_similarity)} or result or {"Hello": "World!"} |
|
""" |