Spaces:
Runtime error
Runtime error
Aakash Goel
commited on
Commit
·
ab0470d
1
Parent(s):
a6a36c6
log code change directory 9
Browse files- 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 |
-
|
17 |
-
|
18 |
-
|
19 |
-
st.title("Answering questions from text")
|
20 |
-
st.write("Upload text, pose questions, get answers")
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
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 |
|