|
from langchain.embeddings import HuggingFaceBgeEmbeddings |
|
from langchain.document_loaders import PyPDFLoader, DirectoryLoader |
|
from langchain.vectorstores import Chroma |
|
from langchain.chains import RetrievalQA |
|
from langchain.prompts import PromptTemplate |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain_groq import ChatGroq |
|
import os |
|
import gradio as gr |
|
|
|
def initialize_llm(): |
|
"""Initialize the Groq LLM""" |
|
llm = ChatGroq( |
|
temperature=0, |
|
groq_api_key="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", |
|
model_name="llama-3.3-70b-versatile" |
|
) |
|
return llm |
|
|
|
def create_vector_db(): |
|
"""Create and populate the vector database from PDF files""" |
|
print("Creating vector database...") |
|
|
|
loader = DirectoryLoader("/content/bg/", glob='*.pdf', loader_cls=PyPDFLoader) |
|
documents = loader.load() |
|
|
|
print(f"Loaded {len(documents)} documents") |
|
|
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) |
|
texts = text_splitter.split_documents(documents) |
|
|
|
print(f"Split into {len(texts)} text chunks") |
|
|
|
embeddings = HuggingFaceBgeEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') |
|
vector_db = Chroma.from_documents(texts, embeddings, persist_directory='./chroma_db') |
|
vector_db.persist() |
|
|
|
print("ChromaDB created and data saved") |
|
|
|
return vector_db |
|
|
|
def setup_qa_chain(vector_db, llm): |
|
"""Set up the question-answering chain""" |
|
retriever = vector_db.as_retriever(search_kwargs={"k": 5}) |
|
|
|
prompt_template = """You are a knowledgeable spiritual guide specialized in the Bhagavad Gita, an ancient Hindu scripture. |
|
Your task is to answer questions based on the relevant verses from the Bhagavad Gita in Sanskrit and Hindi only. If the user ask for translation than only answer in English. |
|
|
|
CONTEXT INFORMATION: |
|
{context} |
|
|
|
USER QUESTION: |
|
{question} |
|
|
|
Please provide a comprehensive answer to the user's question by following these guidelines: |
|
1. Format your response with clear headings (use ## for main sections, ### for subsections) and paragraphs for readability. |
|
2. Always cite the specific chapter and verse numbers (e.g., Chapter 2, Verse 47) when referencing the scripture. |
|
3. When quoting Sanskrit verses, clearly mark them with "Sanskrit:" and use quotation marks. |
|
4. For longer answers, structure your response with these sections: |
|
- Direct Answer (a concise response to the question) |
|
- Relevant Verses (the key verses that address the question) |
|
- Explanation (deeper analysis of the verses) |
|
- Modern Application (if appropriate only based on the context and nothing from the Internet) |
|
5. Use bullet points for listing multiple related concepts. |
|
6. If multiple verses are relevant, organize them logically to show their relationship. |
|
7. When appropriate, include a brief conclusion that summarizes the key insights. |
|
8. If the context doesn't fully answer the question, acknowledge this and provide the best answer based on the available verses. |
|
9. If asked for a verse in Sanskrit, provide both the Sanskrit text (in Devanagari if requested) and the transliteration. |
|
10. You can answer in English, Hindi, or Sanskrit based on the user's preferred language. |
|
|
|
ANSWER: |
|
""" |
|
|
|
PROMPT = PromptTemplate(template=prompt_template, input_variables=['context', 'question']) |
|
|
|
qa_chain = RetrievalQA.from_chain_type( |
|
llm=llm, |
|
chain_type="stuff", |
|
retriever=retriever, |
|
chain_type_kwargs={"prompt": PROMPT} |
|
) |
|
return qa_chain |
|
|
|
|
|
print("Initializing Chatbot...") |
|
llm = initialize_llm() |
|
|
|
db_path = "./chroma_db" |
|
|
|
if not os.path.exists(db_path): |
|
vector_db = create_vector_db() |
|
else: |
|
print("Loading existing vector database...") |
|
embeddings = HuggingFaceBgeEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') |
|
vector_db = Chroma(persist_directory=db_path, embedding_function=embeddings) |
|
|
|
qa_chain = setup_qa_chain(vector_db, llm) |
|
print("Chatbot initialized successfully!") |
|
|
|
|
|
def chatbot_response(message, history): |
|
"""Process user input and generate response for Gradio ChatInterface""" |
|
if not message.strip(): |
|
return "Please provide a valid question about the Bhagavad Gita." |
|
|
|
try: |
|
response = qa_chain.run(message) |
|
return response |
|
except Exception as e: |
|
return f"I encountered an error processing your request: {str(e)}" |
|
|
|
|
|
example_questions = [ |
|
"What does Krishna say about dharma?", |
|
"How should one deal with anger?", |
|
"What is the nature of the soul?", |
|
"Explain karma yoga from the Gita", |
|
"What happens after death according to the Bhagavad Gita?", |
|
"How to find inner peace according to Krishna?" |
|
] |
|
|
|
|
|
with gr.Blocks(theme="soft") as app: |
|
gr.Markdown(""" |
|
# 🕉️ Bhagavad Gita Wisdom Guide |
|
|
|
Ask questions about the Bhagavad Gita and receive insights from this ancient sacred text. |
|
The guide can answer questions about specific verses, concepts, teachings, and applications of the Gita. |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=4): |
|
chatbot = gr.ChatInterface( |
|
fn=chatbot_response, |
|
examples=example_questions, |
|
title="Bhagavad Gita Spiritual Guide", |
|
analytics_enabled=False |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gr.Markdown(""" |
|
*This guide provides information based on the Bhagavad Gita text. For spiritual guidance, please consult with qualified teachers.* |
|
""") |
|
|
|
if __name__ == "__main__": |
|
app.launch() |