Zane Vijay Falcao commited on
Commit
8a6b2b1
·
verified ·
1 Parent(s): 68d2e37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -171
app.py CHANGED
@@ -1,172 +1,172 @@
1
- from langchain.embeddings import HuggingFaceBgeEmbeddings
2
- from langchain.document_loaders import PyPDFLoader, DirectoryLoader
3
- from langchain.vectorstores import Chroma
4
- from langchain.chains import RetrievalQA
5
- from langchain.prompts import PromptTemplate
6
- from langchain.text_splitter import RecursiveCharacterTextSplitter
7
- from langchain_groq import ChatGroq
8
- import os
9
- import gradio as gr
10
-
11
- def initialize_llm():
12
- """Initialize the Groq LLM"""
13
- llm = ChatGroq(
14
- temperature=0,
15
- groq_api_key="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",
16
- model_name="llama-3.3-70b-versatile"
17
- )
18
- return llm
19
-
20
- def create_vector_db():
21
- """Create and populate the vector database from PDF files"""
22
- print("Creating vector database...")
23
-
24
- loader = DirectoryLoader("/content/bg/", glob='*.pdf', loader_cls=PyPDFLoader)
25
- documents = loader.load()
26
-
27
- print(f"Loaded {len(documents)} documents")
28
-
29
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
30
- texts = text_splitter.split_documents(documents)
31
-
32
- print(f"Split into {len(texts)} text chunks")
33
-
34
- embeddings = HuggingFaceBgeEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
35
- vector_db = Chroma.from_documents(texts, embeddings, persist_directory='./chroma_db')
36
- vector_db.persist()
37
-
38
- print("ChromaDB created and data saved")
39
-
40
- return vector_db
41
-
42
- def setup_qa_chain(vector_db, llm):
43
- """Set up the question-answering chain"""
44
- retriever = vector_db.as_retriever(search_kwargs={"k": 5})
45
-
46
- prompt_template = """You are a knowledgeable spiritual guide specialized in the Bhagavad Gita, an ancient Hindu scripture.
47
- 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.
48
-
49
- CONTEXT INFORMATION:
50
- {context}
51
-
52
- USER QUESTION:
53
- {question}
54
-
55
- Please provide a comprehensive answer to the user's question by following these guidelines:
56
- 1. Format your response with clear headings (use ## for main sections, ### for subsections) and paragraphs for readability.
57
- 2. Always cite the specific chapter and verse numbers (e.g., Chapter 2, Verse 47) when referencing the scripture.
58
- 3. When quoting Sanskrit verses, clearly mark them with "Sanskrit:" and use quotation marks.
59
- 4. For longer answers, structure your response with these sections:
60
- - Direct Answer (a concise response to the question)
61
- - Relevant Verses (the key verses that address the question)
62
- - Explanation (deeper analysis of the verses)
63
- - Modern Application (if appropriate)
64
- 5. Use bullet points for listing multiple related concepts.
65
- 6. If multiple verses are relevant, organize them logically to show their relationship.
66
- 7. When appropriate, include a brief conclusion that summarizes the key insights.
67
- 8. If the context doesn't fully answer the question, acknowledge this and provide the best answer based on the available verses.
68
- 9. If asked for a verse in Sanskrit, provide both the Sanskrit text (in Devanagari if requested) and the transliteration.
69
- 10. You can answer in English, Hindi, or Sanskrit based on the user's preferred language.
70
-
71
- ANSWER:
72
- """
73
-
74
- PROMPT = PromptTemplate(template=prompt_template, input_variables=['context', 'question'])
75
-
76
- qa_chain = RetrievalQA.from_chain_type(
77
- llm=llm,
78
- chain_type="stuff",
79
- retriever=retriever,
80
- chain_type_kwargs={"prompt": PROMPT}
81
- )
82
- return qa_chain
83
-
84
- # Initialize the chatbot components
85
- print("Initializing Chatbot...")
86
- llm = initialize_llm()
87
-
88
- db_path = "./chroma_db"
89
-
90
- if not os.path.exists(db_path):
91
- vector_db = create_vector_db()
92
- else:
93
- print("Loading existing vector database...")
94
- embeddings = HuggingFaceBgeEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
95
- vector_db = Chroma(persist_directory=db_path, embedding_function=embeddings)
96
-
97
- qa_chain = setup_qa_chain(vector_db, llm)
98
- print("Chatbot initialized successfully!")
99
-
100
- # Define the proper Gradio chatbot response function
101
- def chatbot_response(message, history):
102
- """Process user input and generate response for Gradio ChatInterface"""
103
- if not message.strip():
104
- return "Please provide a valid question about the Bhagavad Gita."
105
-
106
- try:
107
- response = qa_chain.run(message)
108
- return response
109
- except Exception as e:
110
- return f"I encountered an error processing your request: {str(e)}"
111
-
112
- # Create example questions for the chatbot
113
- example_questions = [
114
- "What does Krishna say about dharma?",
115
- "How should one deal with anger?",
116
- "What is the nature of the soul?",
117
- "Explain karma yoga from the Gita",
118
- "What happens after death according to the Bhagavad Gita?",
119
- "How to find inner peace according to Krishna?"
120
- ]
121
-
122
- # Set up the Gradio interface
123
- with gr.Blocks(theme="soft") as app:
124
- gr.Markdown("""
125
- # 🕉️ Bhagavad Gita Wisdom Guide
126
-
127
- Ask questions about the Bhagavad Gita and receive insights from this ancient sacred text.
128
- The guide can answer questions about specific verses, concepts, teachings, and applications of the Gita.
129
- """)
130
-
131
- with gr.Row():
132
- with gr.Column(scale=4):
133
- chatbot = gr.ChatInterface(
134
- fn=chatbot_response,
135
- examples=example_questions,
136
- title="Bhagavad Gita Spiritual Guide",
137
- analytics_enabled=False
138
- )
139
-
140
- # with gr.Column(scale=1):
141
- # gr.Markdown("""
142
- # ### About this Guide
143
-
144
- # This application uses AI to help you explore the wisdom of the Bhagavad Gita.
145
-
146
- # You can ask about:
147
- # - Specific verses
148
- # - Philosophical concepts
149
- # - Practical applications
150
- # - Sanskrit terminology
151
-
152
- # The guide draws from the Bhagavad Gita text to provide accurate information.
153
- # """)
154
-
155
- # gr.Markdown("""
156
- # ### Sample Topics
157
-
158
- # - Dharma (duty)
159
- # - Karma (action)
160
- # - Atman (soul)
161
- # - Meditation techniques
162
- # - Handling emotions
163
- # - Spiritual practices
164
- # - Moksha (liberation)
165
- # """)
166
-
167
- gr.Markdown("""
168
- *This guide provides information based on the Bhagavad Gita text. For spiritual guidance, please consult with qualified teachers.*
169
- """)
170
-
171
- if __name__ == "__main__":
172
  app.launch()
 
