pranavnair2 commited on
Commit
15fdfcf
1 Parent(s): c2fcb3c

Upload 3 files

Browse files
Files changed (3) hide show
  1. app1.py +72 -0
  2. htmlTemplates.py +44 -0
  3. requirements.txt +0 -0
app1.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ import streamlit as st
3
+ from langchain.chains import RetrievalQA
4
+ from langchain.llms import HuggingFaceHub
5
+ from langchain.vectorstores import Qdrant
6
+ from langchain.embeddings import HuggingFaceInstructEmbeddings
7
+ import qdrant_client
8
+ import os
9
+ from langchain.memory import ConversationBufferMemory
10
+ from langchain.chains import ConversationalRetrievalChain
11
+ from htmlTemplates import css, user_template, bot_template
12
+
13
+ def get_vector_store():
14
+
15
+ client = qdrant_client.QdrantClient(
16
+ os.getenv("QDRANT_HOST"),
17
+ api_key=os.getenv("QDRANT_API_KEY")
18
+ )
19
+
20
+ embeddings =HuggingFaceInstructEmbeddings(model_name = "hkunlp/instructor-xl")
21
+
22
+ vector_store = Qdrant(
23
+ client=client,
24
+ collection_name="penalCode",
25
+ embeddings=embeddings,
26
+ )
27
+
28
+ return vector_store
29
+
30
+ def get_conversation_chain(vectorstore):
31
+ #llm = ChatOpenAI()
32
+ llm = HuggingFaceHub( repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
33
+ memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
34
+ conversation_chain = ConversationalRetrievalChain.from_llm(
35
+ llm =llm,
36
+ retriever=vectorstore.as_retriever(),
37
+ memory=memory
38
+ )
39
+ return conversation_chain
40
+
41
+ def handle_userinput(user_question):
42
+ response = st.session_state.conversation({'question':user_question})
43
+ st.write(response)
44
+
45
+
46
+ def main():
47
+ load_dotenv()
48
+
49
+ st.set_page_config(page_title="PDF Assistant", page_icon=":books:")
50
+ st.write(css, unsafe_allow_html=True)
51
+ st.header("Is that legal?")
52
+
53
+ if "conversation" not in st.session_state:
54
+ st.session_state.conversation = None
55
+ # create vector store
56
+ vector_store = get_vector_store()
57
+
58
+ if st.button("Start!"):
59
+ with st.spinner("Processing"):
60
+ st.session_state.conversation = get_conversation_chain(vector_store)
61
+ user_question = st.text_input("Ask your questions here:")
62
+
63
+ if user_question:
64
+ handle_userinput(user_question)
65
+
66
+ st.write(user_template.replace("{{MSG}}","Hello Bot"), unsafe_allow_html=True)
67
+ st.write(bot_template.replace("{{MSG}}","Hello Bot"), unsafe_allow_html=True)
68
+
69
+
70
+
71
+ if __name__ == '__main__':
72
+ main()
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
+ '''
requirements.txt ADDED
File without changes