oliverwang15 commited on
Commit
4831f4c
1 Parent(s): 4d47539

Multi-files input supported

Browse files
Files changed (3) hide show
  1. app.py +28 -9
  2. backend.py +134 -39
  3. openai.py +9 -2
app.py CHANGED
@@ -31,7 +31,7 @@ with gr.Blocks(theme="dark") as demo:
31
  label='Enter your OpenAI API key here',
32
  type='password')
33
 
34
- file = gr.File(label='Upload your .txt file here', file_types=['.txt'])
35
 
36
  questions = gr.CheckboxGroup(choices = QUESTIONS, value = QUESTIONS, label="Questions", info="Please select the question you want to ask")
37
 
@@ -42,14 +42,23 @@ with gr.Blocks(theme="dark") as demo:
42
  with gr.Group():
43
  gr.Markdown(f'<center><h1>Output</h1></center>')
44
  gr.Markdown(f'<center><p>The answer to your question is :</p></center>')
 
45
  question_box = gr.Textbox(label='Question')
46
  answer_box = gr.Textbox(label='Answer')
47
  # reference_box = gr.Textbox(label='Reference')
48
 
49
  highlighted_text = gr.outputs.HTML(label="Highlighted Text")
50
- with gr.Row():
51
- btn_last_question = gr.Button(value='Last Question')
52
- btn_next_question = gr.Button(value='Next Question')
 
 
 
 
 
 
 
 
53
 
54
  # Correctness
55
  with gr.Group():
@@ -103,7 +112,7 @@ with gr.Blocks(theme="dark") as demo:
103
  btn_submit_txt.click(
104
  backend.process_file,
105
  inputs=[file, questions, openai_key],
106
- outputs=[question_box, answer_box, highlighted_text, correct_answer, correct_reference],
107
  )
108
 
