Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
|
2 |
-
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
model = "gpt-3.5-turbo",
|
16 |
-
messages = messages
|
17 |
-
)
|
18 |
-
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
19 |
-
messages.append({"role": "assistant", "content": ChatGPT_reply})
|
20 |
-
return ChatGPT_reply
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import streamlit as st
|
4 |
+
from langchain_community.vectorstores import FAISS
|
5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
+
from langchain.prompts import PromptTemplate
|
7 |
+
from langchain.memory import ConversationBufferWindowMemory
|
8 |
+
from langchain.chains import ConversationalRetrievalChain
|
9 |
+
from langchain_together import Together
|
10 |
+
from footer import footer # Ensure this module is present in the working directory
|
11 |
|
12 |
+
# Set Streamlit configuration
|
13 |
+
st.set_page_config(page_title="AI Legal App", layout="centered")
|
14 |
|
15 |
+
# Display a logo or banner (replace with a local image or URL)
|
16 |
+
col1, col2, col3 = st.columns([1, 30, 1])
|
17 |
+
with col2:
|
18 |
+
st.image("https://github.com/Nike-one/BharatLAW/blob/master/images/banner.png?raw=true", use_column_width=True)
|
19 |
|
20 |
+
def hide_hamburger_menu():
|
21 |
+
st.markdown("""
|
22 |
+
<style>
|
23 |
+
#MainMenu {visibility: hidden;}
|
24 |
+
footer {visibility: hidden;}
|
25 |
+
</style>
|
26 |
+
""", unsafe_allow_html=True)
|
27 |
|
28 |
+
hide_hamburger_menu()
|
29 |
|
30 |
+
# Initialize session state
|
31 |
+
if "messages" not in st.session_state:
|
32 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
if "memory" not in st.session_state:
|
35 |
+
st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history", return_messages=True)
|
36 |
+
|
37 |
+
@st.cache_resource
|
38 |
+
def load_embeddings():
|
39 |
+
"""Load and cache the embeddings model."""
|
40 |
+
return HuggingFaceEmbeddings(model_name="law-ai/InLegalBERT")
|
41 |
+
|
42 |
+
embeddings = load_embeddings()
|
43 |
+
db = FAISS.load_local("ipc_embed_db", embeddings, allow_dangerous_deserialization=True)
|
44 |
+
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})
|
45 |
+
|
46 |
+
prompt_template = """
|
47 |
+
<s>[INST]
|
48 |
+
As a legal chatbot specializing in Indian law, your responses must be concise and accurate:
|
49 |
+
- Provide bullet points summarizing key legal aspects.
|
50 |
+
- Avoid assumptions or overly specific advice unless requested.
|
51 |
+
- Clarify any common misconceptions.
|
52 |
+
- Keep responses aligned with general legal principles.
|
53 |
+
CONTEXT: {context}
|
54 |
+
CHAT HISTORY: {chat_history}
|
55 |
+
QUESTION: {question}
|
56 |
+
ANSWER:
|
57 |
+
</s>[INST]
|
58 |
+
"""
|
59 |
+
|
60 |
+
prompt = PromptTemplate(template=prompt_template,
|
61 |
+
input_variables=['context', 'question', 'chat_history'])
|
62 |
+
|
63 |
+
api_key = os.getenv('TOGETHER_API_KEY')
|
64 |
+
llm = Together(model="mistralai/Mixtral-8x22B-Instruct-v0.1", temperature=0.5, max_tokens=1024, together_api_key=api_key)
|
65 |
+
|
66 |
+
qa = ConversationalRetrievalChain.from_llm(llm=llm, memory=st.session_state.memory, retriever=db_retriever, combine_docs_chain_kwargs={'prompt': prompt})
|
67 |
+
|
68 |
+
def extract_answer(full_response):
|
69 |
+
"""Extracts the assistant's answer from the response."""
|
70 |
+
return full_response.strip()
|
71 |
+
|
72 |
+
def reset_conversation():
|
73 |
+
st.session_state.messages = []
|
74 |
+
st.session_state.memory.clear()
|
75 |
+
|
76 |
+
for message in st.session_state.messages:
|
77 |
+
with st.chat_message(message["role"]):
|
78 |
+
st.write(message["content"])
|
79 |
+
|
80 |
+
input_prompt = st.chat_input("Ask your legal query...")
|
81 |
+
if input_prompt:
|
82 |
+
with st.chat_message("user"):
|
83 |
+
st.markdown(f"**You:** {input_prompt}")
|
84 |
+
|
85 |
+
st.session_state.messages.append({"role": "user", "content": input_prompt})
|
86 |
+
with st.chat_message("assistant"):
|
87 |
+
with st.spinner("Analyzing..."):
|
88 |
+
result = qa.invoke(input=input_prompt)
|
89 |
+
message_placeholder = st.empty()
|
90 |
+
answer = extract_answer(result["answer"])
|
91 |
+
|
92 |
+
# Simulated typing effect
|
93 |
+
response = ""
|
94 |
+
for char in answer:
|
95 |
+
response += char
|
96 |
+
time.sleep(0.02)
|
97 |
+
message_placeholder.markdown(response + " |", unsafe_allow_html=True)
|
98 |
+
|
99 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|
100 |
+
|
101 |
+
if st.button('🗑️ Reset Chat', on_click=reset_conversation):
|
102 |
+
st.experimental_rerun()
|
103 |
+
|
104 |
+
footer()
|