Spaces:
Runtime error
Runtime error
nostromo
#2
by
fragom
- opened
- app.py +12 -19
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -1,18 +1,12 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Gradio requires input to be fed in a very peculiar way and does not provide too much flexibility - don't expect from this demo too much. The backbone had to be adjusted to work on hugging face spaces. Go see https://github.com/PiotrAntoniak/QuestionAnswering for a prettier version utilizing streamlit.
|
| 3 |
-
"""
|
| 4 |
-
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
description = """Do you have a long document and a bunch of questions that can be answered given the data in this file?
|
| 9 |
Fear not for this demo is for you.
|
| 10 |
Upload your pdf, ask your questions and wait for the magic to happen.
|
| 11 |
DISCLAIMER: I do no have idea what happens to the pdfs that you upload and who has access to them so make sure there is nothing confidential there.
|
| 12 |
-
"""
|
| 13 |
title = "QA answering from a pdf."
|
| 14 |
|
| 15 |
-
from datetime import datetime
|
| 16 |
import numpy as np
|
| 17 |
import time
|
| 18 |
import hashlib
|
|
@@ -57,7 +51,7 @@ def encode_docs(docs,maxlen = 64, stride = 32):
|
|
| 57 |
spans = []
|
| 58 |
file_names = []
|
| 59 |
name, text = docs
|
| 60 |
-
|
| 61 |
text = text.split(" ")
|
| 62 |
if len(text) < maxlen:
|
| 63 |
text = " ".join(text)
|
|
@@ -92,7 +86,6 @@ def encode_docs(docs,maxlen = 64, stride = 32):
|
|
| 92 |
return embeddings, spans, file_names
|
| 93 |
|
| 94 |
def predict(query,data):
|
| 95 |
-
print(datetime.today().strftime('%Y-%m-%d %H:%M:%S'))
|
| 96 |
name_to_save = data.name.split("/")[-1].split(".")[0][:-8]
|
| 97 |
k=20
|
| 98 |
st = str([query,name_to_save])
|
|
@@ -175,7 +168,6 @@ def predict(query,data):
|
|
| 175 |
print(df)
|
| 176 |
print("time: "+ str(time.time()-start))
|
| 177 |
|
| 178 |
-
|
| 179 |
with open("HISTORY.txt","a", encoding = "utf-8") as f:
|
| 180 |
f.write(hist)
|
| 181 |
f.write(" " + str(current_time))
|
|
@@ -195,20 +187,21 @@ def predict(query,data):
|
|
| 195 |
|
| 196 |
return list_outputs
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
iface = gr.Interface(examples = [
|
| 201 |
["How high is the highest mountain?","China.pdf"],
|
| 202 |
-
["Where
|
| 203 |
],
|
| 204 |
|
| 205 |
fn =predict,
|
| 206 |
-
inputs = [gr.Textbox(),
|
| 207 |
-
gr.File(),
|
| 208 |
],
|
| 209 |
-
outputs =
|
|
|
|
|
|
|
| 210 |
description=description,
|
| 211 |
-
title = title
|
| 212 |
-
|
|
|
|
| 213 |
|
| 214 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
description = """Do you have a long document and a bunch of questions that can be answered given the data in this file?
|
| 4 |
Fear not for this demo is for you.
|
| 5 |
Upload your pdf, ask your questions and wait for the magic to happen.
|
| 6 |
DISCLAIMER: I do no have idea what happens to the pdfs that you upload and who has access to them so make sure there is nothing confidential there.
|
| 7 |
+
"""
|
| 8 |
title = "QA answering from a pdf."
|
| 9 |
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
import time
|
| 12 |
import hashlib
|
|
|
|
| 51 |
spans = []
|
| 52 |
file_names = []
|
| 53 |
name, text = docs
|
| 54 |
+
|
| 55 |
text = text.split(" ")
|
| 56 |
if len(text) < maxlen:
|
| 57 |
text = " ".join(text)
|
|
|
|
| 86 |
return embeddings, spans, file_names
|
| 87 |
|
| 88 |
def predict(query,data):
|
|
|
|
| 89 |
name_to_save = data.name.split("/")[-1].split(".")[0][:-8]
|
| 90 |
k=20
|
| 91 |
st = str([query,name_to_save])
|
|
|
|
| 168 |
print(df)
|
| 169 |
print("time: "+ str(time.time()-start))
|
| 170 |
|
|
|
|
| 171 |
with open("HISTORY.txt","a", encoding = "utf-8") as f:
|
| 172 |
f.write(hist)
|
| 173 |
f.write(" " + str(current_time))
|
|
|
|
| 187 |
|
| 188 |
return list_outputs
|
| 189 |
|
|
|
|
|
|
|
| 190 |
iface = gr.Interface(examples = [
|
| 191 |
["How high is the highest mountain?","China.pdf"],
|
| 192 |
+
["Where does UK prime minister live?","London.pdf"]
|
| 193 |
],
|
| 194 |
|
| 195 |
fn =predict,
|
| 196 |
+
inputs = [gr.inputs.Textbox(default="What is Open-domain question answering?"),
|
| 197 |
+
gr.inputs.File(),
|
| 198 |
],
|
| 199 |
+
outputs = [
|
| 200 |
+
gr.outputs.Carousel(['text']),
|
| 201 |
+
],
|
| 202 |
description=description,
|
| 203 |
+
title = title,
|
| 204 |
+
allow_flagging ="manual",flagging_options = ["correct","wrong"],
|
| 205 |
+
allow_screenshot=False)
|
| 206 |
|
| 207 |
+
iface.launch(share = True,enable_queue=True, show_error =True)
|
requirements.txt
CHANGED
|
@@ -3,5 +3,4 @@ textract
|
|
| 3 |
scipy
|
| 4 |
pandas
|
| 5 |
numpy
|
| 6 |
-
transformers
|
| 7 |
-
gradio==3.0.20
|
|
|
|
| 3 |
scipy
|
| 4 |
pandas
|
| 5 |
numpy
|
| 6 |
+
transformers
|
|
|