├── requirementes.txt ├── gptbook1.jpg ├── gptbook2.jpg ├── LICENSE.txt ├── README.md └── gpt_book_assistant.py /requirementes.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | langchain 3 | PyPDFLoader 4 | -------------------------------------------------------------------------------- /gptbook1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fstech-digital/gptbook/HEAD/gptbook1.jpg -------------------------------------------------------------------------------- /gptbook2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fstech-digital/gptbook/HEAD/gptbook2.jpg -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Felipe Silva 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GPTBook 2 | GPT Book é uma aplicação desenvolvida em Python que utiliza o modelo de linguagem GPT-4 para ler e processar livros em PDF, permitindo ao usuário interagir e buscar informações relevantes dentro do conteúdo. 3 | 4 | ![Descrição da Imagem 1](https://github.com/felipeOliveira-1/gptbook/blob/main/gptbook1.jpg?raw=true) 5 | ![Descrição da Imagem 2](https://github.com/felipeOliveira-1/gptbook/blob/main/gptbook2.jpg?raw=true) 6 | 7 | 8 | ## Requisitos 9 | * Python 3.8+ 10 | 11 | *Bibliotecas*: 12 | * streamlit 13 | * langchain 14 | * PyPDFLoader 15 | * Chroma 16 | ## Instalação e configuração 17 | 1. Instale as bibliotecas necessárias: 18 | 19 | `` 20 | pip install streamlit langchain PyPDFLoader Chroma 21 | `` 22 | 23 | 2. Clone o repositório e acesse a pasta do projeto 24 | 25 | `` 26 | git clone [URL_DO_REPOSITORIO] 27 | `` 28 | 29 | `` 30 | cd gpt_book 31 | `` 32 | 33 | 3. Adicione a chave da API da OpenAI ao arquivo gpt_book.py 34 | 35 | `` 36 | os.environ['OPENAI_API_KEY'] = 'sua_chave_da_api' 37 | `` 38 | 39 | 4. Execute o aplicativo: 40 | 41 | `` 42 | streamlit run gpt_book.py 43 | `` 44 | 45 | ## Uso 46 | 1. Faça o upload do livro em PDF que deseja processar. 47 | 2. Aguarde a mensagem de sucesso ao concluir o processamento. 48 | 3. Digite um prompt na caixa de texto e pressione Enter para obter a resposta do GPT-4. 49 | 4. Veja os resultados relevantes das páginas do livro processado. 50 | 51 | Com o GPT Book, você pode obter respostas rápidas e relevantes de um livro. 52 | -------------------------------------------------------------------------------- /gpt_book_assistant.py: -------------------------------------------------------------------------------- 1 | # Import os to set API key 2 | import os 3 | # Import OpenAI as main LLM service 4 | from langchain.llms import OpenAI 5 | # Bring in streamlit for UI/app interface 6 | import streamlit as st 7 | 8 | # Import PDF document loaders...there's other ones as well! 9 | from langchain.document_loaders import PyPDFLoader 10 | # Import chroma as the vector store 11 | from langchain.vectorstores import Chroma 12 | 13 | 14 | # Import vector store stuff 15 | from langchain.agents.agent_toolkits import ( 16 | create_vectorstore_agent, 17 | VectorStoreToolkit, 18 | VectorStoreInfo 19 | ) 20 | 21 | # Set APIkey for OpenAI Service 22 | # Can sub this out for other LLM providers 23 | os.environ['OPENAI_API_KEY'] = '' 24 | 25 | # Create instance of OpenAI LLM 26 | llm = OpenAI(temperature=0.1, verbose=True) 27 | 28 | def process_pdf(book_filename): 29 | # Create and load PDF Loader 30 | loader = PyPDFLoader(book_filename) 31 | # Split pages from pdf 32 | pages = loader.load_and_split() 33 | # Load documents into vector database aka ChromaDB 34 | store = Chroma.from_documents(pages, collection_name='user_book') 35 | 36 | # Create vectorstore info object - metadata repo? 37 | vectorstore_info = VectorStoreInfo( 38 | name="user_book", 39 | description="a user's uploaded book as a pdf", 40 | vectorstore=store 41 | ) 42 | # Convert the document store into a langchain toolkit 43 | toolkit = VectorStoreToolkit(vectorstore_info=vectorstore_info) 44 | 45 | # Add the toolkit to an end-to-end LC 46 | agent_executor = create_vectorstore_agent( 47 | llm=llm, 48 | toolkit=toolkit, 49 | verbose=True 50 | ) 51 | 52 | return agent_executor, store 53 | 54 | st.title('🦜🔗 GPT Book') 55 | st.subheader('Upload seu livro em PDF') 56 | 57 | uploaded_file = st.file_uploader("escolha o arquivo", type=['pdf']) 58 | 59 | if uploaded_file is not None: 60 | with st.spinner('Processando...'): 61 | book_filename = f'user_uploaded_book.pdf' 62 | with open(book_filename, 'wb') as f: 63 | f.write(uploaded_file.getbuffer()) 64 | agent_executor, store = process_pdf(book_filename) 65 | st.success('Livro processado com sucesso!') 66 | 67 | # Create a text input box for the user 68 | prompt = st.text_input('Escreva seu prompt aqui') 69 | 70 | # If the user hits enter 71 | if prompt: 72 | # Then pass the prompt to the LLM 73 | response = agent_executor.run(prompt) 74 | # ...and write it out to the screen 75 | st.write(response) 76 | 77 | # With a streamlit expander 78 | with st.expander('Pesquisa de Similaridade de Documentos'): 79 | # Find the relevant pages 80 | search = store.similarity_search_with_score(prompt) 81 | # Write out the first 82 | st.write(search[0][0].page_content) 83 | else: 84 | st.warning('Por favor upload um documento em formato PDF.') 85 | --------------------------------------------------------------------------------