Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 3 |
+
from langchain_community.embeddings import OpenAIEmbeddings
|
| 4 |
+
from langchain_community.vectorstores import Chroma
|
| 5 |
+
from langchain_community.llms import OpenAI
|
| 6 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 7 |
+
from langchain.memory import ConversationBufferMemory
|
| 8 |
+
|
| 9 |
+
# Set up your API key for OpenAI
|
| 10 |
+
os.environ["OPENAI_API_KEY"] = "your_openai_api_key"
|
| 11 |
+
|
| 12 |
+
def load_document(file_path):
|
| 13 |
+
"""Load and parse the document."""
|
| 14 |
+
loader = PyPDFLoader(file_path)
|
| 15 |
+
documents = loader.load()
|
| 16 |
+
return documents
|
| 17 |
+
|
| 18 |
+
def setup_vector_store(documents):
|
| 19 |
+
"""Create embeddings and store them in a vector database."""
|
| 20 |
+
embeddings = OpenAIEmbeddings()
|
| 21 |
+
vector_store = Chroma.from_documents(documents, embeddings)
|
| 22 |
+
return vector_store
|
| 23 |
+
|
| 24 |
+
def setup_retrieval_chain(vector_store):
|
| 25 |
+
"""Set up the conversational retrieval chain with memory."""
|
| 26 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 27 |
+
retrieval_chain = ConversationalRetrievalChain.from_llm(
|
| 28 |
+
OpenAI(model_name="gpt-4"),
|
| 29 |
+
retriever=vector_store.as_retriever(),
|
| 30 |
+
memory=memory
|
| 31 |
+
)
|
| 32 |
+
return retrieval_chain
|
| 33 |
+
|
| 34 |
+
def query_document(retrieval_chain):
|
| 35 |
+
"""CLI loop to interactively query the document."""
|
| 36 |
+
print("Interactive Document Query Tool")
|
| 37 |
+
print("Type 'exit' to stop the session.\n")
|
| 38 |
+
while True:
|
| 39 |
+
user_query = input("Enter your question: ")
|
| 40 |
+
if user_query.lower() == "exit":
|
| 41 |
+
print("Exiting the query tool. Goodbye!")
|
| 42 |
+
break
|
| 43 |
+
response = retrieval_chain({"question": user_query})
|
| 44 |
+
print("Answer:", response['answer'])
|
| 45 |
+
print("\n")
|
| 46 |
+
|
| 47 |
+
def main():
|
| 48 |
+
# Load the document
|
| 49 |
+
file_path = input("Enter the path to your PDF document: ")
|
| 50 |
+
documents = load_document(file_path)
|
| 51 |
+
print("DOC Loaded")
|
| 52 |
+
|
| 53 |
+
# Set up the vector store
|
| 54 |
+
vector_store = setup_vector_store(documents)
|
| 55 |
+
|
| 56 |
+
# Set up the retrieval chain
|
| 57 |
+
retrieval_chain = setup_retrieval_chain(vector_store)
|
| 58 |
+
|
| 59 |
+
# Start querying the document
|
| 60 |
+
query_document(retrieval_chain)
|
| 61 |
+
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
main()
|