Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,14 +1,20 @@
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from config import SanatanConfig
|
|
|
|
| 6 |
from drive_downloader import ZipDownloader
|
| 7 |
from graph_helper import generate_graph
|
| 8 |
-
import uuid
|
| 9 |
-
import logging
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
|
|
|
|
| 12 |
logging.basicConfig()
|
| 13 |
logger = logging.getLogger()
|
| 14 |
logger.setLevel(logging.INFO)
|
|
@@ -18,42 +24,81 @@ graph = generate_graph()
|
|
| 18 |
|
| 19 |
def init():
|
| 20 |
load_dotenv(override=True)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# 2. Extract it
|
| 36 |
-
downloader.unzip(zip_path, extract_to="./")
|
| 37 |
|
| 38 |
|
| 39 |
def init_session():
|
| 40 |
-
# Generate a unique ID per browser session
|
| 41 |
return str(uuid.uuid4())
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
async def chat_wrapper(message, history, thread_id, debug):
|
| 45 |
if debug:
|
| 46 |
-
# Yield each message as it's generated
|
| 47 |
async for chunk in chat_streaming(message, history, thread_id):
|
| 48 |
yield chunk
|
| 49 |
else:
|
| 50 |
-
# Yield full response once
|
| 51 |
response = chat(message, history, thread_id)
|
| 52 |
yield response
|
| 53 |
|
| 54 |
|
| 55 |
def chat(message, history, thread_id):
|
| 56 |
-
print("received chat message for thread:", thread_id)
|
| 57 |
config = {"configurable": {"thread_id": thread_id}}
|
| 58 |
response = graph.invoke(
|
| 59 |
{"messages": [{"role": "user", "content": message}]}, config=config
|
|
@@ -61,107 +106,74 @@ def chat(message, history, thread_id):
|
|
| 61 |
return response["messages"][-1].content
|
| 62 |
|
| 63 |
|
| 64 |
-
import uuid
|
| 65 |
-
import asyncio
|
| 66 |
-
from html import escape
|
| 67 |
-
|
| 68 |
-
|
| 69 |
async def chat_streaming(message, history, thread_id):
|
| 70 |
state = {"messages": (history or []) + [{"role": "user", "content": message}]}
|
| 71 |
config = {"configurable": {"thread_id": thread_id}}
|
| 72 |
|
| 73 |
-
|
| 74 |
-
step_counter = 0
|
| 75 |
-
|
| 76 |
-
thinking_verbs = [
|
| 77 |
-
"thinking",
|
| 78 |
-
"processing",
|
| 79 |
-
"crunching data",
|
| 80 |
-
"please wait",
|
| 81 |
-
"just a few more seconds",
|
| 82 |
-
"closing in",
|
| 83 |
-
"analyzing",
|
| 84 |
-
"reasoning",
|
| 85 |
-
"computing",
|
| 86 |
-
"synthesizing insight",
|
| 87 |
-
"searching through the cosmos",
|
| 88 |
-
"decoding ancient knowledge",
|
| 89 |
-
"scanning the scriptures",
|
| 90 |
-
"accessing divine memory",
|
| 91 |
-
"gathering wisdom",
|
| 92 |
-
"consulting the rishis",
|
| 93 |
-
"listening to the ātmā",
|
| 94 |
-
"channeling sacred energy",
|
| 95 |
-
"unfolding the divine word",
|
| 96 |
-
"meditating on the meaning",
|
| 97 |
-
"reciting from memory",
|
| 98 |
-
"traversing the Vedas",
|
| 99 |
-
"seeking the inner light",
|
| 100 |
-
"invoking paramārtha",
|
| 101 |
-
"putting it all together",
|
| 102 |
-
"digging deeper",
|
| 103 |
-
"making sense of it",
|
| 104 |
-
"connecting the dots",
|
| 105 |
-
"almost there",
|
| 106 |
-
"getting closer",
|
| 107 |
-
"wrapping it up",
|
| 108 |
-
"piecing it together",
|
| 109 |
-
"swirling through verses",
|
| 110 |
-
"diving into the ocean of knowledge",
|
| 111 |
-
"lighting the lamp of understanding",
|
| 112 |
-
"walking the path of inquiry",
|
| 113 |
-
"aligning stars of context",
|
| 114 |
-
]
|
| 115 |
|
| 116 |
-
|
| 117 |
-
for
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
last_message = messages[-1]
|
| 124 |
-
content = getattr(last_message, "content", "") or ""
|
| 125 |
-
|
| 126 |
-
full = escape(content)
|
| 127 |
truncated = (full[:300] + "…") if len(full) > 300 else full
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
# Yield animated thinking bubble BEFORE actual message
|
| 135 |
-
yield f"<div id='{thinking_id}'><em>💭 {random.choice(thinking_verbs)}...</em></div>"
|
| 136 |
-
await asyncio.sleep(0.5) # let the thinking bubble show up slightly before
|
| 137 |
-
|
| 138 |
-
# Yield actual message in transparent font with node name
|
| 139 |
-
# Instead of two yields: one for thinking, one for message
|
| 140 |
-
# Combine both in one yield so frontend shows them as one stream block
|
| 141 |
-
html = (
|
| 142 |
-
f"<div><em>💭 {random.choice(thinking_verbs)} ...</em></div>"
|
| 143 |
-
f"<div style='opacity: 0.5' title='{full}'>"
|
| 144 |
-
f"<strong>[{node_name}]</strong> {truncated or '...'}"
|
| 145 |
f"</div>"
|
| 146 |
)
|
| 147 |
-
yield
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
thread_id = gr.State(init_session)
|
|
|
|
| 159 |
supported_scriptures = "\n - ".join(
|
| 160 |
[
|
| 161 |
f"📖 **{scripture['title']}** [source]({scripture['source']})"
|
| 162 |
for scripture in SanatanConfig.scriptures
|
| 163 |
]
|
| 164 |
)
|
|
|
|
| 165 |
intro_messages = [
|
| 166 |
{
|
| 167 |
"role": "assistant",
|
|
@@ -170,19 +182,20 @@ intro_messages = [
|
|
| 170 |
{
|
| 171 |
"role": "assistant",
|
| 172 |
"content": """
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
},
|
| 185 |
]
|
|
|
|
| 186 |
chatbot = gr.Chatbot(
|
| 187 |
elem_id="chatbot",
|
| 188 |
avatar_images=("assets/avatar_user.png", "assets/adiyen_bot.png"),
|
|
@@ -202,9 +215,22 @@ chatInterface = gr.ChatInterface(
|
|
| 202 |
fn=chat_wrapper,
|
| 203 |
additional_inputs=[thread_id, debug_checkbox],
|
| 204 |
chatbot=chatbot,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
)
|
| 206 |
|
| 207 |
-
|
| 208 |
-
# initializze database
|
| 209 |
init()
|
| 210 |
chatInterface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
+
import asyncio
|
| 4 |
+
import logging
|
| 5 |
+
import uuid
|
| 6 |
+
from html import escape
|
| 7 |
import gradio as gr
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
from langchain_core.messages.ai import AIMessage, AIMessageChunk
|
| 10 |
+
from langchain_core.messages.tool import ToolMessage
|
| 11 |
+
|
| 12 |
from config import SanatanConfig
|
| 13 |
+
from db import SanatanDatabase
|
| 14 |
from drive_downloader import ZipDownloader
|
| 15 |
from graph_helper import generate_graph
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Logging
|
| 18 |
logging.basicConfig()
|
| 19 |
logger = logging.getLogger()
|
| 20 |
logger.setLevel(logging.INFO)
|
|
|
|
| 24 |
|
| 25 |
def init():
|
| 26 |
load_dotenv(override=True)
|
| 27 |
+
try:
|
| 28 |
+
SanatanDatabase().test_sanity()
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.warning("Sanity Test Failed - %s", e)
|
| 31 |
+
logger.info("Downloading database ...")
|
| 32 |
+
downloader = ZipDownloader(
|
| 33 |
+
service_account_json=os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 34 |
+
)
|
| 35 |
+
zip_path = downloader.download_zip_from_drive(
|
| 36 |
+
file_id=os.getenv("CHROMADB_FILE_ID"),
|
| 37 |
+
output_path=SanatanConfig.dbStorePath,
|
| 38 |
+
)
|
| 39 |
+
downloader.unzip(zip_path, extract_to="./")
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
def init_session():
|
|
|
|
| 43 |
return str(uuid.uuid4())
|
| 44 |
|
| 45 |
|
| 46 |
+
def render_message_with_tooltip(content: str, max_chars=200):
|
| 47 |
+
short = escape(content[:max_chars]) + ("…" if len(content) > max_chars else "")
|
| 48 |
+
return f"<div title='{escape(content)}'>{short}</div>"
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
thinking_verbs = [
|
| 52 |
+
"thinking",
|
| 53 |
+
"processing",
|
| 54 |
+
"crunching data",
|
| 55 |
+
"please wait",
|
| 56 |
+
"just a few more seconds",
|
| 57 |
+
"closing in",
|
| 58 |
+
"analyzing",
|
| 59 |
+
"reasoning",
|
| 60 |
+
"computing",
|
| 61 |
+
"synthesizing insight",
|
| 62 |
+
"searching through the cosmos",
|
| 63 |
+
"decoding ancient knowledge",
|
| 64 |
+
"scanning the scriptures",
|
| 65 |
+
"accessing divine memory",
|
| 66 |
+
"gathering wisdom",
|
| 67 |
+
"consulting the rishis",
|
| 68 |
+
"listening to the ātmā",
|
| 69 |
+
"channeling sacred energy",
|
| 70 |
+
"unfolding the divine word",
|
| 71 |
+
"meditating on the meaning",
|
| 72 |
+
"reciting from memory",
|
| 73 |
+
"traversing the Vedas",
|
| 74 |
+
"seeking the inner light",
|
| 75 |
+
"invoking paramārtha",
|
| 76 |
+
"putting it all together",
|
| 77 |
+
"digging deeper",
|
| 78 |
+
"making sense of it",
|
| 79 |
+
"connecting the dots",
|
| 80 |
+
"almost there",
|
| 81 |
+
"getting closer",
|
| 82 |
+
"wrapping it up",
|
| 83 |
+
"piecing it together",
|
| 84 |
+
"swirling through verses",
|
| 85 |
+
"diving into the ocean of knowledge",
|
| 86 |
+
"lighting the lamp of understanding",
|
| 87 |
+
"walking the path of inquiry",
|
| 88 |
+
"aligning stars of context",
|
| 89 |
+
]
|
| 90 |
+
|
| 91 |
+
|
| 92 |
async def chat_wrapper(message, history, thread_id, debug):
|
| 93 |
if debug:
|
|
|
|
| 94 |
async for chunk in chat_streaming(message, history, thread_id):
|
| 95 |
yield chunk
|
| 96 |
else:
|
|
|
|
| 97 |
response = chat(message, history, thread_id)
|
| 98 |
yield response
|
| 99 |
|
| 100 |
|
| 101 |
def chat(message, history, thread_id):
|
|
|
|
| 102 |
config = {"configurable": {"thread_id": thread_id}}
|
| 103 |
response = graph.invoke(
|
| 104 |
{"messages": [{"role": "user", "content": message}]}, config=config
|
|
|
|
| 106 |
return response["messages"][-1].content
|
| 107 |
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
async def chat_streaming(message, history, thread_id):
|
| 110 |
state = {"messages": (history or []) + [{"role": "user", "content": message}]}
|
| 111 |
config = {"configurable": {"thread_id": thread_id}}
|
| 112 |
|
| 113 |
+
streamed_response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
try:
|
| 116 |
+
async for msg, metadata in graph.astream(
|
| 117 |
+
state, config=config, stream_mode="messages"
|
| 118 |
+
):
|
| 119 |
+
node = metadata.get("langgraph_node", "?")
|
| 120 |
+
name = getattr(msg, "name", "-")
|
| 121 |
+
full: str = escape(msg.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
truncated = (full[:300] + "…") if len(full) > 300 else full
|
| 123 |
|
| 124 |
+
processing_message = (
|
| 125 |
+
f"<div style='opacity: 0.1' title='{full}'>"
|
| 126 |
+
f"<div class='thinking-bubble'><em>🤔{random.choice(thinking_verbs)} ...</em></div>"
|
| 127 |
+
f"<span>{node}:{name or ''}:</span>"
|
| 128 |
+
f"<strong>Looking for : [{message}]</strong> {truncated or '...'}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
f"</div>"
|
| 130 |
)
|
| 131 |
+
# yield processing_message
|
| 132 |
+
if isinstance(msg, ToolMessage):
|
| 133 |
+
html = (
|
| 134 |
+
f"<div class='thinking-bubble'><em>🤔{name} : {random.choice(thinking_verbs)} ...</em></div>"
|
| 135 |
+
f"<div style='opacity: 0.5'>"
|
| 136 |
+
f"<strong>Looking for : [{message}]</strong> {truncated or '...'}"
|
| 137 |
+
f"</div>"
|
| 138 |
+
)
|
| 139 |
+
yield html
|
| 140 |
+
# yield f"""
|
| 141 |
+
# <div class='thinking-bubble'>🤔 {random.choice(thinking_verbs)}...</div>
|
| 142 |
+
# <div style='opacity: 0.5'><strong>[{node} - {name}]</strong>: {escape(msg.content)}</div>"""
|
| 143 |
+
|
| 144 |
+
elif isinstance(msg, AIMessageChunk):
|
| 145 |
+
if not msg.content:
|
| 146 |
+
# logger.warning("*** No Message Chunk!")
|
| 147 |
+
yield processing_message
|
| 148 |
+
else:
|
| 149 |
+
streamed_response += msg.content
|
| 150 |
+
yield streamed_response
|
| 151 |
+
else:
|
| 152 |
+
logger.debug("message = ", type(msg), msg.content[:100])
|
| 153 |
+
full: str = escape(msg.content)
|
| 154 |
+
truncated = (full[:300] + "…") if len(full) > 300 else full
|
| 155 |
+
html = (
|
| 156 |
+
f"<div class='thinking-bubble'><em>🤔 {random.choice(thinking_verbs)} ...</em></div>"
|
| 157 |
+
f"<div style='opacity: 0.1'>"
|
| 158 |
+
f"<strong>Telling myself:</strong> {truncated or '...'}"
|
| 159 |
+
f"</div>"
|
| 160 |
+
)
|
| 161 |
+
yield html
|
| 162 |
+
|
| 163 |
+
yield streamed_response
|
| 164 |
+
except Exception as e:
|
| 165 |
+
yield f"Error processing request {str(e)}"
|
| 166 |
+
|
| 167 |
+
# UI Elements
|
| 168 |
thread_id = gr.State(init_session)
|
| 169 |
+
|
| 170 |
supported_scriptures = "\n - ".join(
|
| 171 |
[
|
| 172 |
f"📖 **{scripture['title']}** [source]({scripture['source']})"
|
| 173 |
for scripture in SanatanConfig.scriptures
|
| 174 |
]
|
| 175 |
)
|
| 176 |
+
|
| 177 |
intro_messages = [
|
| 178 |
{
|
| 179 |
"role": "assistant",
|
|
|
|
| 182 |
{
|
| 183 |
"role": "assistant",
|
| 184 |
"content": """
|
| 185 |
+
#### Example questions you can ask:
|
| 186 |
+
- How is the form of Vishnu described across the scriptures?
|
| 187 |
+
- What teachings did Krishna share in the Gita?
|
| 188 |
+
- How did Arjuna respond upon witnessing Krishna’s Vishwarupa?
|
| 189 |
+
- What are some names of Vishnu from the Sahasranamam?
|
| 190 |
+
- Give me a pasuram by Andal
|
| 191 |
+
- explain sakrudeva prapannaaya shlokam in ramayana
|
| 192 |
+
- give the shlokam in ramayanam that vibheeshana uses to perform sharanagathi to rama, give the sanskrit shlokam and its meaning
|
| 193 |
+
- give one name in vishnu sahasranamam related to "cause"
|
| 194 |
+
- explain name of vishnu in sahasranamam related to wheel
|
| 195 |
+
""",
|
| 196 |
},
|
| 197 |
]
|
| 198 |
+
|
| 199 |
chatbot = gr.Chatbot(
|
| 200 |
elem_id="chatbot",
|
| 201 |
avatar_images=("assets/avatar_user.png", "assets/adiyen_bot.png"),
|
|
|
|
| 215 |
fn=chat_wrapper,
|
| 216 |
additional_inputs=[thread_id, debug_checkbox],
|
| 217 |
chatbot=chatbot,
|
| 218 |
+
css="""
|
| 219 |
+
<style>
|
| 220 |
+
.thinking-bubble {
|
| 221 |
+
opacity: 0.5;
|
| 222 |
+
font-style: italic;
|
| 223 |
+
animation: pulse 1.5s infinite;
|
| 224 |
+
margin-bottom: 5px;
|
| 225 |
+
}
|
| 226 |
+
@keyframes pulse {
|
| 227 |
+
0% { opacity: 0.3; }
|
| 228 |
+
50% { opacity: 1; }
|
| 229 |
+
100% { opacity: 0.3; }
|
| 230 |
+
}
|
| 231 |
+
</style>
|
| 232 |
+
""",
|
| 233 |
)
|
| 234 |
|
|
|
|
|
|
|
| 235 |
init()
|
| 236 |
chatInterface.launch()
|
db.py
CHANGED
|
@@ -43,7 +43,9 @@ class SanatanDatabase:
|
|
| 43 |
collection_name: str,
|
| 44 |
query: str,
|
| 45 |
metadata_field: str,
|
| 46 |
-
metadata_search_operator: Literal[
|
|
|
|
|
|
|
| 47 |
metadata_value: str,
|
| 48 |
n_results=2,
|
| 49 |
):
|
|
@@ -59,9 +61,16 @@ class SanatanDatabase:
|
|
| 59 |
return response
|
| 60 |
|
| 61 |
def count(self, collection_name: str):
|
| 62 |
-
logger.info("Getting total records in [%s]", collection_name)
|
| 63 |
collection = self.chroma_client.get_or_create_collection(name=collection_name)
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
|
|
|
| 43 |
collection_name: str,
|
| 44 |
query: str,
|
| 45 |
metadata_field: str,
|
| 46 |
+
metadata_search_operator: Literal[
|
| 47 |
+
"$in", "$eq", "$gt", "$gte", "$lt", "$lte", "$ne", "$nin"
|
| 48 |
+
],
|
| 49 |
metadata_value: str,
|
| 50 |
n_results=2,
|
| 51 |
):
|
|
|
|
| 61 |
return response
|
| 62 |
|
| 63 |
def count(self, collection_name: str):
|
|
|
|
| 64 |
collection = self.chroma_client.get_or_create_collection(name=collection_name)
|
| 65 |
+
total_count = collection.count()
|
| 66 |
+
logger.info("Total records in [%s] = %d", collection_name, total_count)
|
| 67 |
+
return total_count
|
| 68 |
+
|
| 69 |
+
def test_sanity(self):
|
| 70 |
+
for scripture in SanatanConfig().scriptures:
|
| 71 |
+
count = self.count(collection_name=scripture["collection_name"])
|
| 72 |
+
if count == 0:
|
| 73 |
+
raise Exception(f"No data in collection {scripture["collection_name"]}")
|
| 74 |
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|