Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,20 +9,20 @@ from langchain_community.vectorstores import FAISS
|
|
9 |
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
10 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
11 |
from dotenv import load_dotenv
|
12 |
-
|
13 |
load_dotenv()
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
st.title("Legal Assistant")
|
20 |
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
prompt = ChatPromptTemplate.from_template(
|
26 |
"""
|
27 |
Answer the questions based on the provided context only.
|
28 |
Please provide the most accurate response based on the question
|
@@ -34,49 +34,37 @@ Questions:{input}
|
|
34 |
"""
|
35 |
)
|
36 |
|
37 |
-
# Vector embedding function
|
38 |
def vector_embedding():
|
|
|
39 |
if "vectors" not in st.session_state:
|
40 |
-
|
41 |
-
st.session_state.
|
42 |
-
st.session_state.
|
43 |
-
st.session_state.
|
44 |
-
st.session_state.
|
45 |
-
st.session_state.
|
46 |
-
|
47 |
-
|
48 |
vector_embedding()
|
49 |
|
50 |
-
# Input box for questions
|
51 |
-
prompt1 = st.text_input("Enter Your Question From Documents")
|
52 |
|
53 |
-
# Initialize session state for history if not already present
|
54 |
-
if "chat_history" not in st.session_state:
|
55 |
-
st.session_state.chat_history = []
|
56 |
|
57 |
-
|
58 |
-
with st.sidebar:
|
59 |
-
st.title("Chat History")
|
60 |
-
if st.button("Clear Chat History"):
|
61 |
-
st.session_state.chat_history = []
|
62 |
-
if st.session_state.chat_history:
|
63 |
-
for idx, entry in enumerate(st.session_state.chat_history):
|
64 |
-
if st.button(f"Q{idx + 1}: {entry['question']}"):
|
65 |
-
st.session_state.selected_question = entry['question']
|
66 |
|
67 |
-
# Display selected question from history
|
68 |
-
if "selected_question" in st.session_state:
|
69 |
-
st.write(f"**Selected Question:** {st.session_state.selected_question}")
|
70 |
|
71 |
-
# Process question and generate response
|
72 |
-
if prompt1:
|
73 |
-
document_chain = create_stuff_documents_chain(llm, prompt)
|
74 |
-
retriever = st.session_state.vectors.as_retriever()
|
75 |
-
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
76 |
-
response = retrieval_chain.invoke({'input': prompt1})
|
77 |
|
78 |
-
# Save to history
|
79 |
-
st.session_state.chat_history.append({"question": prompt1, "answer": response['answer']})
|
80 |
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
st.write(response['answer'])
|
|
|
|
|
|
9 |
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
10 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
11 |
from dotenv import load_dotenv
|
12 |
+
import os
|
13 |
load_dotenv()
|
14 |
|
15 |
+
## load the GROQ And OpenAI API
|
16 |
+
|
17 |
+
groq_api_key=os.getenv('groqapi')
|
18 |
+
os.environ["GOOGLE_API_KEY"]=os.getenv("GOOGLE_API_KEY")
|
19 |
|
20 |
st.title("Legal Assistant")
|
21 |
|
22 |
+
llm=ChatGroq(groq_api_key=groq_api_key,
|
23 |
+
model_name="Llama3-8b-8192")
|
24 |
|
25 |
+
prompt=ChatPromptTemplate.from_template(
|
|
|
26 |
"""
|
27 |
Answer the questions based on the provided context only.
|
28 |
Please provide the most accurate response based on the question
|
|
|
34 |
"""
|
35 |
)
|
36 |
|
|
|
37 |
def vector_embedding():
|
38 |
+
|
39 |
if "vectors" not in st.session_state:
|
40 |
+
|
41 |
+
st.session_state.embeddings=GoogleGenerativeAIEmbeddings(model = "models/embedding-001")
|
42 |
+
st.session_state.loader=PyPDFDirectoryLoader("./new") ## Data Ingestion
|
43 |
+
st.session_state.docs=st.session_state.loader.load() ## Document Loading
|
44 |
+
st.session_state.text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200) ## Chunk Creation
|
45 |
+
st.session_state.final_documents=st.session_state.text_splitter.split_documents(st.session_state.docs[:20]) #splitting
|
46 |
+
st.session_state.vectors=FAISS.from_documents(st.session_state.final_documents,st.session_state.embeddings) #vector OpenAI embeddings
|
47 |
+
|
48 |
vector_embedding()
|
49 |
|
|
|
|
|
50 |
|
|
|
|
|
|
|
51 |
|
52 |
+
prompt1=st.text_input("Enter Your Question From Doduments")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
|
|
|
|
|
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
|
|
|
|
56 |
|
57 |
+
import time
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
if prompt1:
|
62 |
+
document_chain=create_stuff_documents_chain(llm,prompt)
|
63 |
+
retriever=st.session_state.vectors.as_retriever()
|
64 |
+
retrieval_chain=create_retrieval_chain(retriever,document_chain)
|
65 |
+
start=time.process_time()
|
66 |
+
response=retrieval_chain.invoke({'input':prompt1})
|
67 |
+
print("Response time :",time.process_time()-start)
|
68 |
st.write(response['answer'])
|
69 |
+
|
70 |
+
|