import streamlit as st # from dotenv import load_dotenv import pickle from PyPDF2 import PdfReader from streamlit_extras.add_vertical_space import add_vertical_space from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.embeddings.openai import OpenAIEmbeddings from langchain.vectorstores import FAISS from langchain.llms import OpenAI from langchain.chains.question_answering import load_qa_chain from langchain.callbacks import get_openai_callback import os from langchain.vectorstores import Chroma from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings from langchain.embeddings import HuggingFaceHubEmbeddings # Sidebar contents from langchain.llms import HuggingFaceHub with st.sidebar: st.title('🤗💬 LLM Chat App') st.markdown(''' ## About This app is an LLM-powered chatbot built using: - [Streamlit](https://streamlit.io/) - [LangChain](https://python.langchain.com/) - [OpenAI](https://platform.openai.com/docs/models) LLM model ''') add_vertical_space(5) st.write( 'Made with ❤️ by [Prompt Engineer](https://youtube.com/@engineerprompt)') # load_dotenv() os.environ['OPEN_AI_APIKEY'] = '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' def main(): st.header("Chat with PDF 💬") # upload a PDF file pdf = st.file_uploader("Upload your PDF", type='pdf') # st.write(pdf) if pdf is not None: pdf_reader = PdfReader(pdf) text = "" for page in pdf_reader.pages: text += page.extract_text() text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=200, length_function=len ) chunks = text_splitter.split_text(text=text) # # embeddings store_name = pdf.name[:-4] st.write(f'{store_name}') # st.write(chunks) if os.path.exists(f"{store_name}.pkl"): with open(f"{store_name}.pkl", "rb") as f: VectorStore = pickle.load(f) # st.write('Embeddings Loaded from the Disk')s else: # embeddings = OpenAIEmbeddings( # openai_api_key='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') # embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") embeddings = HuggingFaceHubEmbeddings() VectorStore = FAISS.from_texts(chunks, embedding=embeddings) # VectorStore=Chroma.from_documents(chunks, embeddings) with open(f"{store_name}.pkl", "wb") as f: pickle.dump(VectorStore, f) # embeddings = OpenAIEmbeddings() # VectorStore = FAISS.from_texts(chunks, embedding=embeddings) # Accept user questions/query query = st.text_input("Ask questions about your PDF file:") # st.write(query) if query: docs = VectorStore.similarity_search(query=query, k=3) llm = HuggingFaceHub(repo_id='OpenAssistant/oasst-sft-1-pythia-12b', token='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', model_kwargs={"temperature": 0.1, "max_new_tokens": 250}) # llm = OpenAI() chain = load_qa_chain(llm=llm, chain_type="stuff") response = chain.run(input_documents=docs, question=query) # with get_openai_callback() as cb: # response = chain.run(input_documents=docs, question=query) # print(cb) st.write(response) if __name__ == '__main__': main()