File size: 6,465 Bytes
8a6b2b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227506e
1
2
3
4
5
6
7
8
9
10
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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

# Initialize the chatbot components
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!")

# Define the proper Gradio chatbot response function
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)}"

# Create example questions for the chatbot
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?"
]

# Set up the Gradio interface
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
            )

    #     with gr.Column(scale=1):
    #         gr.Markdown("""
    #         ### About this Guide

    #         This application uses AI to help you explore the wisdom of the Bhagavad Gita.

    #         You can ask about:
    #         - Specific verses
    #         - Philosophical concepts
    #         - Practical applications
    #         - Sanskrit terminology

    #         The guide draws from the Bhagavad Gita text to provide accurate information.
    #         """)

    #         gr.Markdown("""
    #         ### Sample Topics

    #         - Dharma (duty)
    #         - Karma (action)
    #         - Atman (soul)
    #         - Meditation techniques
    #         - Handling emotions
    #         - Spiritual practices
    #         - Moksha (liberation)
    #         """)

    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()