File size: 2,570 Bytes
c8149d3
855467f
c8149d3
855467f
 
c8149d3
 
855467f
 
c8149d3
 
 
c7aff60
855467f
 
c8149d3
855467f
 
c8149d3
855467f
 
c7aff60
855467f
 
 
 
 
c7aff60
855467f
 
 
 
 
 
 
 
 
 
 
 
 
 
c7aff60
 
 
 
855467f
c8149d3
855467f
 
 
c8149d3
855467f
 
c8149d3
855467f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import nest_asyncio
import gradio as gr
from dotenv import load_dotenv
from IPython.display import Markdown, display

from llama_index.core import Settings
from llama_index.core import VectorStoreIndex, ServiceContext, SimpleDirectoryReader

from llama_index.llms.cohere import Cohere
from llama_index.embeddings.cohere import CohereEmbedding
from llama_index.postprocessor.cohere_rerank import CohereRerank

# allows nested access to the event loop
nest_asyncio.apply()

# put your API key here, find one at: https://dashboard.cohere.com/api-keys
API_KEY = 'ziEpsRreaJzBi5HUDap7gMecJWXX69O26Hf71Kxo'

# setup llm & embedding model
llm = Cohere(api_key=API_KEY, model="command-r-plus")

embed_model = CohereEmbedding(
    cohere_api_key=API_KEY,
    model_name="embed-english-v3.0",
    input_type="search_query",
)

# Function to load data from uploaded PDF
def process_pdfs(pdf_files):
    # Create a temporary directory to store the uploaded PDFs
    temp_dir = 'temp_pdf_directory'
    os.makedirs(temp_dir, exist_ok=True)
    
    # Save uploaded files to the temporary directory
    for file in pdf_files:
        file_path = os.path.join(temp_dir, file.name)
        with open(file_path, 'wb') as f:
            f.write(file.read())
    
    # Load data from the temporary directory
    loader = SimpleDirectoryReader(
                input_dir=temp_dir,
                required_exts=[".pdf"],
                recursive=True
            )
    docs = loader.load_data()
    
    # Create an index over loaded data
    Settings.embed_model = embed_model
    index = VectorStoreIndex.from_documents(docs, show_progress=True)
    
    # Create a cohere reranker
    cohere_rerank = CohereRerank(api_key=API_KEY)
    
    # Create the query engine, where we use a cohere reranker on the fetched nodes
    Settings.llm = llm
    query_engine = index.as_query_engine(node_postprocessors=[cohere_rerank])
    
    return index, query_engine

# Query function
def query_pdfs(pdf_files, question):
    index, query_engine = process_pdfs(pdf_files)
    response = query_engine.query(question)
    return str(response)

# Create Gradio interface
iface = gr.Interface(
    fn=query_pdfs,
    inputs=[
        gr.inputs.File(label="Upload PDF Files", type="file", multiple=True),
        gr.inputs.Textbox(label="Ask a Question", placeholder="Enter your question here...")
    ],
    outputs="text",
    title="PDF Query System",
    description="Upload PDF files and ask questions to extract information from them."
)

if __name__ == "__main__":
    iface.launch()