|
import streamlit as st |
|
from PyPDF2 import PdfReader |
|
from dotenv import load_dotenv |
|
from langchain.embeddings.openai import OpenAIEmbeddings |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain.vectorstores import FAISS |
|
from langchain.chains.question_answering import load_qa_chain |
|
from langchain.llms import OpenAI |
|
import os |
|
|
|
load_dotenv() |
|
|
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") |
|
SERPAPI_API_KEY = os.getenv("SERPAPI_API_KEY") |
|
|
|
|
|
pdfreader = PdfReader('XYZ_contract_pdf_Sumit Yenugwar.pdf') |
|
raw_text = '' |
|
for i, page in enumerate(pdfreader.pages): |
|
content = page.extract_text() |
|
if content: |
|
raw_text += content |
|
|
|
|
|
text_splitter = CharacterTextSplitter( |
|
separator="\n", |
|
chunk_size=800, |
|
chunk_overlap=200, |
|
length_function=len, |
|
) |
|
texts = text_splitter.split_text(raw_text) |
|
|
|
|
|
embeddings = OpenAIEmbeddings() |
|
|
|
|
|
document_search = FAISS.from_texts(texts, embeddings) |
|
|
|
|
|
document_search.save_local("faiss_index") |
|
|
|
|
|
new_document_search = FAISS.load_local("faiss_index", embeddings) |
|
|
|
|
|
|
|
|
|
|
|
chain = load_qa_chain(OpenAI(), chain_type="stuff") |
|
|
|
with st.sidebar: |
|
st.title('π€π¬ LLM Chat APP') |
|
st.markdown(''' |
|
## About |
|
This app is an LLM-powered chatbot built using: |
|
- [Streamlit](https://streamlit.io/) |
|
- [LangChain](https://python.langchain.com/) |
|
- [OpenAI](https://platform.openai.com/docs/models) LLM model |
|
|
|
''') |
|
st.markdown("<br>", unsafe_allow_html=True) |
|
st.write('Made with β€οΈ by [Prompt Engineer](https://www.youtube.com/watch?v=M4mc-z_K1NU&list=PLUTApKyNO6MwrOioHGaFCeXtZpchoGv6W)') |
|
|
|
|
|
def main(): |
|
st.title("DAMA-Data Management body of knowledge") |
|
|
|
|
|
user_input = st.text_area("Enter your MCQ question ",height=150) |
|
|
|
|
|
if st.button("Get Answer"): |
|
|
|
prompt_query = f"you have provided with MCQ question and its option as a chatbot model: {user_input}" |
|
text_query = prompt_query + user_input |
|
|
|
|
|
docs = new_document_search.similarity_search(text_query) |
|
|
|
|
|
model_answer = chain.run(input_documents=docs, question=user_input) |
|
|
|
|
|
st.text_area("Model Answer:", value=model_answer) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |
|
|