Update app.py
Browse files
app.py
CHANGED
@@ -17,10 +17,9 @@ from langchain_g4f import G4FLLM
|
|
17 |
|
18 |
llm = LLM = G4FLLM(model=models.gpt_35_turbo,provider=Provider.DeepAi,)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
css_style = """
|
25 |
|
26 |
.gradio-container {
|
@@ -29,7 +28,7 @@ css_style = """
|
|
29 |
"""
|
30 |
|
31 |
|
32 |
-
def request_pathname(files, data):
|
33 |
if files is None:
|
34 |
return [[]]
|
35 |
for file in files:
|
@@ -37,15 +36,19 @@ def request_pathname(files, data):
|
|
37 |
if file.name in [x[0] for x in data]:
|
38 |
continue
|
39 |
data.append([file.name, None, None])
|
40 |
-
return [[len(data), 0]], data, data
|
41 |
|
42 |
|
43 |
-
def validate_dataset(dataset):
|
44 |
docs_ready = dataset.iloc[-1, 0] != ""
|
45 |
-
if docs_ready:
|
46 |
return "✨Ready✨"
|
47 |
-
|
|
|
|
|
48 |
return "⚠️Waiting for documents⚠️"
|
|
|
|
|
49 |
|
50 |
|
51 |
def make_stats(docs):
|
@@ -53,10 +56,11 @@ def make_stats(docs):
|
|
53 |
|
54 |
|
55 |
# , progress=gr.Progress()):
|
56 |
-
def do_ask(question, button, dataset, length, do_marg, k, max_sources, docs):
|
57 |
passages = ""
|
58 |
docs_ready = dataset.iloc[-1, 0] != ""
|
59 |
-
if button == "✨Ready✨" and docs_ready:
|
|
|
60 |
if docs is None:
|
61 |
docs = paperqa.Docs(llm=llm, embeddings=embeddings)
|
62 |
# dataset is pandas dataframe
|
@@ -85,7 +89,7 @@ def do_ask(question, button, dataset, length, do_marg, k, max_sources, docs):
|
|
85 |
yield result.formatted_answer, result.context, passages, docs, make_stats(docs)
|
86 |
|
87 |
|
88 |
-
def download_repo(gh_repo, data, pbar=gr.Progress()):
|
89 |
# download zipped version of repo
|
90 |
r = requests.get(f'https://api.github.com/repos/{gh_repo}/zipball')
|
91 |
if r.status_code == 200:
|
@@ -117,7 +121,7 @@ def download_repo(gh_repo, data, pbar=gr.Progress()):
|
|
117 |
if path in [x[0] for x in data]:
|
118 |
continue
|
119 |
data.append([path, citation, key])
|
120 |
-
yield [[len(data), 0]], data, data
|
121 |
pbar(int((i+1)/len(z.namelist()) * 99),
|
122 |
f'Added {f}')
|
123 |
pbar(100, 'Done')
|
@@ -130,6 +134,7 @@ with gr.Blocks(css=css_style) as demo:
|
|
130 |
|
131 |
docs = gr.State(None)
|
132 |
data = gr.State([])
|
|
|
133 |
|
134 |
gr.Markdown(f"""
|
135 |
# Document Question and Answer (v{paperqa.__version__})
|
@@ -145,9 +150,12 @@ with gr.Blocks(css=css_style) as demo:
|
|
145 |
* [PaperQA](https://github.com/whitead/paper-qa) is the code used to build this tool.
|
146 |
* [langchain](https://github.com/hwchase17/langchain) is the main library this tool utilizes.
|
147 |
|
148 |
-
1.
|
149 |
-
2.
|
|
|
150 |
""")
|
|
|
|
|
151 |
with gr.Tab('File Upload'):
|
152 |
uploaded_files = gr.File(
|
153 |
label="Your Documents Upload (PDF or txt)", file_count="multiple", )
|
@@ -166,7 +174,7 @@ with gr.Blocks(css=css_style) as demo:
|
|
166 |
overflow_row_behaviour='paginate',
|
167 |
max_rows=5
|
168 |
)
|
169 |
-
buildb = gr.Textbox("⚠️Waiting for documents...",
|
170 |
label="Status", interactive=False, show_label=True,
|
171 |
max_lines=1)
|
172 |
stats = gr.Dataframe(headers=['Docs', 'Chunks'],
|
@@ -174,11 +182,14 @@ with gr.Blocks(css=css_style) as demo:
|
|
174 |
col_count=(2, "fixed"),
|
175 |
interactive=False,
|
176 |
label="Doc Stats")
|
177 |
-
|
|
|
|
|
|
|
178 |
uploaded_files.change(request_pathname, inputs=[
|
179 |
-
uploaded_files, data], outputs=[stats, data, dataset, buildb])
|
180 |
download.click(fn=download_repo, inputs=[
|
181 |
-
gh_repo, data], outputs=[stats, data, dataset, buildb])
|
182 |
query = gr.Textbox(
|
183 |
placeholder="Enter your question here...", label="Question")
|
184 |
with gr.Row():
|
@@ -197,8 +208,8 @@ with gr.Blocks(css=css_style) as demo:
|
|
197 |
|
198 |
with gr.Accordion("Raw Text", open=False):
|
199 |
passages = gr.Markdown(label="Passages")
|
200 |
-
ask.click(fn=do_ask, inputs=[query,
|
201 |
-
|
202 |
length, marg, k, sources,
|
203 |
docs], outputs=[answer, context, passages, docs, stats])
|
204 |
|
|
|
17 |
|
18 |
llm = LLM = G4FLLM(model=models.gpt_35_turbo,provider=Provider.DeepAi,)
|
19 |
|
20 |
+
embed_model = HuggingFaceEmbeddings(
|
21 |
+
model_name="sentence-transformers/all-mpnet-base-v2"
|
22 |
+
)
|
|
|
23 |
css_style = """
|
24 |
|
25 |
.gradio-container {
|
|
|
28 |
"""
|
29 |
|
30 |
|
31 |
+
def request_pathname(files, data, openai_api_key):
|
32 |
if files is None:
|
33 |
return [[]]
|
34 |
for file in files:
|
|
|
36 |
if file.name in [x[0] for x in data]:
|
37 |
continue
|
38 |
data.append([file.name, None, None])
|
39 |
+
return [[len(data), 0]], data, data, validate_dataset(pd.DataFrame(data), openai_api_key)
|
40 |
|
41 |
|
42 |
+
def validate_dataset(dataset, openapi):
|
43 |
docs_ready = dataset.iloc[-1, 0] != ""
|
44 |
+
if docs_ready and type(openapi) is str and len(openapi) > 0:
|
45 |
return "✨Ready✨"
|
46 |
+
elif docs_ready:
|
47 |
+
return "⚠️Waiting for key⚠️"
|
48 |
+
elif type(openapi) is str and len(openapi) > 0:
|
49 |
return "⚠️Waiting for documents⚠️"
|
50 |
+
else:
|
51 |
+
return "⚠️Waiting for documents and key⚠️"
|
52 |
|
53 |
|
54 |
def make_stats(docs):
|
|
|
56 |
|
57 |
|
58 |
# , progress=gr.Progress()):
|
59 |
+
def do_ask(question, button, openapi, dataset, length, do_marg, k, max_sources, docs):
|
60 |
passages = ""
|
61 |
docs_ready = dataset.iloc[-1, 0] != ""
|
62 |
+
if button == "✨Ready✨" and type(openapi) is str and len(openapi) > 0 and docs_ready:
|
63 |
+
os.environ['OPENAI_API_KEY'] = openapi.strip()
|
64 |
if docs is None:
|
65 |
docs = paperqa.Docs(llm=llm, embeddings=embeddings)
|
66 |
# dataset is pandas dataframe
|
|
|
89 |
yield result.formatted_answer, result.context, passages, docs, make_stats(docs)
|
90 |
|
91 |
|
92 |
+
def download_repo(gh_repo, data, openai_api_key, pbar=gr.Progress()):
|
93 |
# download zipped version of repo
|
94 |
r = requests.get(f'https://api.github.com/repos/{gh_repo}/zipball')
|
95 |
if r.status_code == 200:
|
|
|
121 |
if path in [x[0] for x in data]:
|
122 |
continue
|
123 |
data.append([path, citation, key])
|
124 |
+
yield [[len(data), 0]], data, data, validate_dataset(pd.DataFrame(data), openai_api_key)
|
125 |
pbar(int((i+1)/len(z.namelist()) * 99),
|
126 |
f'Added {f}')
|
127 |
pbar(100, 'Done')
|
|
|
134 |
|
135 |
docs = gr.State(None)
|
136 |
data = gr.State([])
|
137 |
+
openai_api_key = gr.State('')
|
138 |
|
139 |
gr.Markdown(f"""
|
140 |
# Document Question and Answer (v{paperqa.__version__})
|
|
|
150 |
* [PaperQA](https://github.com/whitead/paper-qa) is the code used to build this tool.
|
151 |
* [langchain](https://github.com/hwchase17/langchain) is the main library this tool utilizes.
|
152 |
|
153 |
+
1. Enter API Key ([What is that?](https://platform.openai.com/account/api-keys))
|
154 |
+
2. Upload your documents
|
155 |
+
3. Ask a questions
|
156 |
""")
|
157 |
+
openai_api_key = gr.Textbox(
|
158 |
+
label="OpenAI API Key", placeholder="sk-...", type="password")
|
159 |
with gr.Tab('File Upload'):
|
160 |
uploaded_files = gr.File(
|
161 |
label="Your Documents Upload (PDF or txt)", file_count="multiple", )
|
|
|
174 |
overflow_row_behaviour='paginate',
|
175 |
max_rows=5
|
176 |
)
|
177 |
+
buildb = gr.Textbox("⚠️Waiting for documents and key...",
|
178 |
label="Status", interactive=False, show_label=True,
|
179 |
max_lines=1)
|
180 |
stats = gr.Dataframe(headers=['Docs', 'Chunks'],
|
|
|
182 |
col_count=(2, "fixed"),
|
183 |
interactive=False,
|
184 |
label="Doc Stats")
|
185 |
+
openai_api_key.change(validate_dataset, inputs=[
|
186 |
+
dataset, openai_api_key], outputs=[buildb])
|
187 |
+
dataset.change(validate_dataset, inputs=[
|
188 |
+
dataset, openai_api_key], outputs=[buildb])
|
189 |
uploaded_files.change(request_pathname, inputs=[
|
190 |
+
uploaded_files, data, openai_api_key], outputs=[stats, data, dataset, buildb])
|
191 |
download.click(fn=download_repo, inputs=[
|
192 |
+
gh_repo, data, openai_api_key], outputs=[stats, data, dataset, buildb])
|
193 |
query = gr.Textbox(
|
194 |
placeholder="Enter your question here...", label="Question")
|
195 |
with gr.Row():
|
|
|
208 |
|
209 |
with gr.Accordion("Raw Text", open=False):
|
210 |
passages = gr.Markdown(label="Passages")
|
211 |
+
ask.click(fn=do_ask, inputs=[query, buildb,
|
212 |
+
openai_api_key, dataset,
|
213 |
length, marg, k, sources,
|
214 |
docs], outputs=[answer, context, passages, docs, stats])
|
215 |
|