mlrefkws / app.py
stmnk's picture
Update app.py
9c17c41
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 match'):
if question:
try:
url = f"{ES_URL}/document/_search?pretty"
payload = json.dumps({"query":{"match":{"content": question}}}) # "moldova"
headers = {'Content-Type': 'application/json'}
response = requests.request("GET", url, headers=headers, data=payload)
kws_result = response.json() # print(response.text)
except Exception as e:
qa_result = str(e)
top_5_hits = kws_result['hits']['hits'][:5] # print("First 5 results:")
top_5_text = [{'text': hit['_source']['content'][:500],
'confidence': hit['_score']} for hit in top_5_hits ]
for i, doc_hit in enumerate(top_5_text):
st.subheader(f'Search result #{i+1} (and score):')
st.write(f'<em>{doc_hit["text"]}...</em>', unsafe_allow_html = True)
st.markdown(f'> (*confidence score*: **{format(doc_hit["confidence"], ".3f")}**)')
st.write(f'Answer JSON: '); st.write(top_5_text) # st.write(qa_result)
else:
st.write('Write a query to submit your keyword search'); st.stop()
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, # "What is the capital city of Netherlands?"
"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)
kws_result = response.json() # print(response.text)
except Exception as e:
qa_result = str(e)
top_5_hits = kws_result['hits']['hits'][:5] # print("First 5 results:")
top_5_text = [{'text': hit['_source']['content'][:500],
'confidence': hit['_score']} for hit in top_5_hits ]
for i, doc_hit in enumerate(top_5_text):
st.subheader(f'Search result #{i+1} (and score):')
st.write(f'<em>{doc_hit["text"]}...</em>', unsafe_allow_html = True)
st.markdown(f'> (*confidence score*: **{format(doc_hit["confidence"], ".3f")}**)')
st.write(f'Answer JSON: '); st.write(top_5_text) # st.write(qa_result)
else:
st.write('Write a query to submit your keyword search'); st.stop()