kzachos commited on
Commit
0690fa3
·
1 Parent(s): 603036f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -5,11 +5,16 @@ import os
5
  import json
6
  import gradio as gr
7
 
8
- def construct_index(file):
 
 
 
 
 
9
  PDFReader = download_loader("PDFReader")
10
 
11
  loader = PDFReader()
12
- documents = loader.load_data(file=Path(file.name))
13
 
14
  # Construct a simple vector index
15
  index = GPTSimpleVectorIndex.from_documents(documents)
@@ -19,10 +24,10 @@ def construct_index(file):
19
 
20
  return index
21
 
22
- def qabot(file, input_text):
23
- # Load the index from the uploaded file
24
- index = construct_index(file)
25
 
 
26
  # Query the index with the user's input text
27
  response = index.query(input_text, response_mode="compact")
28
  return response.response
@@ -31,10 +36,11 @@ def qabot(file, input_text):
31
  file_upload = gr.inputs.File(label="Upload PDF file")
32
 
33
  # Change the input components of the function to the file upload component and a text box for user input
34
- iface = gr.Interface(fn=qabot, inputs=[file_upload, gr.inputs.Textbox(lines=7, label='Enter your query')], outputs="text", title="Custom-trained QA Application")
35
 
36
  # Add a separate interface to update the index
37
  def update_index(file):
 
38
  index = construct_index(file)
39
  return "Index generated from uploaded file: {}".format(file.name)
40
 
@@ -42,4 +48,4 @@ update_index_interface = gr.Interface(update_index, inputs=file_upload, outputs=
42
 
43
  # Launch both interfaces
44
  iface.launch()
45
- update_index_interface.launch()
 
5
  import json
6
  import gradio as gr
7
 
8
+ def load_index(file_path):
9
+ # Load the index from the saved file
10
+ index = GPTSimpleVectorIndex.load_from_disk(file_path)
11
+ return index
12
+
13
+ def construct_index(file_path):
14
  PDFReader = download_loader("PDFReader")
15
 
16
  loader = PDFReader()
17
+ documents = loader.load_data(file=Path(file_path))
18
 
19
  # Construct a simple vector index
20
  index = GPTSimpleVectorIndex.from_documents(documents)
 
24
 
25
  return index
26
 
27
+ # Initialize the index variable with the index from the saved file
28
+ index = load_index('index.json')
 
29
 
30
+ def qabot(input_text):
31
  # Query the index with the user's input text
32
  response = index.query(input_text, response_mode="compact")
33
  return response.response
 
36
  file_upload = gr.inputs.File(label="Upload PDF file")
37
 
38
  # Change the input components of the function to the file upload component and a text box for user input
39
+ iface = gr.Interface(fn=qabot, inputs=gr.inputs.Textbox(lines=7, label='Enter your query'), outputs="text", title="Custom-trained QA Application")
40
 
41
  # Add a separate interface to update the index
42
  def update_index(file):
43
+ global index
44
  index = construct_index(file)
45
  return "Index generated from uploaded file: {}".format(file.name)
46
 
 
48
 
49
  # Launch both interfaces
50
  iface.launch()
51
+ update_index_interface.launch()