1MR commited on
Commit
cae2ea7
·
verified ·
1 Parent(s): 08c527d

Upload 4 files

Browse files
Files changed (4) hide show
  1. htmlTemplates.py +44 -0
  2. python-version.txt +1 -0
  3. rag_with_pdf.py +99 -0
  4. requirements.txt +14 -0
htmlTemplates.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ css = '''
2
+ <style>
3
+ .chat-message {
4
+ padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
5
+ }
6
+ .chat-message.user {
7
+ background-color: #2b313e
8
+ }
9
+ .chat-message.bot {
10
+ background-color: #475063
11
+ }
12
+ .chat-message .avatar {
13
+ width: 20%;
14
+ }
15
+ .chat-message .avatar img {
16
+ max-width: 78px;
17
+ max-height: 78px;
18
+ border-radius: 50%;
19
+ object-fit: cover;
20
+ }
21
+ .chat-message .message {
22
+ width: 80%;
23
+ padding: 0 1.5rem;
24
+ color: #fff;
25
+ }
26
+ '''
27
+
28
+ bot_template = '''
29
+ <div class="chat-message bot">
30
+ <div class="avatar">
31
+ <img src="https://i.ibb.co/cN0nmSj/Screenshot-2023-05-28-at-02-37-21.png" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;">
32
+ </div>
33
+ <div class="message">{{MSG}}</div>
34
+ </div>
35
+ '''
36
+
37
+ user_template = '''
38
+ <div class="chat-message user">
39
+ <div class="avatar">
40
+ <img src="https://i.ibb.co/rdZC7LZ/Photo-logo-1.png">
41
+ </div>
42
+ <div class="message">{{MSG}}</div>
43
+ </div>
44
+ '''
python-version.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ 3.9
rag_with_pdf.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """RAG With PDF.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1FVkw8Ozi4IN97pMN-vat02c3QHQoDkEJ
8
+ """
9
+
10
+ !pip install streamlit PyPDF2 langchain-community
11
+
12
+ import streamlit as st
13
+ # from dotenv import load_dotenv
14
+ from PyPDF2 import PdfReader
15
+ from langchain.text_splitter import CharacterTextSplitter
16
+ from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
17
+ from langchain.vectorstores import FAISS
18
+ from langchain.chat_models import ChatOpenAI
19
+ from langchain.memory import ConversationBufferMemory
20
+ from langchain.chains import ConversationalRetrievalChain
21
+ from htmlTemplates import css, bot_template, user_template
22
+ from langchain.llms import HuggingFaceHub
23
+
24
+ def get_pdf_text(pdf_docs):
25
+ text = ""
26
+ for pdf in pdf_docs:
27
+ pdf_reader = PdfReader(pdf)
28
+ for page in pdf_reader.pages:
29
+ text += page.extract_text()
30
+ return text
31
+
32
+ def get_text_chunks(text):
33
+ text_splitter=CharacterTextSplitter(
34
+ separator="\n",
35
+ chunks=1000,
36
+ chunk_overlap=200,
37
+ length_function=len
38
+ )
39
+ chunks=text_splitter.split_text(text)
40
+ return chunks
41
+
42
+ def get_vectorstore(text_chunks):
43
+ embeddings=HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
44
+ vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
45
+ return vectorstore
46
+
47
+ def get_conversation_chain(vectorstore):
48
+ llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
49
+ memory=ConversationBufferMemory(
50
+ memory_key='chat_history',return_messages=True)
51
+ conversation_chain = ConversationalRetrievalChain.from_llm(
52
+ llm=llm,
53
+ retriever=vectorstore.as_retriever(),
54
+ memory=memory
55
+ )
56
+ return conversation_chain
57
+
58
+ def handle_userinput(user_question):
59
+ response = st.session_state.conversation({'question':user_question})
60
+ st.session_state.chat_history = response['chat_history']
61
+
62
+ for i, message in enumerate(st.session_state.chat_history):
63
+ if i % 2 == 0:
64
+ st.write(user_template.replace("{{MSG}}", message.content),unsafe_allow_html=True)
65
+ else:
66
+ st.write(bot_template.replace("{{MSG}}", message.content),unsafe_allow_html=True)
67
+
68
+ def main():
69
+ st.set_page_config(page_title="Chat with My RAG",
70
+ page_icon=":books:")
71
+ st.write(css,unsafe_allow_html=True)
72
+
73
+ if "conversation" not in st.session_state:
74
+ st.session_state.conversation = None
75
+ else:
76
+ st.session_state.chat_history = None
77
+
78
+ st.header("Chat with My RAG :books:")
79
+ user_question=st.text_input("Ask a question about your documents:")
80
+ if user_question:
81
+ handle_userinput(user_question)
82
+
83
+ with st.sidebar:
84
+ st.subheader("Your Documents")
85
+ pdf_docs = st.file_uploader("Upload your PDFs here and click on 'Process'", accept_multiple_files=True)
86
+ if st.button("Process"):
87
+ with st.spinner("Processing"):
88
+ raw_text =get_pdf_text(pdf_docs)
89
+
90
+ text_chunks = get_text_chunks(raw_text)
91
+
92
+ vectorstore = get_vectorstore(text_chunks)
93
+
94
+ st.session_state.conversation = get_conversation_chain(vectorstore)
95
+
96
+
97
+ if __name__ == '__main__':
98
+ main()
99
+
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain==0.0.184
2
+ PyPDF2==3.0.1
3
+ python-dotenv==1.0.0
4
+ streamlit==1.18.1
5
+ openai==0.27.6
6
+ faiss-cpu==1.7.4
7
+ altair==4
8
+ tiktoken==0.4.0
9
+ # uncomment to use huggingface llms
10
+ # huggingface-hub==0.14.1
11
+
12
+ # uncomment to use instructor embeddings
13
+ # InstructorEmbedding==1.0.1
14
+ # sentence-transformers==2.2.2