Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,63 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
from langchain_groq import ChatGroq
|
4 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
-
from langchain.chains.combine_documents import create_stuff_documents_chain
|
7 |
-
from langchain_core.prompts import ChatPromptTemplate
|
8 |
-
from langchain.chains import create_retrieval_chain
|
9 |
-
from langchain_community.vectorstores import FAISS
|
10 |
-
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
65 |
-
start = time.process_time()
|
66 |
-
response = retrieval_chain.invoke({'input': prompt1})
|
67 |
-
st.write("Response time: ", time.process_time() - start)
|
68 |
-
st.write(response['answer'])
|
69 |
-
|
70 |
-
with st.expander("Document Similarity Search"):
|
71 |
-
for i, doc in enumerate(response["context"]):
|
72 |
-
st.write(doc.page_content)
|
73 |
-
st.write("--------------------------------")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from langchain_groq import ChatGroq
|
4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
6 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
7 |
+
from langchain_core.prompts import ChatPromptTemplate
|
8 |
+
from langchain.chains import create_retrieval_chain
|
9 |
+
from langchain_community.vectorstores import FAISS
|
10 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
11 |
+
import time
|
12 |
+
|
13 |
+
# Retrieve the API keys from environment variables
|
14 |
+
huggingfacehub_api_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
15 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
16 |
+
|
17 |
+
# Set environment variables for Hugging Face
|
18 |
+
os.environ['HUGGINGFACEHUB_API_TOKEN'] = huggingfacehub_api_token
|
19 |
+
|
20 |
+
# Initialize the ChatGroq LLM with the retrieved API key
|
21 |
+
llm = ChatGroq(api_key=groq_api_key, model_name="Llama3-8b-8192")
|
22 |
+
|
23 |
+
st.title("DataScience Chatgroq With Llama3")
|
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.
|
29 |
+
<context>
|
30 |
+
{context}
|
31 |
+
<context>
|
32 |
+
Questions: {input}
|
33 |
+
"""
|
34 |
+
)
|
35 |
+
|
36 |
+
def vector_embedding():
|
37 |
+
if "vectors" not in st.session_state:
|
38 |
+
st.session_state.embeddings = HuggingFaceEmbeddings()
|
39 |
+
st.session_state.loader = PyPDFDirectoryLoader("./Data_Science") # Data Ingestion
|
40 |
+
st.session_state.docs = st.session_state.loader.load() # Document Loading
|
41 |
+
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # Chunk Creation
|
42 |
+
st.session_state.final_documents = st.session_state.text_splitter.split_documents(st.session_state.docs[:20]) # Splitting
|
43 |
+
st.session_state.vectors = FAISS.from_documents(st.session_state.final_documents, st.session_state.embeddings) # Vector HuggingFace embeddings
|
44 |
+
|
45 |
+
prompt1 = st.text_input("Enter Your Question From Documents")
|
46 |
+
|
47 |
+
if st.button("Documents Embedding"):
|
48 |
+
vector_embedding()
|
49 |
+
st.write("Vector Store DB Is Ready")
|
50 |
+
|
51 |
+
if prompt1:
|
52 |
+
document_chain = create_stuff_documents_chain(llm, prompt)
|
53 |
+
retriever = st.session_state.vectors.as_retriever()
|
54 |
+
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
55 |
+
start = time.process_time()
|
56 |
+
response = retrieval_chain.invoke({'input': prompt1})
|
57 |
+
st.write("Response time: ", time.process_time() - start)
|
58 |
+
st.write(response['answer'])
|
59 |
+
|
60 |
+
with st.expander("Document Similarity Search"):
|
61 |
+
for i, doc in enumerate(response["context"]):
|
62 |
+
st.write(doc.page_content)
|
63 |
+
st.write("--------------------------------")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|