stmnk commited on
Commit
c04f9ac
1 Parent(s): 3e4a3e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os; import json; import requests
2
+ import streamlit as st
3
+ ES_URL = os.environ.get("ES_URL")
4
+
5
+ question = 'What is the capital of Netherlands?'
6
+ query_text = 'Query used for keyword search (you can also edit, and experiment with the responses)'
7
+ written_question = st.text_input(query_text, question)
8
+ if written_question:
9
+ question = written_question
10
+ if st.button('Run keyword search'):
11
+ if question:
12
+ try:
13
+ # qa_result = pipe_exqa(question=question, context=paragraph)
14
+ url = f"{ES_URL}/document/_search?pretty"
15
+ # payload = json.dumps({"query":{"match":{"content":"moldova"}}})
16
+ payload = json.dumps({"query": {
17
+ "more_like_this": { "like": question, # "What is the capital city of Netherlands?"
18
+ "fields": ["content"], "min_term_freq": 1.9, "min_doc_freq": 4, "max_query_terms": 50
19
+ }}})
20
+ headers = {'Content-Type': 'application/json'}
21
+ response = requests.request("GET", url, headers=headers, data=payload)
22
+ qa_result = response.json() # print(response.text)
23
+
24
+ except Exception as e:
25
+ qa_result = str(e)
26
+
27
+ # if "answer" in qa_result.keys():
28
+ # answer_span, answer_score = qa_result["answer"], qa_result["score"]
29
+ # st.write(f'Answer: **{answer_span}**')
30
+ # start_par, stop_para = max(0, qa_result["start"]-86), min(qa_result["end"]+90, len(paragraph))
31
+ # answer_context = paragraph[start_par:stop_para].replace(answer_span, f'**{answer_span}**')
32
+ # st.write(f'Answer context (and score): ... _{answer_context}_ ... (score: {format(answer_score, ".3f")})')
33
+
34
+ st.write(f'Answer JSON: '); st.write(qa_result)
35
+ else:
36
+ st.write('Write a query to submit your keyword search'); st.stop()
37
+
38
+ """
39
+ result_first_two_hits = result['hits']['hits'][:2] # print("First 2 results:")
40
+ question_similarity = [ (hit['_score'], hit['_source']['content'][:200])
41
+ for hit in result_first_two_hits ] # print(question_similarity)
42
+
43
+ top_hit = result['hits']['hits'][0]
44
+ context = top_hit['_source']['content']
45
+ # context = r" Extractive Question Answering is the task of extracting
46
+ # an answer from a text given a question. An example of a question
47
+ # answering dataset is the SQuAD dataset, which is entirely based
48
+ # on that task. If you would like to fine-tune a model on a SQuAD task,
49
+ # you may leverage the `examples/pytorch/question-answering/run_squad.py` script."
50
+ question = input # "What is extractive question answering?"
51
+ # "What is a good example of a question answering dataset?"
52
+ print(question)
53
+ context = context[:5000]
54
+ print(context)
55
+ try:
56
+ qa_result = pipe_exqa(question=question, context=context)
57
+ except Exception as e:
58
+ return {"output": str(e)}
59
+
60
+ return {"output": str(qa_result)}
61
+
62
+ answer = qa_result['answer']
63
+ score = round(qa_result['score'], 4)
64
+ span = f"start: {qa_result['start']}, end: {qa_result['end']}"
65
+ # st.write(answer); st.write(f"score: {score}"); st.write(f"span: {span}")
66
+ output = f"{str(answer)} \n {str(score)} \n {str(span)}"
67
+
68
+ return {"output": output} or {"output": str(question_similarity)} or result or {"Hello": "World!"}
69
+ """