Update app.py
Browse files
app.py
CHANGED
|
@@ -18,7 +18,7 @@ css_style = """
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
|
| 21 |
-
def request_pathname(files, data
|
| 22 |
if files is None:
|
| 23 |
return [[]]
|
| 24 |
for file in files:
|
|
@@ -26,19 +26,15 @@ def request_pathname(files, data, openai_api_key):
|
|
| 26 |
if file.name in [x[0] for x in data]:
|
| 27 |
continue
|
| 28 |
data.append([file.name, None, None])
|
| 29 |
-
return [[len(data), 0]], data, data
|
| 30 |
|
| 31 |
|
| 32 |
-
def validate_dataset(dataset
|
| 33 |
docs_ready = dataset.iloc[-1, 0] != ""
|
| 34 |
-
if docs_ready
|
| 35 |
return "✨Ready✨"
|
| 36 |
-
elif docs_ready:
|
| 37 |
-
return "⚠️Waiting for key⚠️"
|
| 38 |
-
elif type(openapi) is str and len(openapi) > 0:
|
| 39 |
-
return "⚠️Waiting for documents⚠️"
|
| 40 |
else:
|
| 41 |
-
return "⚠️Waiting for documents
|
| 42 |
|
| 43 |
|
| 44 |
def make_stats(docs):
|
|
@@ -46,11 +42,10 @@ def make_stats(docs):
|
|
| 46 |
|
| 47 |
|
| 48 |
# , progress=gr.Progress()):
|
| 49 |
-
def do_ask(question, button,
|
| 50 |
passages = ""
|
| 51 |
docs_ready = dataset.iloc[-1, 0] != ""
|
| 52 |
-
if button == "✨Ready✨" and
|
| 53 |
-
os.environ['OPENAI_API_KEY'] = openapi.strip()
|
| 54 |
if docs is None:
|
| 55 |
docs = paperqa.Docs()
|
| 56 |
# dataset is pandas dataframe
|
|
@@ -79,7 +74,7 @@ def do_ask(question, button, openapi, dataset, length, do_marg, k, max_sources,
|
|
| 79 |
yield result.formatted_answer, result.context, passages, docs, make_stats(docs)
|
| 80 |
|
| 81 |
|
| 82 |
-
def download_repo(gh_repo, data,
|
| 83 |
# download zipped version of repo
|
| 84 |
r = requests.get(f'https://api.github.com/repos/{gh_repo}/zipball')
|
| 85 |
if r.status_code == 200:
|
|
@@ -111,7 +106,7 @@ def download_repo(gh_repo, data, openai_api_key, pbar=gr.Progress()):
|
|
| 111 |
if path in [x[0] for x in data]:
|
| 112 |
continue
|
| 113 |
data.append([path, citation, key])
|
| 114 |
-
yield [[len(data), 0]], data, data
|
| 115 |
pbar(int((i+1)/len(z.namelist()) * 99),
|
| 116 |
f'Added {f}')
|
| 117 |
pbar(100, 'Done')
|
|
@@ -124,7 +119,6 @@ with gr.Blocks(css=css_style) as demo:
|
|
| 124 |
|
| 125 |
docs = gr.State(None)
|
| 126 |
data = gr.State([])
|
| 127 |
-
openai_api_key = gr.State('')
|
| 128 |
|
| 129 |
gr.Markdown(f"""
|
| 130 |
# Document Question and Answer (v{paperqa.__version__})
|
|
@@ -140,12 +134,9 @@ with gr.Blocks(css=css_style) as demo:
|
|
| 140 |
* [PaperQA](https://github.com/whitead/paper-qa) is the code used to build this tool.
|
| 141 |
* [langchain](https://github.com/hwchase17/langchain) is the main library this tool utilizes.
|
| 142 |
|
| 143 |
-
1.
|
| 144 |
-
2.
|
| 145 |
-
3. Ask a questions
|
| 146 |
""")
|
| 147 |
-
openai_api_key = gr.Textbox(
|
| 148 |
-
label="OpenAI API Key", placeholder="sk-...", type="password")
|
| 149 |
with gr.Tab('File Upload'):
|
| 150 |
uploaded_files = gr.File(
|
| 151 |
label="Your Documents Upload (PDF or txt)", file_count="multiple", )
|
|
@@ -164,7 +155,7 @@ with gr.Blocks(css=css_style) as demo:
|
|
| 164 |
overflow_row_behaviour='paginate',
|
| 165 |
max_rows=5
|
| 166 |
)
|
| 167 |
-
buildb = gr.Textbox("⚠️Waiting for documents
|
| 168 |
label="Status", interactive=False, show_label=True,
|
| 169 |
max_lines=1)
|
| 170 |
stats = gr.Dataframe(headers=['Docs', 'Chunks'],
|
|
@@ -172,14 +163,11 @@ with gr.Blocks(css=css_style) as demo:
|
|
| 172 |
col_count=(2, "fixed"),
|
| 173 |
interactive=False,
|
| 174 |
label="Doc Stats")
|
| 175 |
-
|
| 176 |
-
dataset, openai_api_key], outputs=[buildb])
|
| 177 |
-
dataset.change(validate_dataset, inputs=[
|
| 178 |
-
dataset, openai_api_key], outputs=[buildb])
|
| 179 |
uploaded_files.change(request_pathname, inputs=[
|
| 180 |
-
uploaded_files, data
|
| 181 |
download.click(fn=download_repo, inputs=[
|
| 182 |
-
gh_repo, data
|
| 183 |
query = gr.Textbox(
|
| 184 |
placeholder="Enter your question here...", label="Question")
|
| 185 |
with gr.Row():
|
|
@@ -198,8 +186,8 @@ with gr.Blocks(css=css_style) as demo:
|
|
| 198 |
|
| 199 |
with gr.Accordion("Raw Text", open=False):
|
| 200 |
passages = gr.Markdown(label="Passages")
|
| 201 |
-
ask.click(fn=do_ask, inputs=[query,
|
| 202 |
-
|
| 203 |
length, marg, k, sources,
|
| 204 |
docs], outputs=[answer, context, passages, docs, stats])
|
| 205 |
|
|
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
|
| 21 |
+
def request_pathname(files, data):
|
| 22 |
if files is None:
|
| 23 |
return [[]]
|
| 24 |
for file in files:
|
|
|
|
| 26 |
if file.name in [x[0] for x in data]:
|
| 27 |
continue
|
| 28 |
data.append([file.name, None, None])
|
| 29 |
+
return [[len(data), 0]], data, data
|
| 30 |
|
| 31 |
|
| 32 |
+
def validate_dataset(dataset):
|
| 33 |
docs_ready = dataset.iloc[-1, 0] != ""
|
| 34 |
+
if docs_ready:
|
| 35 |
return "✨Ready✨"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
else:
|
| 37 |
+
return "⚠️Waiting for documents⚠️"
|
| 38 |
|
| 39 |
|
| 40 |
def make_stats(docs):
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
# , progress=gr.Progress()):
|
| 45 |
+
def do_ask(question, button, dataset, length, do_marg, k, max_sources, docs):
|
| 46 |
passages = ""
|
| 47 |
docs_ready = dataset.iloc[-1, 0] != ""
|
| 48 |
+
if button == "✨Ready✨" and docs_ready:
|
|
|
|
| 49 |
if docs is None:
|
| 50 |
docs = paperqa.Docs()
|
| 51 |
# dataset is pandas dataframe
|
|
|
|
| 74 |
yield result.formatted_answer, result.context, passages, docs, make_stats(docs)
|
| 75 |
|
| 76 |
|
| 77 |
+
def download_repo(gh_repo, data, pbar=gr.Progress()):
|
| 78 |
# download zipped version of repo
|
| 79 |
r = requests.get(f'https://api.github.com/repos/{gh_repo}/zipball')
|
| 80 |
if r.status_code == 200:
|
|
|
|
| 106 |
if path in [x[0] for x in data]:
|
| 107 |
continue
|
| 108 |
data.append([path, citation, key])
|
| 109 |
+
yield [[len(data), 0]], data, data
|
| 110 |
pbar(int((i+1)/len(z.namelist()) * 99),
|
| 111 |
f'Added {f}')
|
| 112 |
pbar(100, 'Done')
|
|
|
|
| 119 |
|
| 120 |
docs = gr.State(None)
|
| 121 |
data = gr.State([])
|
|
|
|
| 122 |
|
| 123 |
gr.Markdown(f"""
|
| 124 |
# Document Question and Answer (v{paperqa.__version__})
|
|
|
|
| 134 |
* [PaperQA](https://github.com/whitead/paper-qa) is the code used to build this tool.
|
| 135 |
* [langchain](https://github.com/hwchase17/langchain) is the main library this tool utilizes.
|
| 136 |
|
| 137 |
+
1. Upload your documents
|
| 138 |
+
2. Ask a questions
|
|
|
|
| 139 |
""")
|
|
|
|
|
|
|
| 140 |
with gr.Tab('File Upload'):
|
| 141 |
uploaded_files = gr.File(
|
| 142 |
label="Your Documents Upload (PDF or txt)", file_count="multiple", )
|
|
|
|
| 155 |
overflow_row_behaviour='paginate',
|
| 156 |
max_rows=5
|
| 157 |
)
|
| 158 |
+
buildb = gr.Textbox("⚠️Waiting for documents...",
|
| 159 |
label="Status", interactive=False, show_label=True,
|
| 160 |
max_lines=1)
|
| 161 |
stats = gr.Dataframe(headers=['Docs', 'Chunks'],
|
|
|
|
| 163 |
col_count=(2, "fixed"),
|
| 164 |
interactive=False,
|
| 165 |
label="Doc Stats")
|
| 166 |
+
dataset.change(validate_dataset, inputs=[dataset], outputs=[buildb])
|
|
|
|
|
|
|
|
|
|
| 167 |
uploaded_files.change(request_pathname, inputs=[
|
| 168 |
+
uploaded_files, data], outputs=[stats, data, dataset, buildb])
|
| 169 |
download.click(fn=download_repo, inputs=[
|
| 170 |
+
gh_repo, data], outputs=[stats, data, dataset, buildb])
|
| 171 |
query = gr.Textbox(
|
| 172 |
placeholder="Enter your question here...", label="Question")
|
| 173 |
with gr.Row():
|
|
|
|
| 186 |
|
| 187 |
with gr.Accordion("Raw Text", open=False):
|
| 188 |
passages = gr.Markdown(label="Passages")
|
| 189 |
+
ask.click(fn=do_ask, inputs=[query,
|
| 190 |
+
buildb, dataset,
|
| 191 |
length, marg, k, sources,
|
| 192 |
docs], outputs=[answer, context, passages, docs, stats])
|
| 193 |
|