Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +159 -139
- markdown.py +153 -42
app.py
CHANGED
|
@@ -1,140 +1,160 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pymongo
|
| 3 |
-
import certifi
|
| 4 |
-
from llama_index.core import VectorStoreIndex
|
| 5 |
-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 6 |
-
from llama_index.llms.groq import Groq
|
| 7 |
-
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
|
| 8 |
-
from llama_index.core.prompts import PromptTemplate
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
-
import os
|
| 11 |
-
import base64
|
| 12 |
-
import markdown as md
|
| 13 |
-
from datetime import datetime
|
| 14 |
-
|
| 15 |
-
# Load environment variables
|
| 16 |
-
load_dotenv()
|
| 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 |
-
with gr.
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pymongo
|
| 3 |
+
import certifi
|
| 4 |
+
from llama_index.core import VectorStoreIndex
|
| 5 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 6 |
+
from llama_index.llms.groq import Groq
|
| 7 |
+
from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch
|
| 8 |
+
from llama_index.core.prompts import PromptTemplate
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
import os
|
| 11 |
+
import base64
|
| 12 |
+
import markdown as md
|
| 13 |
+
from datetime import datetime
|
| 14 |
+
|
| 15 |
+
# Load environment variables
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
+
# --- Embedding Model ---
|
| 19 |
+
embed_model = HuggingFaceEmbedding(model_name="intfloat/multilingual-e5-base")
|
| 20 |
+
|
| 21 |
+
# --- Prompt Template ---
|
| 22 |
+
ramayana_qa_template = PromptTemplate(
|
| 23 |
+
"""You are an expert on the Valmiki Ramayana and a guide who always inspires people with the great Itihasa like the Ramayana.
|
| 24 |
+
|
| 25 |
+
Below is text from the epic, including shlokas and their explanations:
|
| 26 |
+
---------------------
|
| 27 |
+
{context_str}
|
| 28 |
+
---------------------
|
| 29 |
+
|
| 30 |
+
Using only this information, answer the following query.
|
| 31 |
+
|
| 32 |
+
Query: {query_str}
|
| 33 |
+
|
| 34 |
+
Answer:
|
| 35 |
+
- Intro or general description to ```Query```
|
| 36 |
+
- Related shloka/shlokas followed by its explanation
|
| 37 |
+
- Overview of ```Query```"""
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
gita_qa_template = PromptTemplate(
|
| 41 |
+
"""You are an expert on the Bhagavad Gita and a spiritual guide.
|
| 42 |
+
|
| 43 |
+
Below is text from the scripture, including verses and their explanations:
|
| 44 |
+
---------------------
|
| 45 |
+
{context_str}
|
| 46 |
+
---------------------
|
| 47 |
+
|
| 48 |
+
Using only this information, answer the following query.
|
| 49 |
+
|
| 50 |
+
Query: {query_str}
|
| 51 |
+
|
| 52 |
+
Answer:
|
| 53 |
+
- Intro or context about the topic
|
| 54 |
+
- Relevant verse(s) with explanation
|
| 55 |
+
- Conclusion or reflection"""
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# --- Connect to MongoDB once at startup ---
|
| 59 |
+
def get_vector_index(db_name, collection_name, vector_index_name):
|
| 60 |
+
mongo_client = pymongo.MongoClient(
|
| 61 |
+
os.getenv("ATLAS_CONNECTION_STRING"),
|
| 62 |
+
tlsCAFile=certifi.where(),
|
| 63 |
+
tlsAllowInvalidCertificates=False,
|
| 64 |
+
connectTimeoutMS=30000,
|
| 65 |
+
serverSelectionTimeoutMS=30000,
|
| 66 |
+
)
|
| 67 |
+
mongo_client.server_info()
|
| 68 |
+
print(f"✅ Connected to MongoDB Atlas for collection: {collection_name}")
|
| 69 |
+
|
| 70 |
+
vector_store = MongoDBAtlasVectorSearch(
|
| 71 |
+
mongo_client,
|
| 72 |
+
db_name=db_name,
|
| 73 |
+
collection_name=collection_name,
|
| 74 |
+
vector_index_name=vector_index_name,
|
| 75 |
+
)
|
| 76 |
+
return VectorStoreIndex.from_vector_store(vector_store, embed_model=embed_model)
|
| 77 |
+
|
| 78 |
+
# --- Respond Function (uses API key from state) ---
|
| 79 |
+
def chat_with_groq(index, template):
|
| 80 |
+
def fn(message, history, groq_key):
|
| 81 |
+
if not groq_key or not groq_key.startswith("gsk_"):
|
| 82 |
+
return "❌ Invalid Groq API Key. Please enter a valid key."
|
| 83 |
+
llm = Groq(model="llama-3.1-8b-instant", api_key=groq_key)
|
| 84 |
+
query_engine = index.as_query_engine(
|
| 85 |
+
llm=llm,
|
| 86 |
+
text_qa_template=template,
|
| 87 |
+
similarity_top_k=5,
|
| 88 |
+
verbose=True,
|
| 89 |
+
)
|
| 90 |
+
response = query_engine.query(message)
|
| 91 |
+
print(f"\n{datetime.now()}:: {message} --> {str(response)}\n")
|
| 92 |
+
return str(response)
|
| 93 |
+
return fn
|
| 94 |
+
|
| 95 |
+
# Load vector indices once
|
| 96 |
+
ramayana_index = get_vector_index("RAG", "ramayana", "ramayana_vector_index")
|
| 97 |
+
gita_index = get_vector_index("RAG", "bhagavad_gita", "gita_vector_index")
|
| 98 |
+
|
| 99 |
+
# Encode logos
|
| 100 |
+
|
| 101 |
+
def encode_image(image_path):
|
| 102 |
+
with open(image_path, "rb") as image_file:
|
| 103 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 104 |
+
|
| 105 |
+
github_logo_encoded = encode_image("Images/github-logo.png")
|
| 106 |
+
linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
|
| 107 |
+
website_logo_encoded = encode_image("Images/ai-logo.png")
|
| 108 |
+
|
| 109 |
+
# --- Gradio UI ---
|
| 110 |
+
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css='footer {visibility: hidden}') as demo:
|
| 111 |
+
with gr.Tabs():
|
| 112 |
+
with gr.TabItem("Intro"):
|
| 113 |
+
gr.Markdown(md.description)
|
| 114 |
+
|
| 115 |
+
def create_tab(tab_title, chatbot_title, vector_index, template, intro):
|
| 116 |
+
with gr.TabItem(tab_title):
|
| 117 |
+
with gr.Column(visible=True) as accordion_container:
|
| 118 |
+
with gr.Accordion("How to get Groq API KEY", open=False):
|
| 119 |
+
gr.Markdown(md.groq_api_key)
|
| 120 |
+
|
| 121 |
+
groq_key_box = gr.Textbox(
|
| 122 |
+
label="Enter Groq API Key",
|
| 123 |
+
type="password",
|
| 124 |
+
placeholder="Paste your Groq API key here..."
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
start_btn = gr.Button("Start Chat")
|
| 128 |
+
groq_state = gr.State(value="")
|
| 129 |
+
|
| 130 |
+
with gr.Column(visible=False) as chatbot_container:
|
| 131 |
+
with gr.Accordion("Overview & Summary", open=False):
|
| 132 |
+
gr.Markdown(intro)
|
| 133 |
+
chatbot = gr.ChatInterface(
|
| 134 |
+
fn=chat_with_groq(vector_index, template),
|
| 135 |
+
additional_inputs=[groq_state],
|
| 136 |
+
chatbot=gr.Chatbot(height=500),
|
| 137 |
+
title=chatbot_title,
|
| 138 |
+
show_progress="full",
|
| 139 |
+
fill_height=True,
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
def save_key_and_show_chat(key):
|
| 143 |
+
if key and key.startswith("gsk_"):
|
| 144 |
+
return key, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
| 145 |
+
else:
|
| 146 |
+
return "", gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
|
| 147 |
+
|
| 148 |
+
start_btn.click(
|
| 149 |
+
fn=save_key_and_show_chat,
|
| 150 |
+
inputs=[groq_key_box],
|
| 151 |
+
outputs=[groq_state, groq_key_box, start_btn, accordion_container, chatbot_container]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
create_tab("RamayanaGPT", "🕉️ RamayanaGPT", ramayana_index, ramayana_qa_template, md.RamayanaGPT)
|
| 155 |
+
create_tab("GitaGPT", "🕉️ GitaGPT", gita_index, gita_qa_template, md.GitaGPT)
|
| 156 |
+
|
| 157 |
+
gr.HTML(md.footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
|
| 158 |
+
|
| 159 |
+
if __name__ == "__main__":
|
| 160 |
demo.launch()
|
markdown.py
CHANGED
|
@@ -1,11 +1,19 @@
|
|
| 1 |
description = """
|
| 2 |
-
## 🕉️ **Project Title: RamayanaGPT –
|
| 3 |
|
| 4 |
---
|
| 5 |
|
| 6 |
### 🔍 **Project Overview**
|
| 7 |
|
| 8 |
-
**RamayanaGPT**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
---
|
| 11 |
|
|
@@ -13,72 +21,99 @@ description = """
|
|
| 13 |
|
| 14 |
#### 1. **Vector Store: MongoDB Atlas**
|
| 15 |
|
| 16 |
-
*
|
| 17 |
-
|
| 18 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
#### 3. **Language Model: Groq API**
|
| 26 |
|
| 27 |
-
*
|
| 28 |
-
*
|
| 29 |
-
*
|
| 30 |
|
| 31 |
#### 4. **Prompt Engineering**
|
| 32 |
|
| 33 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
*
|
| 36 |
-
*
|
| 37 |
-
*
|
| 38 |
-
* Give a closing summary relevant to the query.
|
| 39 |
-
* Prompt ensures scholarly tone and contextual accuracy.
|
| 40 |
|
| 41 |
-
#### 5. **
|
| 42 |
|
| 43 |
-
*
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
#### 6. **User Interface: Gradio**
|
| 47 |
|
| 48 |
-
*
|
| 49 |
-
*
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
* API key input and
|
| 53 |
-
*
|
| 54 |
-
* Uses `gr.State` to hold the Groq API key during the session.
|
| 55 |
|
| 56 |
---
|
| 57 |
|
| 58 |
### ⚙️ **Technical Stack**
|
| 59 |
|
| 60 |
-
| Component | Technology
|
| 61 |
-
| --------------- |
|
| 62 |
-
| Backend LLM | Groq (LLaMA 3.1 8B via API)
|
| 63 |
-
| Embedding Model | Hugging Face (multilingual-e5-base) |
|
| 64 |
-
| Vector Store | MongoDB Atlas Vector Search
|
| 65 |
-
|
|
| 66 |
-
| Prompt Engine | LlamaIndex PromptTemplate
|
| 67 |
-
|
|
| 68 |
-
|
|
|
|
|
| 69 |
|
| 70 |
---
|
| 71 |
|
| 72 |
### ✅ **Features Implemented**
|
| 73 |
|
| 74 |
-
* [x]
|
| 75 |
-
|
| 76 |
-
*
|
| 77 |
-
*
|
| 78 |
-
* [x]
|
| 79 |
-
* [x]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
---
|
| 82 |
"""
|
| 83 |
|
| 84 |
groq_api_key = """
|
|
@@ -96,6 +131,82 @@ groq_api_key = """
|
|
| 96 |
⚠️ **Don't share** your API key. Revoke and regenerate if needed.
|
| 97 |
"""
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
footer = """
|
| 100 |
<div style="background-color: #1d2938; color: white; padding: 10px; width: 100%; bottom: 0; left: 0; display: flex; justify-content: space-between; align-items: center; padding: .2rem 35px; box-sizing: border-box; font-size: 16px;">
|
| 101 |
<div style="text-align: left;">
|
|
|
|
| 1 |
description = """
|
| 2 |
+
## 🕉️ **Project Title: RamayanaGPT & GitaGPT – RAG-based Chatbots for Ancient Indian Epics**
|
| 3 |
|
| 4 |
---
|
| 5 |
|
| 6 |
### 🔍 **Project Overview**
|
| 7 |
|
| 8 |
+
**RamayanaGPT** and **GitaGPT** are knowledge-based conversational AI tools designed to answer questions from the *Valmiki Ramayana* and the *Bhagavad Gita*, respectively. These chatbots use **Retrieval-Augmented Generation (RAG)** architecture to generate accurate, scripture-based responses. They combine powerful **vector search capabilities** with **large language models (LLMs)** to deliver spiritually insightful, context-rich conversations.
|
| 9 |
+
|
| 10 |
+
These tools leverage:
|
| 11 |
+
|
| 12 |
+
* **MongoDB Atlas Vector Search** for embedding-based document retrieval
|
| 13 |
+
* **Hugging Face** embeddings (`intfloat/multilingual-e5-base`)
|
| 14 |
+
* **Groq LLaMA 3.1 8B** via API
|
| 15 |
+
* **LlamaIndex** for orchestration
|
| 16 |
+
* **Gradio** for user interface
|
| 17 |
|
| 18 |
---
|
| 19 |
|
|
|
|
| 21 |
|
| 22 |
#### 1. **Vector Store: MongoDB Atlas**
|
| 23 |
|
| 24 |
+
* Two collections are created in the `RAG` database:
|
| 25 |
+
|
| 26 |
+
* `ramayana` for **Valmiki Ramayana**
|
| 27 |
+
* `bhagavad_gita` for **Bhagavad Gita**
|
| 28 |
+
* Each collection contains vector indexes:
|
| 29 |
+
|
| 30 |
+
* `ramayana_vector_index`
|
| 31 |
+
* `gita_vector_index`
|
| 32 |
+
* Each document includes:
|
| 33 |
|
| 34 |
+
* For Ramayana: `kanda`, `sarga`, `shloka`, `shloka_text`, and `explanation`
|
| 35 |
+
* For Gita: `Title`, `Chapter`, `Verse`, and `explanation`
|
| 36 |
|
| 37 |
+
#### 2. **Vector Embedding: Hugging Face**
|
| 38 |
+
|
| 39 |
+
* Model: `intfloat/multilingual-e5-base`
|
| 40 |
+
* Used to convert `shloka_text + explanation` or `verse + explanation` into vector representations
|
| 41 |
+
* These embeddings are indexed into MongoDB for semantic similarity search
|
| 42 |
|
| 43 |
#### 3. **Language Model: Groq API**
|
| 44 |
|
| 45 |
+
* LLM used: `llama-3.1-8b-instant` via **Groq API**
|
| 46 |
+
* Users input their Groq API key securely
|
| 47 |
+
* LLM is instantiated per query using `llama_index.llms.groq.Groq`
|
| 48 |
|
| 49 |
#### 4. **Prompt Engineering**
|
| 50 |
|
| 51 |
+
* Custom **PromptTemplates** guide the response structure for each chatbot
|
| 52 |
+
* **RamayanaGPT Prompt**:
|
| 53 |
+
|
| 54 |
+
* Introduction to the query
|
| 55 |
+
* Related shlokas with explanations
|
| 56 |
+
* Summary/overview
|
| 57 |
+
* **GitaGPT Prompt**:
|
| 58 |
|
| 59 |
+
* Context or spiritual background
|
| 60 |
+
* Relevant verse(s) with meaning
|
| 61 |
+
* Reflective conclusion
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
#### 5. **Index Initialization**
|
| 64 |
|
| 65 |
+
* Vector indexes are loaded **once** at application startup:
|
| 66 |
+
|
| 67 |
+
```python
|
| 68 |
+
ramayana_index = get_vector_index("RAG", "ramayana", "ramayana_vector_index")
|
| 69 |
+
gita_index = get_vector_index("RAG", "bhagavad_gita", "gita_vector_index")
|
| 70 |
+
```
|
| 71 |
+
* Shared across all user queries for speed and efficiency
|
| 72 |
|
| 73 |
#### 6. **User Interface: Gradio**
|
| 74 |
|
| 75 |
+
* Built with `gr.Blocks` using the `Soft` theme and `Roboto Mono` font
|
| 76 |
+
* Two tabs:
|
| 77 |
+
|
| 78 |
+
* 🕉️ **RamayanaGPT**
|
| 79 |
+
* 🕉️ **GitaGPT**
|
| 80 |
+
* Users enter their Groq API key once; it's stored in `gr.State`
|
| 81 |
+
* Upon authentication:
|
| 82 |
|
| 83 |
+
* API key input and help accordion are hidden
|
| 84 |
+
* Full chat interface is revealed (`gr.ChatInterface`)
|
|
|
|
| 85 |
|
| 86 |
---
|
| 87 |
|
| 88 |
### ⚙️ **Technical Stack**
|
| 89 |
|
| 90 |
+
| Component | Technology |
|
| 91 |
+
| --------------- | ------------------------------------- |
|
| 92 |
+
| Backend LLM | Groq (LLaMA 3.1 8B via API) |
|
| 93 |
+
| Embedding Model | Hugging Face (`multilingual-e5-base`) |
|
| 94 |
+
| Vector Store | MongoDB Atlas Vector Search |
|
| 95 |
+
| Vector Engine | LlamaIndex VectorStoreIndex |
|
| 96 |
+
| Prompt Engine | LlamaIndex PromptTemplate |
|
| 97 |
+
| Query Engine | LlamaIndex Query Engine |
|
| 98 |
+
| UI Framework | Gradio (Blocks + ChatInterface) |
|
| 99 |
+
| Deployment | Python app using `app.py` |
|
| 100 |
|
| 101 |
---
|
| 102 |
|
| 103 |
### ✅ **Features Implemented**
|
| 104 |
|
| 105 |
+
* [x] Vector search using MongoDB Atlas
|
| 106 |
+
|
| 107 |
+
* `ramayana_vector_index` for Valmiki Ramayana
|
| 108 |
+
* `gita_vector_index` for Bhagavad Gita
|
| 109 |
+
* [x] Hugging Face embedding (`e5-base`) integration
|
| 110 |
+
* [x] API key input and session handling with `gr.State`
|
| 111 |
+
* [x] LLM integration via Groq API
|
| 112 |
+
* [x] Prompt templates customized for each scripture
|
| 113 |
+
* [x] Tabbed interface for seamless switching between RamayanaGPT and GitaGPT
|
| 114 |
+
* [x] Clean UX with collapsible Groq API key instructions
|
| 115 |
+
* [x] Logging of each query with timestamp (for debugging/monitoring)
|
| 116 |
|
|
|
|
| 117 |
"""
|
| 118 |
|
| 119 |
groq_api_key = """
|
|
|
|
| 131 |
⚠️ **Don't share** your API key. Revoke and regenerate if needed.
|
| 132 |
"""
|
| 133 |
|
| 134 |
+
RamayanaGPT='''
|
| 135 |
+
## 🕉️ **RamayanaGPT – Overview and Dataset Summary**
|
| 136 |
+
|
| 137 |
+
### 📖 **Introduction**
|
| 138 |
+
|
| 139 |
+
**RamayanaGPT** is a RAG-based chatbot that draws upon the **Valmiki Ramayana**, the original Sanskrit epic, to answer user queries with reference to shlokas and their commentaries. It aims to offer precise, contextual, and respectful responses using advanced retrieval and generation technologies.
|
| 140 |
+
|
| 141 |
+
### 🗂️ **Dataset Structure**
|
| 142 |
+
|
| 143 |
+
The uploaded Ramayana dataset includes the following columns:
|
| 144 |
+
|
| 145 |
+
| Column | Description |
|
| 146 |
+
| ------------- | ------------------------------------------------------------------------------ |
|
| 147 |
+
| `kanda` | One of the 7 books (kandas) of the Ramayana (e.g., Bala Kanda, Ayodhya Kanda). |
|
| 148 |
+
| `sarga` | The chapter number within each kanda. |
|
| 149 |
+
| `shloka` | The shloka (verse) number within the sarga. |
|
| 150 |
+
| `shloka_text` | Original Sanskrit verse. |
|
| 151 |
+
| `explanation` | English explanation or interpretation of the shloka. |
|
| 152 |
+
|
| 153 |
+
### 🔍 **Example**
|
| 154 |
+
|
| 155 |
+
```text
|
| 156 |
+
kanda: Bala Kanda
|
| 157 |
+
sarga: 1
|
| 158 |
+
shloka: 1
|
| 159 |
+
shloka_text: तपस्स्वाध्यायनिरतं तपस्वी वाग्विदां वरम् ।
|
| 160 |
+
explanation: Ascetic Valmiki enquired of Narada, preeminent among sages, who was engaged in penance and study of the Vedas.
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
### 💡 **Insights**
|
| 164 |
+
|
| 165 |
+
* The data is well-structured with nearly **1,400+** records.
|
| 166 |
+
* Each record reflects a deep philosophical or narrative moment from the epic.
|
| 167 |
+
* Metadata (`kanda`, `sarga`, `shloka`) allows precise retrieval and organization.
|
| 168 |
+
* Used for vector indexing and semantic retrieval.
|
| 169 |
+
'''
|
| 170 |
+
|
| 171 |
+
GitaGPT='''
|
| 172 |
+
## 🕉️ **GitaGPT – Overview and Dataset Summary**
|
| 173 |
+
|
| 174 |
+
### 📖 **Introduction**
|
| 175 |
+
|
| 176 |
+
**GitaGPT** is a chatbot built to answer spiritual and philosophical questions using the **Bhagavad Gita** as its primary source. It references verses (slokas) directly from the Gita, delivering insights supported by both Sanskrit, Hindi, and English explanations.
|
| 177 |
+
|
| 178 |
+
### 🗂️ **Dataset Structure**
|
| 179 |
+
|
| 180 |
+
The uploaded Gita dataset contains the following fields:
|
| 181 |
+
|
| 182 |
+
| Column | Description |
|
| 183 |
+
| --------------------- | --------------------------------------------------- |
|
| 184 |
+
| `S.No.` | Serial number of the verse. |
|
| 185 |
+
| `Title` | Title of the chapter (e.g., Arjuna's Vishada Yoga). |
|
| 186 |
+
| `Chapter` | Gita chapter number (e.g., Chapter 1). |
|
| 187 |
+
| `Verse` | Verse ID (e.g., Verse 1.1). |
|
| 188 |
+
| `Sanskrit Anuvad` | Original verse in Devanagari Sanskrit. |
|
| 189 |
+
| `Hindi Anuvad` | Hindi translation/interpretation. |
|
| 190 |
+
| `Enlgish Translation` | English translation/interpretation. |
|
| 191 |
+
|
| 192 |
+
### 🔍 **Example**
|
| 193 |
+
|
| 194 |
+
```text
|
| 195 |
+
Chapter: Chapter 1
|
| 196 |
+
Verse: Verse 1.1
|
| 197 |
+
Sanskrit: धृतराष्ट्र उवाच । धर्मक्षेत्रे कुरुक्षेत्रे समवेता युयुत्सवः...
|
| 198 |
+
Hindi: धृतराष्ट्र बोले- हे संजय! धर्मभूमि कुरुक्षेत्र में एकत्र हुए युद्ध की इच्छा रखने वाले...
|
| 199 |
+
English: Dhrtarashtra asked of Sanjaya: O SANJAYA, what did my sons and the sons of Pandu do?
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
### 💡 **Insights**
|
| 203 |
+
|
| 204 |
+
* The dataset contains **700+ verses** from all 18 chapters.
|
| 205 |
+
* Multilingual representation (Sanskrit, Hindi, English) enhances usability for diverse users.
|
| 206 |
+
* The verse structure (`Chapter`, `Verse`) aids in precise referencing and response generation.
|
| 207 |
+
* Perfectly suited for semantic search via vector embeddings.
|
| 208 |
+
'''
|
| 209 |
+
|
| 210 |
footer = """
|
| 211 |
<div style="background-color: #1d2938; color: white; padding: 10px; width: 100%; bottom: 0; left: 0; display: flex; justify-content: space-between; align-items: center; padding: .2rem 35px; box-sizing: border-box; font-size: 16px;">
|
| 212 |
<div style="text-align: left;">
|