1
+ from langchain.embeddings import HuggingFaceBgeEmbeddings
2
+ from langchain.document_loaders import PyPDFLoader, DirectoryLoader
3
+ from langchain.vectorstores import Chroma
4
+ from langchain.chains import RetrievalQA
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
7
+ from langchain_groq import ChatGroq
8
+ import os
9
+ import gradio as gr
10
+
11
+ def initialize_llm():
12
+ """Initialize the Groq LLM"""
13
+ llm = ChatGroq(
14
+ temperature=0,
15
+ groq_api_key="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",
16
+ model_name="llama-3.3-70b-versatile"
17
+ )
18
+ return llm
19
+
20
+ def create_vector_db():
21
+ """Create and populate the vector database from PDF files"""
22
+ print("Creating vector database...")
23
+
24
+ loader = DirectoryLoader("/content/bg/", glob='*.pdf', loader_cls=PyPDFLoader)
25
+ documents = loader.load()
26
+
27
+ print(f"Loaded {len(documents)} documents")
28
+
29
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
30
+ texts = text_splitter.split_documents(documents)
31
+
32
+ print(f"Split into {len(texts)} text chunks")
33
+
34
+ embeddings = HuggingFaceBgeEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
35
+ vector_db = Chroma.from_documents(texts, embeddings, persist_directory='./chroma_db')
36
+ vector_db.persist()
37
+
38
+ print("ChromaDB created and data saved")
39
+
40
+ return vector_db
41
+
42
+ def setup_qa_chain(vector_db, llm):
43
+ """Set up the question-answering chain"""
44
+ retriever = vector_db.as_retriever(search_kwargs={"k": 5})
45
+
46
+ prompt_template = """You are a knowledgeable spiritual guide specialized in the Bhagavad Gita, an ancient Hindu scripture.
47
+ 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.
48
+
49
+ CONTEXT INFORMATION:
50
+ {context}
51
+
52
+ USER QUESTION:
53
+ {question}
54
+
55
+ Please provide a comprehensive answer to the user's question by following these guidelines:
56
+ 1. Format your response with clear headings (use ## for main sections, ### for subsections) and paragraphs for readability.
57
+ 2. Always cite the specific chapter and verse numbers (e.g., Chapter 2, Verse 47) when referencing the scripture.
58
+ 3. When quoting Sanskrit verses, clearly mark them with "Sanskrit:" and use quotation marks.
59
+ 4. For longer answers, structure your response with these sections:
60
+ - Direct Answer (a concise response to the question)
61
+ - Relevant Verses (the key verses that address the question)
62
+ - Explanation (deeper analysis of the verses)
63
+ - Modern Application (if appropriate only based on the context and nothing from the Internet)
64
+ 5. Use bullet points for listing multiple related concepts.
65
+ 6. If multiple verses are relevant, organize them logically to show their relationship.
66
+ 7. When appropriate, include a brief conclusion that summarizes the key insights.
67
+ 8. If the context doesn't fully answer the question, acknowledge this and provide the best answer based on the available verses.
68
+ 9. If asked for a verse in Sanskrit, provide both the Sanskrit text (in Devanagari if requested) and the transliteration.
69
+ 10. You can answer in English, Hindi, or Sanskrit based on the user's preferred language.
70
+
71
+ ANSWER:
72
+ """
73
+
74
+ PROMPT = PromptTemplate(template=prompt_template, input_variables=['context', 'question'])
75
+
76
+ qa_chain = RetrievalQA.from_chain_type(
77
+ llm=llm,
78
+ chain_type="stuff",
79
+ retriever=retriever,
80
+ chain_type_kwargs={"prompt": PROMPT}
81
+ )
82
+ return qa_chain
83
+
84
+ # Initialize the chatbot components
85
+ print("Initializing Chatbot...")
86
+ llm = initialize_llm()
87
+
88
+ db_path = "./chroma_db"
89
+
90
+ if not os.path.exists(db_path):
91
+ vector_db = create_vector_db()
92
+ else:
93
+ print("Loading existing vector database...")
94
+ embeddings = HuggingFaceBgeEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
95
+ vector_db = Chroma(persist_directory=db_path, embedding_function=embeddings)
96
+
97
+ qa_chain = setup_qa_chain(vector_db, llm)
98
+ print("Chatbot initialized successfully!")
99
+
100
+ # Define the proper Gradio chatbot response function
101
+ def chatbot_response(message, history):
102
+ """Process user input and generate response for Gradio ChatInterface"""
103
+ if not message.strip():
104
+ return "Please provide a valid question about the Bhagavad Gita."
105
+
106
+ try:
107
+ response = qa_chain.run(message)
108
+ return response
109
+ except Exception as e:
110
+ return f"I encountered an error processing your request: {str(e)}"
111
+
112
+ # Create example questions for the chatbot
113
+ example_questions = [
114
+ "What does Krishna say about dharma?",
115
+ "How should one deal with anger?",
116
+ "What is the nature of the soul?",
117
+ "Explain karma yoga from the Gita",
118
+ "What happens after death according to the Bhagavad Gita?",
119
+ "How to find inner peace according to Krishna?"
120
+ ]
121
+
122
+ # Set up the Gradio interface
123
+ with gr.Blocks(theme="soft") as app:
124
+ gr.Markdown("""
125
+ # 🕉️ Bhagavad Gita Wisdom Guide
126
+
127
+ Ask questions about the Bhagavad Gita and receive insights from this ancient sacred text.
128
+ The guide can answer questions about specific verses, concepts, teachings, and applications of the Gita.
129
+ """)
130
+
131
+ with gr.Row():
132
+ with gr.Column(scale=4):
133
+ chatbot = gr.ChatInterface(
134
+ fn=chatbot_response,
135
+ examples=example_questions,
136
+ title="Bhagavad Gita Spiritual Guide",
137
+ analytics_enabled=False
138
+ )
139
+
140
+ # with gr.Column(scale=1):
141
+ # gr.Markdown("""
142
+ # ### About this Guide
143
+
144
+ # This application uses AI to help you explore the wisdom of the Bhagavad Gita.
145
+
146
+ # You can ask about:
147
+ # - Specific verses
148
+ # - Philosophical concepts
149
+ # - Practical applications
150
+ # - Sanskrit terminology
151
+
152
+ # The guide draws from the Bhagavad Gita text to provide accurate information.
153
+ # """)
154
+
155
+ # gr.Markdown("""
156
+ # ### Sample Topics
157
+
158
+ # - Dharma (duty)
159
+ # - Karma (action)
160
+ # - Atman (soul)
161
+ # - Meditation techniques
162
+ # - Handling emotions
163
+ # - Spiritual practices
164
+ # - Moksha (liberation)
165
+ # """)
166
+
167
+ gr.Markdown("""
168
+ *This guide provides information based on the Bhagavad Gita text. For spiritual guidance, please consult with qualified teachers.*
169
+ """)
170
+
171
+ if __name__ == "__main__":
172
  app.launch()