samarthagarwal23 commited on
Commit
b5b0fd2
1 Parent(s): 261aa30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -4
app.py CHANGED
@@ -63,9 +63,11 @@ def qa_ranker(query, docs_, top_k_ranker):
63
 
64
  def cstr(s, color='black'):
65
  return "<text style=color:{}>{}</text>".format(color, s)
 
 
66
 
67
  def print_colored(text, start_idx, end_idx):
68
- a = cstr(' '.join([text[:start_idx], cstr(text[start_idx:end_idx], color='red'), text[end_idx:]]), color='black')
69
  #a = colored(text[:start_idx]) + colored(text[start_idx:end_idx], 'red', 'on_yellow') + colored(text[end_idx:])
70
  return a
71
 
@@ -83,13 +85,13 @@ def final_qa_pipeline(file, query):
83
  if len(lvl1) > 0:
84
  fnl_rank = qa_ranker(query, [l["docs"] for l in lvl1], top_k_ranker)
85
  #return (fnl_rank[0]["answer"], str(np.round(100*fnl_rank[0]["score"],2))+"%" , fnl_rank[0]['doc'])
86
- return (fnl_rank[0]["answer"], str(np.round(100*fnl_rank[0]["score"],2))+"%" , print_colored(fnl_rank[0]['doc'], fnl_rank[0]['start'], fnl_rank[0]['end']))
87
  #for fnl_ in fnl_rank:
88
  # print("\n")
89
  # print_colored(fnl_['doc'], fnl_['start'], fnl_['end'])
90
  # print(colored("Confidence score of ") + colored(str(fnl_['score'])[:4], attrs=['bold']))
91
  else:
92
- return ("No match", 0)
93
 
94
  examples = [
95
  [os.path.abspath("dbs-annual-report-2020.pdf"), "how much dividend was paid to shareholders ?"],
@@ -103,7 +105,7 @@ examples = [
103
  iface = gr.Interface(
104
  fn = final_qa_pipeline,
105
  inputs = [gr.inputs.File(label="input pdf file"), gr.inputs.Textbox(label="Question:")],
106
- outputs = [gr.outputs.Textbox(label="Answer"), gr.outputs.Textbox(label="Confidence"), gr.outputs.HTML(label="Reference text")],
107
  examples=examples,
108
  title = "Question Answering on company annual reports",
109
  description = "Simply upload any annual report pdf you are interested in and ask model a question OR load an example from below."
 
63
 
64
  def cstr(s, color='black'):
65
  return "<text style=color:{}>{}</text>".format(color, s)
66
+ def cstr_bold(s, color='black'):
67
+ return "<text style=color:{}><b>{}</b></text>".format(color, s)
68
 
69
  def print_colored(text, start_idx, end_idx):
70
+ a = cstr(' '.join([text[:start_idx], cstr_bold(text[start_idx:end_idx], color='red'), text[end_idx:]]), color='black')
71
  #a = colored(text[:start_idx]) + colored(text[start_idx:end_idx], 'red', 'on_yellow') + colored(text[end_idx:])
72
  return a
73
 
 
85
  if len(lvl1) > 0:
86
  fnl_rank = qa_ranker(query, [l["docs"] for l in lvl1], top_k_ranker)
87
  #return (fnl_rank[0]["answer"], str(np.round(100*fnl_rank[0]["score"],2))+"%" , fnl_rank[0]['doc'])
88
+ return (print_colored(fnl_rank[0]['doc'], fnl_rank[0]['start'], fnl_rank[0]['end']), str(np.round(100*fnl_rank[0]["score"],2))+"%" )
89
  #for fnl_ in fnl_rank:
90
  # print("\n")
91
  # print_colored(fnl_['doc'], fnl_['start'], fnl_['end'])
92
  # print(colored("Confidence score of ") + colored(str(fnl_['score'])[:4], attrs=['bold']))
93
  else:
94
+ return ("No match", "0")
95
 
96
  examples = [
97
  [os.path.abspath("dbs-annual-report-2020.pdf"), "how much dividend was paid to shareholders ?"],
 
105
  iface = gr.Interface(
106
  fn = final_qa_pipeline,
107
  inputs = [gr.inputs.File(label="input pdf file"), gr.inputs.Textbox(label="Question:")],
108
+ outputs = [gr.outputs.HTML(label="Predicted answer"), gr.outputs.Textbox(label="Confidence") ],
109
  examples=examples,
110
  title = "Question Answering on company annual reports",
111
  description = "Simply upload any annual report pdf you are interested in and ask model a question OR load an example from below."