-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
126 lines (92 loc) · 3.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from langchain_google_genai import GoogleGenerativeAI
from langchain.agents import load_tools, initialize_agent, AgentType
from dotenv import load_dotenv
import streamlit as st
from langchain.callbacks import StreamlitCallbackHandler
from langchain import FAISS
from PyPDF2 import PdfReader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain.chains.question_answering import load_qa_chain
#loading environment variables
load_dotenv()
#creating model
llm = GoogleGenerativeAI(
model='gemini-1.5-pro',
temperature = 0.5,
streaming=True
)
#creating tools, duckgo search engine
tools = load_tools(
['ddg-search']
)
agent = initialize_agent(
tools=tools,
llm=llm,
agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,
handle_parsing_errors=True
)
#creating instance of GoogleGenerativeAIEmbeddings
embedding = GoogleGenerativeAIEmbeddings(model='models/embedding-001')
#chatgpt-like text prompt
def text_prompt():
if prompt := st.chat_input():
st.chat_message('user').write(prompt)
if prompt == 'exit':
st.stop()
with st.chat_message('assistant'):
st.write('Im thinking...')
st_callback = StreamlitCallbackHandler(st.container())
response = agent.run(prompt, callbacks=[st_callback])
st.write(response)
#chat with PDF
def process_file():
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, # it will divide the text into 800 chunk size each (800 tokens)
chunk_overlap=200,
)
chunks = text_splitter.split_text(text=text)
# st.write(chunks[1])
knowledge_base = FAISS.from_texts(chunks, embedding)
# Accept user questions/query
query = st.text_input("Ask your questions about your PDF file")
#st.write(query)
if query:
docs = knowledge_base.similarity_search(query)
llm = GoogleGenerativeAI(
model='gemini-1.5-pro',
temperature = 0.5,
streaming=True
)
chain = load_qa_chain(llm, chain_type="stuff")
response = chain.run(input_documents=docs, question=query)
st.success(response)
def main():
#side bar contents
with st.sidebar:
#Webpage title
st.title('PERSONAL AI CHATBOT WITH LANGCHAIN')
st.markdown("""
*`Your personal AI Chatbot`*
""")
add_selectbox = st.selectbox(
"Options: ",
("Text", "Document")
)
with st.chat_message('Assistant'):
st.write('Panagdait! How can I help you today? 🤗')
if add_selectbox == 'Text':
text_prompt()
elif add_selectbox == 'Document':
process_file()
if __name__ == '__main__':
main()