File size: 2,107 Bytes
c851669
b9001d0
 
 
 
 
c851669
b9001d0
0690fa3
b9001d0
 
 
0690fa3
b9001d0
c851669
 
 
 
 
b9001d0
 
 
3a90120
c851669
 
 
 
 
 
 
be9af42
 
b9001d0
 
 
3035a83
 
b9001d0
be9af42
3a90120
3035a83
be9af42
3035a83
c851669
 
 
 
 
 
 
 
 
 
 
 
 
 
3035a83
 
 
 
 
 
0690fa3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Import necessary packages
from llama_index import GPTSimpleVectorIndex, download_loader
from pathlib import Path
import os
import json
import gradio as gr 
import tempfile

def construct_index(file_path):
    PDFReader = download_loader("PDFReader")

    loader = PDFReader()
    documents = loader.load_data(file=Path(file_path))

    # Construct a simple vector index
    index = GPTSimpleVectorIndex.from_documents(documents)

    # Save your index to a index.json file
    index.save_to_disk('index.json')

    return index

def qabot(file, input_text):
    # Check if index already exists
    if not os.path.exists('index.json'):
        # If index does not exist, create index from file
        index = construct_index(file.name)
    else:
        # If index exists, load index from file
        index = GPTSimpleVectorIndex.load_from_disk('index.json')

    # Query the index with the user's input text
    response = index.query(input_text, response_mode="compact")
    return response.response

# Add input component for file upload
file_upload = gr.inputs.File(label="Upload PDF file")

# Change the input components of the function to the file upload component and a text box for user input
iface = gr.Interface(fn=qabot, inputs=[file_upload, gr.inputs.Textbox(lines=7, label='Enter your query')], outputs="text", title="Custom-trained QA Application")

# Add a separate interface to update the index
def update_index(file):
    # Save the uploaded file to a temporary file
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_file.write(file.read())
        temp_file_path = temp_file.name

    # Construct the index from the temporary file
    index = construct_index(temp_file_path)

    # Remove the temporary file
    os.remove(temp_file_path)

    # Update the index file
    index.save_to_disk('index.json')

    return "Index generated from uploaded file: {}".format(file.name)

update_index_interface = gr.Interface(update_index, inputs=file_upload, outputs="text", title="Update Index")

# Launch both interfaces
iface.launch()
update_index_interface.launch()