|
from langchain.document_loaders import PyPDFLoader |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain_community.embeddings.huggingface import HuggingFaceEmbeddings |
|
from langchain_community.vectorstores import Chroma |
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
from langchain.prompts import ChatPromptTemplate |
|
from langchain_core.output_parsers import StrOutputParser |
|
from langchain_core.runnables import RunnablePassthrough |
|
import streamlit as st |
|
|
|
|
|
pdf = PyPDFLoader("quran-in-modern-english.pdf") |
|
data = pdf.load() |
|
rs = RecursiveCharacterTextSplitter(chunk_size=1313, chunk_overlap=200) |
|
splits = rs.split_documents(data) |
|
|
|
|
|
em = HuggingFaceEmbeddings() |
|
|
|
|
|
vectordb = Chroma.from_documents(documents=splits, embedding=em) |
|
|
|
|
|
llm = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0, google_api_key="AIzaSyBG8UJFmZnGq417gnWyoA-5mrTKBn1D1r0") |
|
|
|
|
|
template = """ |
|
You are a helpful assistant to answer the queries. If you don't get an answer from the context, generate it by yourself. Use this context to answer the user's question. |
|
|
|
Context: {context} |
|
|
|
Question: {question} |
|
""" |
|
prompt_template = ChatPromptTemplate.from_template(template) |
|
|
|
|
|
retriever = vectordb.as_retriever() |
|
|
|
|
|
rag_chain = ( |
|
{"context": retriever, "question": RunnablePassthrough()} |
|
| prompt_template |
|
| llm |
|
| StrOutputParser() |
|
) |
|
|
|
|
|
st.title("Quran Query Answering Bot") |
|
st.write("Ask your query, you will get an answer from the context of the Quran.") |
|
|
|
user_query = st.text_input("Enter your query:") |
|
|
|
if user_query: |
|
answer = rag_chain.invoke({"question": user_query}) |
|
st.write("Answer:", answer) |
|
|