Pushkar7779 commited on
Commit
ea75dbb
1 Parent(s): f717406

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +87 -0
  2. htmlTemplates.py +44 -0
  3. requirements.txt +11 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.chat_models import ChatOpenAI
6
+ from langchain.vectorstores import Qdrant
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
+ from langchain_community.embeddings.fastembed import FastEmbedEmbeddings
13
+ import streamlit as st
14
+
15
+
16
+ def get_vector_store():
17
+
18
+ client = qdrant_client.QdrantClient(
19
+ os.getenv("QDRANT_HOST"),
20
+ api_key=os.getenv("QDRANT_API_KEY")
21
+ )
22
+
23
+ embeddings = embeddings = FastEmbedEmbeddings(model_name="BAAI/bge-base-en-v1.5")
24
+
25
+ vector_store = Qdrant(
26
+ client=client,
27
+ collection_name="PenalCode",
28
+ embeddings=embeddings,
29
+ )
30
+
31
+ return vector_store
32
+
33
+ def get_conversation_chain(vectorstore):
34
+ llm = ChatOpenAI()
35
+ #llm = HuggingFaceHub( repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
36
+ memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
37
+ conversation_chain = ConversationalRetrievalChain.from_llm(
38
+ llm =llm,
39
+ retriever=vectorstore.as_retriever(),
40
+ memory=memory
41
+ )
42
+ return conversation_chain
43
+
44
+ def handle_userinput(user_question):
45
+ response = st.session_state.conversation({'question':user_question})
46
+ st.session_state.chat_history = response['chat_history']
47
+
48
+ for i, message in enumerate(st.session_state.chat_history):
49
+ if i%2 == 0:
50
+ st.write(user_template.replace("{{MSG}}",message.content), unsafe_allow_html=True)
51
+ else:
52
+ st.write(bot_template.replace("{{MSG}}",message.content), unsafe_allow_html=True)
53
+
54
+ def main():
55
+ load_dotenv()
56
+
57
+ st.set_page_config(page_title="Legal Assistant", page_icon=":robot_face:")
58
+ st.write(css, unsafe_allow_html=True)
59
+
60
+ st.markdown("<h1 style='text-align: center; color: red;font-family:Georgia'>AI Lawyer Bot 🤖</h1>", unsafe_allow_html=True)
61
+ st.subheader("\"_Is that legal❓_\"")
62
+ st.write("This bot is made to answer all your legal queries in the context of the Indian Penal Code.")
63
+ with st.expander("**Disclamer**"):
64
+ st.write("1. **This is not legal advice**.")
65
+ st.write("2. While the model has the context of the IPC it has not been fine-tuned and hence may not be able to answer all your queries. ")
66
+ st.divider()
67
+ st.caption("Try something like \"What is the punishment for criminal intimidation?\" or \"How is theft defined in the IPC?\"")
68
+
69
+
70
+ if "conversation" not in st.session_state:
71
+ st.session_state.conversation = None
72
+ if "chat_history" not in st.session_state:
73
+ st.session_state.chat_history = None
74
+
75
+ # create vector store
76
+ vector_store = get_vector_store()
77
+
78
+ st.session_state.conversation = get_conversation_chain(vector_store)
79
+
80
+ user_question = st.text_input("Ask your questions here:")
81
+ if user_question:
82
+ handle_userinput(user_question)
83
+
84
+
85
+
86
+ if __name__ == '__main__':
87
+ 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/L022b4R/bot.webp" alt="bot" 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/QML4rqV/user.jpg" alt="user" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;">
41
+ </div>
42
+ <div class="message">{{MSG}}</div>
43
+ </div>
44
+ '''
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ python-dotenv
3
+ streamlit
4
+ qdrant-client
5
+ tiktoken
6
+ huggingface-hub==0.14.1
7
+ InstructorEmbedding==1.0.1
8
+ sentence-transformers==2.2.2
9
+ langchain_community
10
+ fastembed
11
+ openai