File size: 1,861 Bytes
caa8fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235d036
 
caa8fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235d036
caa8fb2
 
 
235d036
 
caa8fb2
 
 
 
235d036
caa8fb2
 
 
 
 
 
 
 
 
 
235d036
 
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
import numpy as np
import pandas as pd
import configparser
import streamlit as st
import chromadb
import langchain
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import PyPDFLoader
import tempfile
import shutil
import os
import openai

# Replace with your OpenAI API key
api_key = "your_openai_api_key_here"

if not api_key:
    st.error("I can not find API key")
else:
    openai.api_key = api_key

    st.title('PDF Documents Q&A App')

    uploaded_file = st.file_uploader("Please upload your PDF file", type=["pdf"])
    if uploaded_file is not None:
        # Save as tempfile
        with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
            shutil.copyfileobj(uploaded_file, tmpfile)
            file_path = tmpfile.name

        # Load and split PDF using PyPDFLoader
        loader = PyPDFLoader(file_path)
        pages = loader.load_and_split()

        # Initialize model and vectorstore
        llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
        embeddings = OpenAIEmbeddings()
        vectorstore = Chroma.from_documents(pages, embedding=embeddings, persist_directory=".")
        vectorstore.persist()

        # Create Q&A chain
        pdf_qa = ConversationalRetrievalChain.from_llm(llm, vectorstore.as_retriever(), return_source_documents=True)

        # Get question from user
        question = st.text_input("Please input your question:")
        if st.button('Get Answer'):
            result = pdf_qa({"question": question, "chat_history": []})
            answer = result["answer"]
            st.write("Answer:", answer)

        # Delete tempfile
        if file_path:
            os.unlink(file_path)