Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -84,15 +84,23 @@ def encode_docs(docs,maxlen = 64, stride = 32):
|
|
84 |
|
85 |
def predict(query,data):
|
86 |
name_to_save = data.name.split("/")[-1].split(".")[0][:-8]
|
87 |
-
|
88 |
st = str([query,name_to_save])
|
89 |
st_hashed = str(hashlib.sha256(st.encode()).hexdigest()) #just to speed up examples load
|
90 |
hist = st + " " + st_hashed
|
91 |
now = datetime.now()
|
92 |
current_time = now.strftime("%H:%M:%S")
|
93 |
try:
|
94 |
-
df = pd.read_csv("{}.csv".format(
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
except Exception as e:
|
97 |
print(e)
|
98 |
print(st)
|
@@ -138,24 +146,14 @@ def predict(query,data):
|
|
138 |
probabilities = "P(a|p): {}, P(a|p,q): {}, P(p|q): {}".format(round(ans["score"],5),
|
139 |
round(ans["score"]*probs[i],5),
|
140 |
round(probs[i],5))
|
141 |
-
passage = passage.replace(str(ans["answer"]),str(ans["answer"]).upper())
|
142 |
table["Passage"].append(passage)
|
143 |
-
table["Passage"].append("---")
|
144 |
table["Answer"].append(str(ans["answer"]).upper())
|
145 |
-
table["Answer"].append("---")
|
146 |
table["Probabilities"].append(probabilities)
|
147 |
-
table["Probabilities"].append("---")
|
148 |
-
table["Source"].append(names)
|
149 |
-
table["Source"].append("---")
|
150 |
else:
|
151 |
table["Passage"].append(passage)
|
152 |
-
table["Passage"].append("---")
|
153 |
table["Answer"].append("no_answer_calculated")
|
154 |
-
table["Answer"].append("---")
|
155 |
table["Probabilities"].append("P(p|q): {}".format(round(probs[i],5)))
|
156 |
-
|
157 |
-
table["Source"].append(names)
|
158 |
-
table["Source"].append("---")
|
159 |
df = pd.DataFrame(table)
|
160 |
print("time: "+ str(time.time()-start))
|
161 |
|
@@ -164,7 +162,17 @@ def predict(query,data):
|
|
164 |
f.write(" " + str(current_time))
|
165 |
f.write("\n")
|
166 |
f.close()
|
167 |
-
df.to_csv("{}.csv".format(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
return df
|
170 |
|
@@ -178,7 +186,7 @@ iface = gr.Interface(examples = [
|
|
178 |
gr.inputs.File(),
|
179 |
],
|
180 |
outputs = [
|
181 |
-
gr.outputs.
|
182 |
],
|
183 |
description=description,
|
184 |
title = title,
|
|
|
84 |
|
85 |
def predict(query,data):
|
86 |
name_to_save = data.name.split("/")[-1].split(".")[0][:-8]
|
87 |
+
k=20
|
88 |
st = str([query,name_to_save])
|
89 |
st_hashed = str(hashlib.sha256(st.encode()).hexdigest()) #just to speed up examples load
|
90 |
hist = st + " " + st_hashed
|
91 |
now = datetime.now()
|
92 |
current_time = now.strftime("%H:%M:%S")
|
93 |
try:
|
94 |
+
df = pd.read_csv("HISTORY/{}.csv".format(hash(st)))
|
95 |
+
list_outputs = []
|
96 |
+
for i in range(k):
|
97 |
+
temp = [df.iloc[n] for n in range(k)][i]
|
98 |
+
text = ''
|
99 |
+
text += 'PROBABILITIES: '+ temp.Probabilities + '\n\n'
|
100 |
+
text += 'ANSWER: ' +temp.Answer + '\n\n'
|
101 |
+
text += 'PASSAGE: '+temp.Passage + '\n\n'
|
102 |
+
list_outputs.append(text)
|
103 |
+
return list_outputs
|
104 |
except Exception as e:
|
105 |
print(e)
|
106 |
print(st)
|
|
|
146 |
probabilities = "P(a|p): {}, P(a|p,q): {}, P(p|q): {}".format(round(ans["score"],5),
|
147 |
round(ans["score"]*probs[i],5),
|
148 |
round(probs[i],5))
|
|
|
149 |
table["Passage"].append(passage)
|
|
|
150 |
table["Answer"].append(str(ans["answer"]).upper())
|
|
|
151 |
table["Probabilities"].append(probabilities)
|
|
|
|
|
|
|
152 |
else:
|
153 |
table["Passage"].append(passage)
|
|
|
154 |
table["Answer"].append("no_answer_calculated")
|
|
|
155 |
table["Probabilities"].append("P(p|q): {}".format(round(probs[i],5)))
|
156 |
+
|
|
|
|
|
157 |
df = pd.DataFrame(table)
|
158 |
print("time: "+ str(time.time()-start))
|
159 |
|
|
|
162 |
f.write(" " + str(current_time))
|
163 |
f.write("\n")
|
164 |
f.close()
|
165 |
+
df.to_csv("HISTORY/{}.csv".format(hash(st)), index=False)
|
166 |
+
|
167 |
+
list_outputs = []
|
168 |
+
for i in range(k):
|
169 |
+
text = ''
|
170 |
+
temp = [df.iloc[n] for n in range(k)][i]
|
171 |
+
text += 'PROBABILITIES: '+ temp.Probabilities + '\n\n'
|
172 |
+
text += 'ANSWER: ' +temp.Answer + '\n\n'
|
173 |
+
text += 'PASSAGE: '+temp.Passage + '\n\n'
|
174 |
+
|
175 |
+
list_outputs.append(text)
|
176 |
|
177 |
return df
|
178 |
|
|
|
186 |
gr.inputs.File(),
|
187 |
],
|
188 |
outputs = [
|
189 |
+
gr.outputs.Carousel(['text']),
|
190 |
],
|
191 |
description=description,
|
192 |
title = title,
|