Spaces:
Runtime error
Runtime error
Avanzando y traduciendo
Browse files
app.py
CHANGED
@@ -9,27 +9,21 @@ def request_pathname(files):
|
|
9 |
return [[file.name, file.name.split('/')[-1]] for file in files]
|
10 |
|
11 |
|
12 |
-
def validate_dataset(dataset
|
13 |
global docs
|
14 |
docs = None # clear it out if dataset is modified
|
15 |
docs_ready = dataset.iloc[-1, 0] != ""
|
16 |
-
if docs_ready
|
17 |
-
return "✨
|
18 |
-
elif docs_ready:
|
19 |
-
return "⚠️Waiting for key..."
|
20 |
-
elif type(openapi) is str and len(openapi) > 0:
|
21 |
-
return "⚠️Waiting for documents..."
|
22 |
else:
|
23 |
-
return "⚠️Waiting for documents
|
24 |
|
25 |
|
26 |
-
def do_ask(question, button,
|
27 |
global docs
|
28 |
docs_ready = dataset.iloc[-1, 0] != ""
|
29 |
-
if button == "✨
|
30 |
if docs is None: # don't want to rebuild index if it's already built
|
31 |
-
import os
|
32 |
-
os.environ['OPENAI_API_KEY'] = openapi.strip()
|
33 |
import paperqa
|
34 |
docs = paperqa.Docs()
|
35 |
# dataset is pandas dataframe
|
@@ -40,17 +34,21 @@ def do_ask(question, button, openapi, dataset, progress=gr.Progress()):
|
|
40 |
docs.add(row['filepath'], row['citation string'], key=key)
|
41 |
else:
|
42 |
return ""
|
43 |
-
progress(0, "
|
44 |
docs._build_faiss_index()
|
45 |
-
progress(0.25, "
|
46 |
result = docs.query(question)
|
47 |
-
progress(1.0, "
|
48 |
return result.formatted_answer, result.context
|
49 |
|
50 |
|
51 |
with gr.Blocks() as demo:
|
52 |
gr.Markdown("""
|
53 |
-
# Document Question and Answer
|
|
|
|
|
|
|
|
|
54 |
|
55 |
This tool will enable asking questions of your uploaded text or PDF documents.
|
56 |
It uses OpenAI's GPT models and thus you must enter your API key below. This
|
@@ -60,15 +58,13 @@ with gr.Blocks() as demo:
|
|
60 |
* [PaperQA](https://github.com/whitead/paper-qa) is the code used to build this tool.
|
61 |
* [langchain](https://github.com/hwchase17/langchain) is the main library this tool utilizes.
|
62 |
|
63 |
-
##
|
64 |
|
65 |
-
|
66 |
-
|
67 |
""")
|
68 |
-
openai_api_key = gr.Textbox(
|
69 |
-
label="OpenAI API Key", placeholder="sk-...", type="password")
|
70 |
uploaded_files = gr.File(
|
71 |
-
label="
|
72 |
dataset = gr.Dataframe(
|
73 |
headers=["filepath", "citation string"],
|
74 |
datatype=["str", "str"],
|
@@ -76,23 +72,21 @@ with gr.Blocks() as demo:
|
|
76 |
interactive=True,
|
77 |
label="Documents and Citations"
|
78 |
)
|
79 |
-
buildb = gr.Textbox("⚠️
|
80 |
label="Status", interactive=False, show_label=True)
|
81 |
-
openai_api_key.change(validate_dataset, inputs=[
|
82 |
-
dataset, openai_api_key], outputs=[buildb])
|
83 |
dataset.change(validate_dataset, inputs=[
|
84 |
dataset, openai_api_key], outputs=[buildb])
|
85 |
uploaded_files.change(request_pathname, inputs=[
|
86 |
uploaded_files], outputs=[dataset])
|
87 |
query = gr.Textbox(
|
88 |
-
placeholder="
|
89 |
-
ask = gr.Button("
|
90 |
-
gr.Markdown("##
|
91 |
-
answer = gr.Markdown(label="
|
92 |
-
with gr.Accordion("
|
93 |
gr.Markdown(
|
94 |
-
"###
|
95 |
-
context = gr.Markdown(label="
|
96 |
ask.click(fn=do_ask, inputs=[query, buildb,
|
97 |
openai_api_key, dataset], outputs=[answer, context])
|
98 |
|
|
|
9 |
return [[file.name, file.name.split('/')[-1]] for file in files]
|
10 |
|
11 |
|
12 |
+
def validate_dataset(dataset):
|
13 |
global docs
|
14 |
docs = None # clear it out if dataset is modified
|
15 |
docs_ready = dataset.iloc[-1, 0] != ""
|
16 |
+
if docs_ready:
|
17 |
+
return "✨Listo✨"
|
|
|
|
|
|
|
|
|
18 |
else:
|
19 |
+
return "⚠️Waiting for documents..."
|
20 |
|
21 |
|
22 |
+
def do_ask(question, button, dataset, progress=gr.Progress()):
|
23 |
global docs
|
24 |
docs_ready = dataset.iloc[-1, 0] != ""
|
25 |
+
if button == "✨Listo✨" and docs_ready:
|
26 |
if docs is None: # don't want to rebuild index if it's already built
|
|
|
|
|
27 |
import paperqa
|
28 |
docs = paperqa.Docs()
|
29 |
# dataset is pandas dataframe
|
|
|
34 |
docs.add(row['filepath'], row['citation string'], key=key)
|
35 |
else:
|
36 |
return ""
|
37 |
+
progress(0, "Construyendo índices...")
|
38 |
docs._build_faiss_index()
|
39 |
+
progress(0.25, "Encolando...")
|
40 |
result = docs.query(question)
|
41 |
+
progress(1.0, "¡Hecho!")
|
42 |
return result.formatted_answer, result.context
|
43 |
|
44 |
|
45 |
with gr.Blocks() as demo:
|
46 |
gr.Markdown("""
|
47 |
+
# Document Question and Answer adaptado al castellano por Pablo Ascorbe.
|
48 |
+
|
49 |
+
Este espacio ha sido clonado y adaptado de: https://huggingface.co/spaces/whitead/paper-qa
|
50 |
+
|
51 |
+
- Texto original:
|
52 |
|
53 |
This tool will enable asking questions of your uploaded text or PDF documents.
|
54 |
It uses OpenAI's GPT models and thus you must enter your API key below. This
|
|
|
58 |
* [PaperQA](https://github.com/whitead/paper-qa) is the code used to build this tool.
|
59 |
* [langchain](https://github.com/hwchase17/langchain) is the main library this tool utilizes.
|
60 |
|
61 |
+
## Instrucciones:
|
62 |
|
63 |
+
Adjunte su documento, ya sea en formato .txt o .pdf, y pregunte lo que desee.
|
64 |
+
|
65 |
""")
|
|
|
|
|
66 |
uploaded_files = gr.File(
|
67 |
+
label="Sus documentos subidos (PDF o txt)", file_count="multiple", )
|
68 |
dataset = gr.Dataframe(
|
69 |
headers=["filepath", "citation string"],
|
70 |
datatype=["str", "str"],
|
|
|
72 |
interactive=True,
|
73 |
label="Documents and Citations"
|
74 |
)
|
75 |
+
buildb = gr.Textbox("⚠️Esperando documentos...",
|
76 |
label="Status", interactive=False, show_label=True)
|
|
|
|
|
77 |
dataset.change(validate_dataset, inputs=[
|
78 |
dataset, openai_api_key], outputs=[buildb])
|
79 |
uploaded_files.change(request_pathname, inputs=[
|
80 |
uploaded_files], outputs=[dataset])
|
81 |
query = gr.Textbox(
|
82 |
+
placeholder="Introduzca su pregunta aquí...", label="Pregunta")
|
83 |
+
ask = gr.Button("Pregunte")
|
84 |
+
gr.Markdown("## Respuesta")
|
85 |
+
answer = gr.Markdown(label="Respuesta")
|
86 |
+
with gr.Accordion("Contexto", open=False):
|
87 |
gr.Markdown(
|
88 |
+
"### Contexto\n\nEl siguiente contexto ha sido utilizado para generar la respuesta:")
|
89 |
+
context = gr.Markdown(label="Contexto")
|
90 |
ask.click(fn=do_ask, inputs=[query, buildb,
|
91 |
openai_api_key, dataset], outputs=[answer, context])
|
92 |
|