samarthagarwal23 commited on
Commit
5e68e2f
1 Parent(s): c010f14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -66,14 +66,14 @@ def cstr(s, color='black'):
66
  def cstr_bold(s, color='black'):
67
  return "<text style=color:{}><b>{}</b></text>".format(color, s)
68
  def cstr_break(s, color='black'):
69
- return "<text style=color:{}>{}<br></text>".format(color, s)
70
 
71
  def print_colored(text, start_idx, end_idx, confidence):
72
  conf_str = 'Confidence: ' + confidence
73
  a = cstr(' '.join([text[:start_idx], \
74
- cstr_bold(text[start_idx:end_idx], color='red'), \
75
  text[end_idx:], \
76
- cstr_break(conf_str, color='yellow')]), color='black')
77
  return a
78
 
79
  def final_qa_pipeline(file, query):
@@ -91,13 +91,18 @@ def final_qa_pipeline(file, query):
91
  fnl_rank = qa_ranker(query, [l["docs"] for l in lvl1], top_k_ranker)
92
  #return (fnl_rank[0]["answer"], str(np.round(100*fnl_rank[0]["score"],2))+"%" , fnl_rank[0]['doc'])
93
  #return (print_colored(fnl_rank[0]['doc'], fnl_rank[0]['start'], fnl_rank[0]['end']), str(np.round(100*fnl_rank[0]["score"],2))+"%"
94
- return (print_colored(fnl_rank[0]['doc'], fnl_rank[0]['start'], fnl_rank[0]['end'], str(np.round(100*fnl_rank[0]["score"],2))+"%") )
 
 
 
 
 
95
  #for fnl_ in fnl_rank:
96
  # print("\n")
97
  # print_colored(fnl_['doc'], fnl_['start'], fnl_['end'])
98
  # print(colored("Confidence score of ") + colored(str(fnl_['score'])[:4], attrs=['bold']))
99
  else:
100
- return ("No match")
101
 
102
  examples = [
103
  [os.path.abspath("dbs-annual-report-2020.pdf"), "how much dividend was paid to shareholders ?"],
@@ -111,7 +116,7 @@ examples = [
111
  iface = gr.Interface(
112
  fn = final_qa_pipeline,
113
  inputs = [gr.inputs.File(label="input pdf file"), gr.inputs.Textbox(label="Question:")],
114
- outputs = [gr.outputs.HTML(label="Predicted answer")],
115
  examples=examples,
116
  title = "Question Answering on company annual reports",
117
  description = "Navigate long annual reports by using Machine learning to answer your questions. \nSimply upload any annual report pdf you are interested in and ask model a question OR load an example from below."
 
66
  def cstr_bold(s, color='black'):
67
  return "<text style=color:{}><b>{}</b></text>".format(color, s)
68
  def cstr_break(s, color='black'):
69
+ return "<text style=color:{}><br>{}</text>".format(color, s)
70
 
71
  def print_colored(text, start_idx, end_idx, confidence):
72
  conf_str = 'Confidence: ' + confidence
73
  a = cstr(' '.join([text[:start_idx], \
74
+ cstr_bold(text[start_idx:end_idx], color='blue'), \
75
  text[end_idx:], \
76
+ cstr_break(conf_str, color='grey')]), color='black')
77
  return a
78
 
79
  def final_qa_pipeline(file, query):
 
91
  fnl_rank = qa_ranker(query, [l["docs"] for l in lvl1], top_k_ranker)
92
  #return (fnl_rank[0]["answer"], str(np.round(100*fnl_rank[0]["score"],2))+"%" , fnl_rank[0]['doc'])
93
  #return (print_colored(fnl_rank[0]['doc'], fnl_rank[0]['start'], fnl_rank[0]['end']), str(np.round(100*fnl_rank[0]["score"],2))+"%"
94
+ top1 = print_colored(fnl_rank[0]['doc'], fnl_rank[0]['start'], fnl_rank[0]['end'], str(np.round(100*fnl_rank[0]["score"],2))+"%")
95
+ if len(lvl1)>1:
96
+ top2 = print_colored(fnl_rank[1]['doc'], fnl_rank[1]['start'], fnl_rank[1]['end'], str(np.round(100*fnl_rank[1]["score"],2))+"%")
97
+ else:
98
+ top2 = "None"
99
+ return (top1, top2)
100
  #for fnl_ in fnl_rank:
101
  # print("\n")
102
  # print_colored(fnl_['doc'], fnl_['start'], fnl_['end'])
103
  # print(colored("Confidence score of ") + colored(str(fnl_['score'])[:4], attrs=['bold']))
104
  else:
105
+ return ("No match","No match")
106
 
107
  examples = [
108
  [os.path.abspath("dbs-annual-report-2020.pdf"), "how much dividend was paid to shareholders ?"],
 
116
  iface = gr.Interface(
117
  fn = final_qa_pipeline,
118
  inputs = [gr.inputs.File(label="input pdf file"), gr.inputs.Textbox(label="Question:")],
119
+ outputs = [gr.outputs.HTML(label="Top 1 answer"), gr.outputs.HTML(label="Top 2 answer")],
120
  examples=examples,
121
  title = "Question Answering on company annual reports",
122
  description = "Navigate long annual reports by using Machine learning to answer your questions. \nSimply upload any annual report pdf you are interested in and ask model a question OR load an example from below."