109
  btn_submit_correctness.click( # TODO
@@ -114,15 +123,25 @@ with gr.Blocks(theme="dark") as demo:
114
 
115
  # Switch question button
116
  btn_last_question.click(
117
- backend.process_last,
118
- outputs=[question_box, answer_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
119
- )
120
 
121
  btn_next_question.click(
122
  backend.process_next,
123
- outputs=[question_box, answer_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
124
  )
125
 
 
 
 
 
 
 
 
 
 
 
126
  # Download button
127
  btn_download_answer.click(
128
  backend.download_answer,
 
31
  label='Enter your OpenAI API key here',
32
  type='password')
33
 
34
+ file = gr.File(label='Upload your .txt file here', file_types=['.txt'], file_count = 'multiple')
35
 
36
  questions = gr.CheckboxGroup(choices = QUESTIONS, value = QUESTIONS, label="Questions", info="Please select the question you want to ask")
37
 
 
42
  with gr.Group():
43
  gr.Markdown(f'<center><h1>Output</h1></center>')
44
  gr.Markdown(f'<center><p>The answer to your question is :</p></center>')
45
+ filename_box = gr.Textbox(label = "File")
46
  question_box = gr.Textbox(label='Question')
47
  answer_box = gr.Textbox(label='Answer')
48
  # reference_box = gr.Textbox(label='Reference')
49
 
50
  highlighted_text = gr.outputs.HTML(label="Highlighted Text")
51
+ with gr.Group():
52
+ gr.Markdown("<center><h4>Please select different questions</h4></center>")
53
+ with gr.Row():
54
+ btn_last_question = gr.Button(value='Last Question')
55
+ btn_next_question = gr.Button(value='Next Question')
56
+
57
+ with gr.Group():
58
+ gr.Markdown("<center><h4>Please select different passages</h4></center>")
59
+ with gr.Row():
60
+ btn_last_passage = gr.Button(value='Last Passage')
61
+ btn_next_passage = gr.Button(value='Next Passage')
62
 
63
  # Correctness
64
  with gr.Group():
 
112
  btn_submit_txt.click(
113
  backend.process_file,
114
  inputs=[file, questions, openai_key],
115
+ outputs=[filename_box, question_box, answer_box, highlighted_text, correct_answer, correct_reference],
116
  )
117
 
118
  btn_submit_correctness.click( # TODO
 
123
 
124
  # Switch question button
125
  btn_last_question.click(
126
+ backend.process_last,
127
+ outputs=[filename_box, question_box, answer_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
128
+ )
129
 
130
  btn_next_question.click(
131
  backend.process_next,
132
+ outputs=[filename_box, question_box, answer_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
133
  )
134
 
135
+ # Switch passwage button
136
+ btn_last_passage.click(
137
+ backend.switch_last_passage,
138
+ outputs=[filename_box, question_box, answer_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
139
+ )
140
+ btn_next_passage.click(
141
+ backend.switch_next_passage,
142
+ outputs=[filename_box, question_box, answer_box, highlighted_text, correct_answer, correct_reference, save_results, answer_correct, reference_correct],
143
+ )
144
+
145
  # Download button
146
  btn_download_answer.click(
147
  backend.download_answer,
backend.py CHANGED
@@ -12,7 +12,7 @@ class Backend:
12
  self.agent = OpenAI()
13
  self.prompt = Prompt()
14
 
15
- def read_file(self, file):
16
  # read the file
17
  if file is not None:
18
  with open(file.name, 'r') as f:
@@ -21,6 +21,19 @@ class Backend:
21
  raise gr.Error("You need to upload a file first")
22
  return text
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def highlight_text(self, text, highlight_list):
25
  # Find the original sentences
26
  # Split the passage into sentences
@@ -40,42 +53,61 @@ class Backend:
40
  text = text.replace('\n', f" <br /> ")
41
 
42
  # add scroll bar
43
- text = f'<div style="height: 500px; overflow: auto;">{text}</div>'
44
 
45
  return text
46
 
47
- def process_file(self, file, questions, openai_key):
48
  # record the questions
49
  self.questions = questions
50
 
51
- # get the text
52
- self.text = self.read_file(file)
53
 
54
  # make the prompt
55
- prompt = self.prompt.get(self.text, questions, 'v3')
56
 
57
  # interact with openai
58
- res = self.agent(prompt, with_history = False, temperature = 0.1, model = 'gpt-3.5-turbo-16k', api_key = openai_key)
59
- res = self.prompt.process_result(res, 'v3')
 
 
 
 
 
 
 
 
60
 
61
- # for multiple questions
62
- self.gpt_result = res
63
- self.curret_question = 0
64
  self.totel_question = len(res.keys())
 
 
65
 
66
  # make a dataframe to record everything
67
- self.ori_answer_df = pd.DataFrame(res).T
68
- self.answer_df = pd.DataFrame(res).T
 
 
 
 
 
 
 
 
69
 
70
  # default fist question
71
  res = res['Question 1']
72
- question = self.questions[self.curret_question]
73
  self.answer = res['answer']
 
74
  self.highlighted_out = res['original sentences']
75
  highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
76
  self.highlighted_out = '\n'.join(self.highlighted_out)
 
 
77
 
78
- return question, self.answer, highlighted_out_html, self.answer, self.highlighted_out
79
 
80
  def process_results(self, answer_correct, correct_answer, reference_correct, correct_reference):
81
  if not hasattr(self, 'clicked_correct_answer'):
@@ -87,75 +119,138 @@ class Backend:
87
  if not hasattr(self, 'answer_df'):
88
  raise gr.Error("You need to submit the document first")
89
 
90
- if self.curret_question >= self.totel_question or self.curret_question < 0:
91
  raise gr.Error("No more questions, please return back")
92
 
93
  # record the answer
94
- self.answer_df.loc[f'Question {self.curret_question + 1}', 'answer_correct'] = answer_correct
95
- self.answer_df.loc[f'Question {self.curret_question + 1}', 'reference_correct'] = reference_correct
 
 
 
 
 
96
 
97
  if self.clicked_correct_answer == True:
98
  if hasattr(self, 'answer'):
99
- self.answer_df.loc[f'Question {self.curret_question + 1}', 'correct_answer'] = self.answer
100
  else:
101
  raise gr.Error("You need to submit the document first")
102
  else:
103
- self.answer_df.loc[f'Question {self.curret_question + 1}', 'correct_answer'] = correct_answer
 
104
 
105
  if self.clicked_correct_reference == True:
106
  if hasattr(self, 'highlighted_out'):
107
- self.answer_df.loc[f'Question {self.curret_question + 1}', 'correct_reference'] = self.highlighted_out
108
  else:
109
  raise gr.Error("You need to submit the document first")
110
  else:
111
- self.answer_df.loc[f'Question {self.curret_question + 1}', 'correct_reference'] = correct_reference
112
 
113
  gr.Info('Results saved!')
114
  return "Results saved!"
115
 
116
  def process_next(self):
117
- self.curret_question += 1
118
  if hasattr(self, 'clicked_correct_answer'):
119
  del self.clicked_correct_answer
120
  if hasattr(self, 'clicked_correct_reference'):
121
  del self.clicked_correct_reference
122
 
123
- if self.curret_question >= self.totel_question:
124
- # self.curret_question -= 1
125
- return "No more questions!", "No more questions!", "No more questions!", 'No more questions!', 'No more questions!', 'Still need to click the button above to save the results', None, None
126
  else:
127
- res = self.gpt_result[f'Question {self.curret_question + 1}']
128
- question = self.questions[self.curret_question]
129
  self.answer = res['answer']
130
  self.highlighted_out = res['original sentences']
131
  highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
132
  self.highlighted_out = '\n'.join(self.highlighted_out)
133
- return question, self.answer, highlighted_out_html, 'Please judge on the generated answer', 'Please judge on the generated answer', 'Still need to click the button above to save the results', None, None
 
 
134
 
135
  def process_last(self):
136
- self.curret_question -= 1
137
 
 
138
  if hasattr(self, 'clicked_correct_answer'):
139
  del self.clicked_correct_answer
140
  if hasattr(self, 'clicked_correct_reference'):
141
  del self.clicked_correct_reference
142
 
143
- if self.curret_question < 0:
144
- # self.curret_question += 1
145
- return "No more questions!", "No more questions!", "No more questions!", 'No more questions!', 'No more questions!', 'Still need to click the button above to save the results', None, None
 
146
  else:
147
- res = self.gpt_result[f'Question {self.curret_question + 1}']
148
- question = self.questions[self.curret_question]
149
  self.answer = res['answer']
150
  self.highlighted_out = res['original sentences']
151
  highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
152
  self.highlighted_out = '\n'.join(self.highlighted_out)
153
- return question, self.answer, highlighted_out_html, 'Please judge on the generated answer', 'Please judge on the generated answer', 'Still need to click the button above to save the results', None, None
154
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  def download_answer(self, path = './tmp', name = 'answer.xlsx'):
156
  os.makedirs(path, exist_ok = True)
157
  path = os.path.join(path, name)
158
- self.ori_answer_df['questions'] = self.questions
159
  self.ori_answer_df.to_excel(path, index = False)
160
 
161
  return path
@@ -163,7 +258,7 @@ class Backend:
163
  def download_corrected(self, path = './tmp', name = 'corrected_answer.xlsx'):
164
  os.makedirs(path, exist_ok = True)
165
  path = os.path.join(path, name)
166
- self.answer_df['questions'] = self.questions
167
  self.answer_df.to_excel(path, index = False)
168
 
169
  return path
 
12
  self.agent = OpenAI()
13
  self.prompt = Prompt()
14
 
15
+ def read_file_single(self, file):
16
  # read the file
17
  if file is not None:
18
  with open(file.name, 'r') as f:
 
21
  raise gr.Error("You need to upload a file first")
22
  return text
23
 
24
+ def read_file(self, files):
25
+ # read the file
26
+ text_list = []
27
+ self.filename_list = []
28
+ if files is not None:
29
+ for file in files:
30
+ with open(file.name, 'r', encoding='utf-8') as f:
31
+ text_list.append(f.read())
32
+ self.filename_list.append(file.name.split('\\')[-1])
33
+ else:
34
+ raise gr.Error("You need to upload a file first")
35
+ return text_list
36
+
37
  def highlight_text(self, text, highlight_list):
38
  # Find the original sentences
39
  # Split the passage into sentences
 
53
  text = text.replace('\n', f" <br /> ")
54
 
55
  # add scroll bar
56
+ text = f'<div style="height: 300px; overflow: auto;">{text}</div>'
57
 
58
  return text
59
 
60
+ def process_file(self, file, questions, openai_key, progress = gr.Progress()):
61
  # record the questions
62
  self.questions = questions
63
 
64
+ # get the text_list
65
+ self.text_list = self.read_file(file)
66
 
67
  # make the prompt
68
+ prompt_list = [self.prompt.get(text, questions, 'v3') for text in self.text_list]
69
 
70
  # interact with openai
71
+ self.res_list = []
72
+ for prompt in progress.tqdm(prompt_list, desc = 'Generating answers...'):
73
+ res = self.agent(prompt, with_history = False, temperature = 0.1, model = 'gpt-3.5-turbo-16k', api_key = openai_key)
74
+ res = self.prompt.process_result(res, 'v3')
75
+ self.res_list.append(res)
76
+
77
+ # Use the first file as default
78
+ # Use the first question for multiple questions
79
+ gpt_res = self.res_list[0]
80
+ self.gpt_result = gpt_res
81
 
82
+ self.current_question = 0
 
 
83
  self.totel_question = len(res.keys())
84
+ self.current_passage = 0
85
+ self.total_passages = len(self.res_list)
86
 
87
  # make a dataframe to record everything
88
+ self.ori_answer_df = pd.DataFrame()
89
+ self.answer_df = pd.DataFrame()
90
+ for i, res in enumerate(self.res_list):
91
+ tmp = pd.DataFrame(res).T
92
+ tmp = tmp.reset_index()
93
+ tmp = tmp.rename(columns={"index":"question_id"})
94
+ tmp['filename'] = self.filename_list[i]
95
+ tmp['question'] = self.questions
96
+ self.ori_answer_df = pd.concat([tmp, self.ori_answer_df])
97
+ self.answer_df = pd.concat([tmp, self.answer_df])
98
 
99
  # default fist question
100
  res = res['Question 1']
101
+ question = self.questions[self.current_question]
102
  self.answer = res['answer']
103
+ self.text = self.text_list[0]
104
  self.highlighted_out = res['original sentences']
105
  highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
106
  self.highlighted_out = '\n'.join(self.highlighted_out)
107
+
108
+ file_name = self.filename_list[self.current_passage]
109
 
110
+ return file_name, question, self.answer, highlighted_out_html, self.answer, self.highlighted_out
111
 
112
  def process_results(self, answer_correct, correct_answer, reference_correct, correct_reference):
113
  if not hasattr(self, 'clicked_correct_answer'):
 
119
  if not hasattr(self, 'answer_df'):
120
  raise gr.Error("You need to submit the document first")
121
 
122
+ if self.current_question >= self.totel_question or self.current_question < 0:
123
  raise gr.Error("No more questions, please return back")
124
 
125
  # record the answer
126
+ condition = (self.answer_df['question_id'] == f'Question {self.current_question + 1}' ) & \
127
+ (self.answer_df['filename'] == self.filename_list[self.current_passage])
128
+ self.answer_df.loc[condition, 'answer_correct'] = answer_correct
129
+ self.answer_df.loc[condition, 'reference_correct'] = reference_correct
130
+
131
+ # self.answer_df.loc[f'Question {self.current_question + 1}', 'answer_correct'] = answer_correct
132
+ # self.answer_df.loc[f'Question {self.current_question + 1}', 'reference_correct'] = reference_correct
133
 
134
  if self.clicked_correct_answer == True:
135
  if hasattr(self, 'answer'):
136
+ self.answer_df.loc[condition, 'correct_answer'] = self.answer
137
  else:
138
  raise gr.Error("You need to submit the document first")
139
  else:
140
+ # self.answer_df.loc[f'Question {self.current_question + 1}', 'correct_answer'] = correct_answer
141
+ self.answer_df.loc[condition, 'correct_answer'] = correct_answer
142
 
143
  if self.clicked_correct_reference == True:
144
  if hasattr(self, 'highlighted_out'):
145
+ self.answer_df.loc[condition, 'correct_reference'] = self.highlighted_out
146
  else:
147
  raise gr.Error("You need to submit the document first")
148
  else:
149
+ self.answer_df.loc[condition, 'correct_reference'] = correct_reference
150
 
151
  gr.Info('Results saved!')
152
  return "Results saved!"
153
 
154
  def process_next(self):
155
+ self.current_question += 1
156
  if hasattr(self, 'clicked_correct_answer'):
157
  del self.clicked_correct_answer
158
  if hasattr(self, 'clicked_correct_reference'):
159
  del self.clicked_correct_reference
160
 
161
+ if self.current_question >= self.totel_question:
162
+ # self.current_question -= 1
163
+ return "No more questions!", "No more questions!", "No more questions!", "No more questions!", 'No more questions!', 'No more questions!', 'Still need to click the button above to save the results', None, None
164
  else:
165
+ res = self.gpt_result[f'Question {self.current_question + 1}']
166
+ question = self.questions[self.current_question]
167
  self.answer = res['answer']
168
  self.highlighted_out = res['original sentences']
169
  highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
170
  self.highlighted_out = '\n'.join(self.highlighted_out)
171
+ file_name = self.filename_list[self.current_passage]
172
+
173
+ return file_name, question, self.answer, highlighted_out_html, 'Please judge on the generated answer', 'Please judge on the generated answer', 'Still need to click the button above to save the results', None, None
174
 
175
  def process_last(self):
176
+ self.current_question -= 1
177
 
178
+ # To make sure to correct the answer first
179
  if hasattr(self, 'clicked_correct_answer'):
180
  del self.clicked_correct_answer
181
  if hasattr(self, 'clicked_correct_reference'):
182
  del self.clicked_correct_reference
183
 
184
+ # check question boundary
185
+ if self.current_question < 0:
186
+ # self.current_question += 1
187
+ return "No more questions!", "No more questions!", "No more questions!", "No more questions!", 'No more questions!', 'No more questions!', 'Still need to click the button above to save the results', None, None
188
  else:
189
+ res = self.gpt_result[f'Question {self.current_question + 1}']
190
+ question = self.questions[self.current_question]
191
  self.answer = res['answer']
192
  self.highlighted_out = res['original sentences']
193
  highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
194
  self.highlighted_out = '\n'.join(self.highlighted_out)
195
+ file_name = self.filename_list[self.current_passage]
196
+ return file_name, question, self.answer, highlighted_out_html, 'Please judge on the generated answer', 'Please judge on the generated answer', 'Still need to click the button above to save the results', None, None
197
+
198
+ def switch_next_passage(self):
199
+ self.current_question = 0
200
+
201
+ # To make sure to correct the answer first
202
+ if hasattr(self, 'clicked_correct_answer'):
203
+ del self.clicked_correct_answer
204
+ if hasattr(self, 'clicked_correct_reference'):
205
+ del self.clicked_correct_reference
206
+
207
+ self.current_passage += 1
208
+
209
+ if self.current_passage >= self.total_passages:
210
+ # self.current_passage -= 1
211
+ return "No more passages!", "No more passages!", "No more passages!", "No more passages!", 'No more passages!', 'No more passages!', 'Still need to click the button above to save the results', None, None
212
+ else:
213
+ gpt_res = self.res_list[self.current_passage]
214
+ self.gpt_result = gpt_res
215
+ res = self.gpt_result[f'Question {self.current_question + 1}']
216
+ question = self.questions[self.current_question]
217
+ self.answer = res['answer']
218
+ self.highlighted_out = res['original sentences']
219
+ highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
220
+ self.highlighted_out = '\n'.join(self.highlighted_out)
221
+ file_name = self.filename_list[self.current_passage]
222
+ return file_name, question, self.answer, highlighted_out_html, 'Please judge on the generated answer', 'Please judge on the generated answer', 'Still need to click the button above to save the results', None, None
223
+
224
+ def switch_last_passage(self):
225
+ self.current_question = 0
226
+
227
+ # To make sure to correct the answer first
228
+ if hasattr(self, 'clicked_correct_answer'):
229
+ del self.clicked_correct_answer
230
+ if hasattr(self, 'clicked_correct_reference'):
231
+ del self.clicked_correct_reference
232
+
233
+ self.current_passage -= 1
234
+
235
+ if self.current_passage < 0:
236
+ # self.current_passage += 1
237
+ return "No more passages!", "No more passages!", "No more passages!", "No more passages!", 'No more passages!', 'No more passages!', 'Still need to click the button above to save the results', None, None
238
+ else:
239
+ gpt_res = self.res_list[self.current_passage]
240
+ self.gpt_result = gpt_res
241
+ res = self.gpt_result[f'Question {self.current_question + 1}']
242
+ question = self.questions[self.current_question]
243
+ self.answer = res['answer']
244
+ self.highlighted_out = res['original sentences']
245
+ highlighted_out_html = self.highlight_text(self.text, self.highlighted_out)
246
+ self.highlighted_out = '\n'.join(self.highlighted_out)
247
+ file_name = self.filename_list[self.current_passage]
248
+ return file_name, question, self.answer, highlighted_out_html, 'Please judge on the generated answer', 'Please judge on the generated answer', 'Still need to click the button above to save the results', None, None
249
+
250
  def download_answer(self, path = './tmp', name = 'answer.xlsx'):
251
  os.makedirs(path, exist_ok = True)
252
  path = os.path.join(path, name)
253
+ # self.ori_answer_df['questions'] = self.questions
254
  self.ori_answer_df.to_excel(path, index = False)
255
 
256
  return path
 
258
  def download_corrected(self, path = './tmp', name = 'corrected_answer.xlsx'):
259
  os.makedirs(path, exist_ok = True)
260
  path = os.path.join(path, name)
261
+ # self.answer_df['questions'] = self.questions
262
  self.answer_df.to_excel(path, index = False)
263
 
264
  return path
openai.py CHANGED
@@ -32,7 +32,14 @@ class OpenAI:
32
  }, headers={
33
  'Authorization': f"Bearer {api_key}"
34
  })
35
- # print(resp.json())
36
  self.history.append(resp.json()['choices'][0]['message'])
 
37
 
38
- return resp.json()['choices'][0]['message']['content']
 
 
 
 
 
 
 
32
  }, headers={
33
  'Authorization': f"Bearer {api_key}"
34
  })
35
+ print(resp)
36
  self.history.append(resp.json()['choices'][0]['message'])
37
+ res = resp.json()['choices'][0]['message']['content']
38
 
39
+ # with open("tmp_res.txt", 'w') as f:
40
+ # f.write(res)
41
+
42
+ # with open("tmp_res.txt", 'r') as f:
43
+ # res = f.read()
44
+
45
+ return res