stmnk commited on
Commit
79a2228
1 Parent(s): c04f9ac

Update app.py

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