tensorboy0101 commited on
Commit
e7e7187
·
verified ·
1 Parent(s): 3ad08a1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ import os
5
+ from langchain_core.prompts import ChatPromptTemplate
6
+ from langchain_community.embeddings.spacy_embeddings import SpacyEmbeddings
7
+ from langchain_community.vectorstores import FAISS
8
+ from langchain.tools.retriever import create_retriever_tool
9
+ from dotenv import load_dotenv
10
+ from langchain_anthropic import ChatAnthropic
11
+ from langchain.agents import AgentExecutor, create_tool_calling_agent
12
+
13
+ load_dotenv()
14
+ embeddings = SpacyEmbeddings(model_name="en_core_web_sm")
15
+ def pdf_read(pdf_doc):
16
+ text = ""
17
+ for pdf in pdf_doc:
18
+ pdf_reader = PdfReader(pdf)
19
+ for page in pdf_reader.pages:
20
+ text += page.extract_text()
21
+ return text
22
+
23
+
24
+
25
+ def get_chunks(text):
26
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
27
+ chunks = text_splitter.split_text(text)
28
+ return chunks
29
+
30
+
31
+ def vector_store(text_chunks):
32
+
33
+ vector_store = FAISS.from_texts(text_chunks, embedding=embeddings)
34
+ vector_store.save_local("faiss_db")
35
+
36
+
37
+ def get_conversational_chain(tools,ques):
38
+ #os.environ["ANTHROPIC_API_KEY"]=os.getenv["ANTHROPIC_API_KEY"]
39
+ llm = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0, api_key=os.getenv("ANTHROPIC_API_KEY"),verbose=True)
40
+
41
+ prompt = ChatPromptTemplate.from_messages(
42
+ [
43
+ (
44
+ "system",
45
+ """You are a helpful assistant. Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in
46
+ provided context just say, "answer is not available in the context", don't provide the wrong answer""",
47
+ ),
48
+ ("placeholder", "{chat_history}"),
49
+ ("human", "{input}"),
50
+ ("placeholder", "{agent_scratchpad}"),
51
+ ]
52
+ )
53
+ tool=[tools]
54
+ agent = create_tool_calling_agent(llm, tool, prompt)
55
+
56
+ agent_executor = AgentExecutor(agent=agent, tools=tool, verbose=True)
57
+ response=agent_executor.invoke({"input": ques})
58
+ print(response)
59
+ st.write("Reply: ", response['output'])
60
+
61
+
62
+
63
+ def user_input(user_question):
64
+
65
+
66
+
67
+ new_db = FAISS.load_local("faiss_db", embeddings,allow_dangerous_deserialization=True)
68
+
69
+ retriever=new_db.as_retriever()
70
+ retrieval_chain= create_retriever_tool(retriever,"pdf_extractor","This tool is to give answer to queries from the pdf")
71
+ get_conversational_chain(retrieval_chain,user_question)
72
+
73
+
74
+
75
+
76
+
77
+ def main():
78
+ st.set_page_config("Chat PDF")
79
+ st.header("RAG based Chat with PDF")
80
+
81
+ user_question = st.text_input("Ask a Question from the PDF Files")
82
+
83
+ if user_question:
84
+ user_input(user_question)
85
+
86
+ with st.sidebar:
87
+ st.title("Menu:")
88
+ pdf_doc = st.file_uploader("Upload your PDF Files and Click on the Submit & Process Button", accept_multiple_files=True)
89
+ if st.button("Submit & Process"):
90
+ with st.spinner("Processing..."):
91
+ raw_text = pdf_read(pdf_doc)
92
+ text_chunks = get_chunks(raw_text)
93
+ vector_store(text_chunks)
94
+ st.success("Done")
95
+
96
+ if __name__ == "__main__":
97
+ main()