Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings, ChatNVIDIA
|
4 |
+
from langchain_community.document_loaders import WebBaseLoader
|
5 |
+
from langchain.embeddings import OllamaEmbeddings
|
6 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
7 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
8 |
+
from langchain_core.prompts import ChatPromptTemplate
|
9 |
+
from langchain_core.output_parsers import StrOutputParser
|
10 |
+
from langchain.chains import create_retrieval_chain
|
11 |
+
from langchain_community.vectorstores import FAISS
|
12 |
+
from langchain_community.document_loaders import PyPDFDirectoryLoader
|
13 |
+
import time
|
14 |
+
import requests
|
15 |
+
import os
|
16 |
+
from dotenv import load_dotenv
|
17 |
+
|
18 |
+
load_dotenv()
|
19 |
+
|
20 |
+
## load the Groq API key
|
21 |
+
os.environ['NVIDIA_API_KEY'] = os.environ.get('api_key')
|
22 |
+
|
23 |
+
def vector_embedding():
|
24 |
+
if "vectors" not in st.session_state:
|
25 |
+
st.session_state.embeddings = NVIDIAEmbeddings()
|
26 |
+
st.session_state.loader = PyPDFDirectoryLoader("./documents") # Data Ingestion
|
27 |
+
st.session_state.docs = st.session_state.loader.load() # Document Loading
|
28 |
+
st.session_state.text_splitter = RecursiveCharacterTextSplitter(chunk_size=700, chunk_overlap=50) # Chunk Creation
|
29 |
+
st.session_state.final_documents = st.session_state.text_splitter.split_documents(st.session_state.docs) # Splitting
|
30 |
+
print("hEllo")
|
31 |
+
st.session_state.vectors = FAISS.from_documents(st.session_state.final_documents, st.session_state.embeddings) # Vector OpenAI embeddings
|
32 |
+
|
33 |
+
st.title("Ayurvedic Chatbot using Nvidia NIM")
|
34 |
+
llm = ChatNVIDIA(model="meta/llama3-70b-instruct")
|
35 |
+
|
36 |
+
prompt = ChatPromptTemplate.from_template(
|
37 |
+
"""
|
38 |
+
Answer the questions based on the provided context only.
|
39 |
+
Please provide the most accurate response based on the question.
|
40 |
+
Give a detailed answer for the question.
|
41 |
+
<context>
|
42 |
+
{context}
|
43 |
+
<context>
|
44 |
+
Questions:{input}
|
45 |
+
"""
|
46 |
+
)
|
47 |
+
|
48 |
+
prompt1 = st.text_input("Enter Your Question From related to Ayurvedic Herbs?")
|
49 |
+
|
50 |
+
if st.button("Documents Embedding"):
|
51 |
+
vector_embedding()
|
52 |
+
st.write("Vector Store DB Is Ready")
|
53 |
+
|
54 |
+
if prompt1:
|
55 |
+
# Ensure vectors are initialized before proceeding
|
56 |
+
if "vectors" not in st.session_state:
|
57 |
+
st.warning("Please embed the documents first by clicking the 'Documents Embedding' button.")
|
58 |
+
else:
|
59 |
+
document_chain = create_stuff_documents_chain(llm, prompt)
|
60 |
+
retriever = st.session_state.vectors.as_retriever()
|
61 |
+
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
62 |
+
start = time.process_time()
|
63 |
+
|
64 |
+
try:
|
65 |
+
response = retrieval_chain.invoke({'input': prompt1})
|
66 |
+
except requests.exceptions.SSLError as e:
|
67 |
+
st.error("SSL error occurred: {}".format(e))
|
68 |
+
response = None
|
69 |
+
|
70 |
+
if response:
|
71 |
+
print("Response time:", time.process_time() - start)
|
72 |
+
st.write(response['answer'])
|
73 |
+
|
74 |
+
# With a streamlit expander
|
75 |
+
with st.expander("Document Similarity Search"):
|
76 |
+
# Find the relevant chunks
|
77 |
+
for i, doc in enumerate(response["context"]):
|
78 |
+
st.write(doc.page_content)
|
79 |
+
st.write("--------------------------------")
|