Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,9 @@
|
|
|
|
1 |
from dataclasses import dataclass
|
2 |
from typing import Literal
|
3 |
import streamlit as st
|
4 |
-
from
|
5 |
-
from
|
6 |
-
from langchain.prompts import PromptTemplate
|
7 |
-
from pinecone import Pinecone #, ServerlessSpec
|
8 |
-
from langchain_community.chat_message_histories import ChatMessageHistory
|
9 |
-
from langchain.memory import ConversationBufferMemory
|
10 |
-
from langchain.chains import ConversationalRetrievalChain
|
11 |
-
from dotenv import load_dotenv
|
12 |
-
import os
|
13 |
-
|
14 |
-
# Load environment variables from the .env file
|
15 |
-
load_dotenv()
|
16 |
|
17 |
@dataclass
|
18 |
class Message:
|
@@ -20,81 +11,31 @@ class Message:
|
|
20 |
origin: Literal["π€ Human", "π¨π»ββοΈ Ai"]
|
21 |
message: str
|
22 |
|
23 |
-
|
24 |
-
def download_hugging_face_embeddings():
|
25 |
-
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
26 |
-
return embeddings
|
27 |
-
|
28 |
-
|
29 |
def initialize_session_state():
|
|
|
30 |
if "history" not in st.session_state:
|
31 |
st.session_state.history = []
|
32 |
if "conversation" not in st.session_state:
|
33 |
-
|
34 |
-
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
|
35 |
-
index = pc.Index("il-legal")
|
36 |
-
docsearch = PineconeVectorStore.from_existing_index(index_name="il-legal", embedding=embeddings)
|
37 |
-
|
38 |
-
repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
39 |
-
llm = HuggingFaceEndpoint(
|
40 |
-
repo_id=repo_id,
|
41 |
-
model_kwargs={"huggingface_api_token":os.getenv("HUGGINGFACEHUB_API_TOKEN")},
|
42 |
-
temperature=0.5,
|
43 |
-
top_k=10,
|
44 |
-
)
|
45 |
-
|
46 |
-
prompt_template = """
|
47 |
-
You are a trained bot to guide people about Illinois Crimnal Law Statutes and the Safe-T Act. You will answer user's query with your knowledge and the context provided.
|
48 |
-
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
49 |
-
Do not say thank you and tell you are an AI Assistant and be open about everything.
|
50 |
-
Use the following pieces of context to answer the users question.
|
51 |
-
Context: {context}
|
52 |
-
Question: {question}
|
53 |
-
Only return the helpful answer below and nothing else.
|
54 |
-
Helpful answer:
|
55 |
-
"""
|
56 |
-
|
57 |
-
PROMPT = PromptTemplate(
|
58 |
-
template=prompt_template,
|
59 |
-
input_variables=["context", "question"])
|
60 |
-
|
61 |
-
#chain_type_kwargs = {"prompt": PROMPT}
|
62 |
-
message_history = ChatMessageHistory()
|
63 |
-
memory = ConversationBufferMemory(
|
64 |
-
memory_key="chat_history",
|
65 |
-
output_key="answer",
|
66 |
-
chat_memory=message_history,
|
67 |
-
return_messages=True,
|
68 |
-
)
|
69 |
-
retrieval_chain = ConversationalRetrievalChain.from_llm(
|
70 |
-
llm=llm,
|
71 |
-
chain_type="stuff",
|
72 |
-
retriever=docsearch.as_retriever(
|
73 |
-
search_kwargs={
|
74 |
-
'filter': {'source': 'user_id'},
|
75 |
-
}),
|
76 |
-
return_source_documents=True,
|
77 |
-
combine_docs_chain_kwargs={"prompt": PROMPT},
|
78 |
-
memory= memory
|
79 |
-
)
|
80 |
-
|
81 |
st.session_state.conversation = retrieval_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
def on_click_callback():
|
85 |
-
human_prompt = st.session_state.human_prompt
|
86 |
-
st.session_state.human_prompt=""
|
87 |
-
response = st.session_state.conversation(
|
88 |
-
human_prompt
|
89 |
-
)
|
90 |
-
llm_response = response['answer']
|
91 |
-
st.session_state.history.append(
|
92 |
-
Message("π€ Human", human_prompt)
|
93 |
-
)
|
94 |
-
st.session_state.history.append(
|
95 |
-
Message("π¨π»ββοΈ Ai", llm_response)
|
96 |
-
)
|
97 |
-
|
98 |
|
99 |
initialize_session_state()
|
100 |
|
@@ -109,7 +50,7 @@ st.markdown(
|
|
109 |
|
110 |
- Answer questions on various aspects of Illinois criminal law.
|
111 |
- Guide you through legal processes relevant to Illinois.
|
112 |
-
- Provide information on your rights and responsibilities
|
113 |
|
114 |
βοΈ **Disclaimer:**
|
115 |
|
@@ -117,29 +58,52 @@ st.markdown(
|
|
117 |
|
118 |
π€ **Getting Started:**
|
119 |
|
120 |
-
Feel free to ask any legal question related to Illinois law
|
|
|
121 |
Let's get started! How may I help you today?
|
122 |
"""
|
123 |
)
|
124 |
|
125 |
chat_placeholder = st.container()
|
126 |
-
prompt_placeholder = st.form("chat-form")
|
127 |
|
128 |
with chat_placeholder:
|
129 |
for chat in st.session_state.history:
|
130 |
st.markdown(f"{chat.origin} : {chat.message}")
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
|
|
|
|
|
1 |
+
import logging
|
2 |
from dataclasses import dataclass
|
3 |
from typing import Literal
|
4 |
import streamlit as st
|
5 |
+
from utils import process
|
6 |
+
from chat.bot import ChatBot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
@dataclass
|
9 |
class Message:
|
|
|
11 |
origin: Literal["π€ Human", "π¨π»ββοΈ Ai"]
|
12 |
message: str
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def initialize_session_state():
|
15 |
+
"""Initialize session state variables."""
|
16 |
if "history" not in st.session_state:
|
17 |
st.session_state.history = []
|
18 |
if "conversation" not in st.session_state:
|
19 |
+
retrieval_chain, chroma_collection, langchain_chroma = ChatBot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
st.session_state.conversation = retrieval_chain
|
21 |
+
st.session_state.chroma_collection = chroma_collection
|
22 |
+
st.session_state.langchain_chroma = langchain_chroma
|
23 |
+
|
24 |
+
def on_submit(user_input):
|
25 |
+
"""Handle user input and generate response."""
|
26 |
+
if user_input:
|
27 |
+
response = st.session_state.conversation({
|
28 |
+
"question":user_input
|
29 |
+
})
|
30 |
+
llm_response = response['answer']
|
31 |
+
st.session_state.history.append(
|
32 |
+
Message("π£οΈ Human", user_input)
|
33 |
+
)
|
34 |
+
st.session_state.history.append(
|
35 |
+
Message("π§ββοΈ AI Lawyer", llm_response)
|
36 |
+
)
|
37 |
|
38 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
initialize_session_state()
|
41 |
|
|
|
50 |
|
51 |
- Answer questions on various aspects of Illinois criminal law.
|
52 |
- Guide you through legal processes relevant to Illinois.
|
53 |
+
- Provide information on your rights and responsibilities regarding Illinois legal standards.
|
54 |
|
55 |
βοΈ **Disclaimer:**
|
56 |
|
|
|
58 |
|
59 |
π€ **Getting Started:**
|
60 |
|
61 |
+
Feel free to ask any legal question related to Illinois criminal law. I'm here to assist you!
|
62 |
+
If you have any documents pertinent to your case to better assist you, please upload them below.
|
63 |
Let's get started! How may I help you today?
|
64 |
"""
|
65 |
)
|
66 |
|
67 |
chat_placeholder = st.container()
|
|
|
68 |
|
69 |
with chat_placeholder:
|
70 |
for chat in st.session_state.history:
|
71 |
st.markdown(f"{chat.origin} : {chat.message}")
|
72 |
|
73 |
+
user_question = st.chat_input("Enter your question here...")
|
74 |
+
|
75 |
+
# File upload and processing
|
76 |
+
uploaded_file = st.file_uploader("Upload your legal document", type="pdf")
|
77 |
+
|
78 |
+
if uploaded_file is not None:
|
79 |
+
try:
|
80 |
+
uploaded_file.seek(0)
|
81 |
+
text = process.extract_text_from_pdf(uploaded_file)
|
82 |
+
chunks = process.chunk_text(text)
|
83 |
+
st.session_state.user_chunks = chunks
|
84 |
+
st.success(f"Uploaded {uploaded_file.name} successfully with {len(chunks)} chunks")
|
85 |
+
|
86 |
+
# Add chunks to Chroma
|
87 |
+
ids = [f"doc_{i}" for i in range(len(chunks))]
|
88 |
+
metadatas = [{"source": "user_upload"} for _ in chunks] #range(len(chunks))],
|
89 |
+
st.session_state.chroma_collection.add(
|
90 |
+
documents=chunks,
|
91 |
+
ids=ids,
|
92 |
+
metadatas=metadatas
|
93 |
+
)
|
94 |
+
|
95 |
+
# Add chunks to LangChain Chroma wrapper
|
96 |
+
st.session_state.langchain_chroma.add_texts(
|
97 |
+
texts=chunks,
|
98 |
+
metadatas=metadatas
|
99 |
+
)
|
100 |
+
|
101 |
+
st.success("Document processed and vectorized successfully!")
|
102 |
+
|
103 |
+
except Exception as e:
|
104 |
+
logging.exception(f"An error occurred while processing {uploaded_file.name}: {str(e)}")
|
105 |
+
st.error(f"An error occurred while processing {uploaded_file.name}: {str(e)}")
|
106 |
+
|
107 |
|
108 |
+
if user_question:
|
109 |
+
on_submit(user_question)
|