Aakash Goel commited on
Commit
ab0470d
·
1 Parent(s): a6a36c6

log code change directory 9

Browse files
Files changed (1) hide show
  1. code/qa.py +49 -51
code/qa.py CHANGED
@@ -3,6 +3,11 @@ from transformers import pipeline
3
  import pandas as pd
4
  import datetime
5
 
 
 
 
 
 
6
  def load_file():
7
  """Load text from file"""
8
  uploaded_file = st.file_uploader("Upload Files",type=['txt'])
@@ -12,62 +17,55 @@ def load_file():
12
  raw_text = str(uploaded_file.read(),"utf-8")
13
  return raw_text
14
 
 
 
15
 
16
- if __name__ == "__main__":
17
-
18
- # App title and description
19
- st.title("Answering questions from text")
20
- st.write("Upload text, pose questions, get answers")
21
 
22
- # Load file
23
- raw_text = load_file()
24
- if raw_text != None and raw_text != '':
25
 
26
- # Display text
27
- with st.expander("See text"):
28
- st.write(raw_text)
29
 
30
- # Perform question answering
31
- question_answerer = pipeline('question-answering')
32
 
33
- answer = ''
34
- question = st.text_input('Ask a question')
35
 
36
- if question != '' and raw_text != '':
37
- answer = question_answerer({
38
- 'question': question,
39
- 'context': raw_text
40
- })
41
 
42
- st.write("Answer: {}".format(answer["answer"]))
43
- st.write("Confidence score: {}".format(round(answer["score"],2)))
44
- import os
45
- # st.write(os.getcwd())
46
- # st.write(os.listdir('.'))
47
- # st.write(os.listdir('../'))
48
- # log_file_object = open('../logs/log_file.tsv','a')
49
- # log_file_object.write(str(datetime.datetime.now())+'\t'+str(raw_text)+'\t'+str(answer["answer"])+'\t'+str(answer["score"]))
50
- # log_file_object.write('\n')
51
- # log_file_object.close()
52
- output_path = "results/df_log_file.tsv"
53
- try:
54
- res_df = pd.DataFrame({"TimeStamp":[str(datetime.datetime.now())],\
55
- "Input":[str(raw_text)],"Answer":[str(answer["answer"])],"Score":[str(answer["score"])]})
56
- st.write("PASS1")
57
- except:
58
- st.write("FAIL1")
59
- try:
60
- res_df.to_csv(output_path, mode='a', index=False, sep="\t", header= not os.path.exists(output_path))
61
- st.write("PASS2")
62
- except:
63
- st.write("FAIL2")
64
- import os
65
- #st.write(os.listdir("logs"))
66
- for i in os.listdir("."):
67
- try:
68
- st.write(i,os.listdir(i))
69
- st.write("="*50)
70
- except:
71
- pass
72
- st.write(pd.read_csv("logs/df_log_file.tsv",sep="\t").head(10))
73
 
 
3
  import pandas as pd
4
  import datetime
5
 
6
+ @st.cache(allow_output_mutation = True)
7
+ def load_model():
8
+ question_answerer = pipeline('question-answering')
9
+ return question_answerer
10
+
11
  def load_file():
12
  """Load text from file"""
13
  uploaded_file = st.file_uploader("Upload Files",type=['txt'])
 
17
  raw_text = str(uploaded_file.read(),"utf-8")
18
  return raw_text
19
 
20
+ # Loading Model
21
+ question_answerer =load_model()
22
 
23
+ # App title and description
24
+ st.title("Answering questions from text")
25
+ st.write("Upload text, pose questions, get answers")
 
 
26
 
27
+ # Load file
28
+ raw_text = load_file()
29
+ if raw_text != None and raw_text != '':
30
 
31
+ # Display text
32
+ with st.expander("See text"):
33
+ st.write(raw_text)
34
 
35
+ # Perform question answering
36
+ # question_answerer = pipeline('question-answering')
37
 
38
+ answer = ''
39
+ question = st.text_input('Ask a question')
40
 
41
+ if question != '' and raw_text != '':
42
+ answer = question_answerer({
43
+ 'question': question,
44
+ 'context': raw_text
45
+ })
46
 
47
+ st.write("Answer: {}".format(answer["answer"]))
48
+ st.write("Confidence score: {}".format(round(answer["score"],2)))
49
+ import os
50
+ # st.write(os.getcwd())
51
+ # st.write(os.listdir('.'))
52
+ # st.write(os.listdir('../'))
53
+ # log_file_object = open('../logs/log_file.tsv','a')
54
+ # log_file_object.write(str(datetime.datetime.now())+'\t'+str(raw_text)+'\t'+str(answer["answer"])+'\t'+str(answer["score"]))
55
+ # log_file_object.write('\n')
56
+ # log_file_object.close()
57
+ output_path = "results/df_log_file.tsv"
58
+ try:
59
+ res_df = pd.DataFrame({"TimeStamp":[str(datetime.datetime.now())],\
60
+ "Input":[str(raw_text)],"Answer":[str(answer["answer"])],"Score":[str(answer["score"])]})
61
+ st.write("PASS1")
62
+ except:
63
+ st.write("FAIL1")
64
+ try:
65
+ res_df.to_csv(output_path, mode='a', index=False, sep="\t", header= not os.path.exists(output_path))
66
+ st.write("PASS2")
67
+ except:
68
+ st.write("FAIL2")
69
+ import os
70
+ st.dataframe(pd.read_csv("results/df_log_file.tsv",sep="\t").head(10))
 
 
 
 
 
 
 
71