CoolFace
Apppublic

Arshil711/PDF_Detail_Extractor

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1from PyPDF2 import PdfReader2from langchain.embeddings.openai import OpenAIEmbeddings3from langchain.text_splitter import CharacterTextSplitter4from langchain.vectorstores import FAISS5# from constants import api_key6import os7import openai8from langchain.chains.question_answering import load_qa_chain9from langchain.llms import OpenAI10import streamlit as st11 12 13 14 15 16def get_pdf_text(pdf_docs):17    text=""18    for pdf in pdf_docs:19        pdf_reader= PdfReader(pdf)20        for page in pdf_reader.pages:21            text+= page.extract_text()22    return  text23 24def get_text_chunks(text):25    text_splitter = CharacterTextSplitter(chunk_size=10000, chunk_overlap=1000)26    chunks = text_splitter.split_text(text)27    return chunks28 29 30def get_vector_store(text_chunks):31    embeddings = OpenAIEmbeddings()32    vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)33    vector_store.save_local("faiss_index")34 35def user_input(user_question):36    embeddings = OpenAIEmbeddings()37    new_db = FAISS.load_local("faiss_index", embeddings)38    chain=load_qa_chain(OpenAI(),chain_type='stuff')39 40    docs=new_db.similarity_search(user_question)41    42    st.write("Reply: ", chain.run(input_documents=docs,question=user_question))43 44 45 46def main():47    st.set_page_config("Chat PDF")48    st.header("Chat with PDF ")49 50    user_question = st.text_input("Ask a Question from the PDF Files")51 52    if user_question:53        user_input(user_question)54       55 56    with st.sidebar:57        st.title("Menu:")58        pdf_docs = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)59        if st.button("Submit & Process"):60            with st.spinner("Processing..."):61                raw_text = get_pdf_text(pdf_docs)62                text_chunks = get_text_chunks(raw_text)63                get_vector_store(text_chunks)64                st.success("Done")65 66if __name__ == "__main__":67    main()