Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Upload folder using huggingface_hub
Browse files- app.py +18 -0
- graph_helper.py +73 -62
- sanatan_assistant.py +6 -6
app.py
CHANGED
|
@@ -359,6 +359,24 @@ chatInterface = gr.ChatInterface(
|
|
| 359 |
additional_inputs=[thread_id, debug_checkbox],
|
| 360 |
chatbot=chatbot,
|
| 361 |
css="""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
.spinner {
|
| 363 |
display: inline-block;
|
| 364 |
width: 1em;
|
|
|
|
| 359 |
additional_inputs=[thread_id, debug_checkbox],
|
| 360 |
chatbot=chatbot,
|
| 361 |
css="""
|
| 362 |
+
table {
|
| 363 |
+
border-collapse: collapse;
|
| 364 |
+
width: 90%;
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
table, th, td {
|
| 368 |
+
border: 1px solid #ddd;
|
| 369 |
+
padding: 6px;
|
| 370 |
+
font-size: small;
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
td {
|
| 374 |
+
word-wrap: break-word;
|
| 375 |
+
white-space: pre-wrap; /* preserves line breaks but wraps long lines */
|
| 376 |
+
max-width: 300px; /* control width */
|
| 377 |
+
vertical-align: top;
|
| 378 |
+
}
|
| 379 |
+
|
| 380 |
.spinner {
|
| 381 |
display: inline-block;
|
| 382 |
width: 1em;
|
graph_helper.py
CHANGED
|
@@ -73,68 +73,79 @@ def generate_graph() -> CompiledStateGraph:
|
|
| 73 |
SystemMessage(
|
| 74 |
content=(
|
| 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 |
),
|
|
|
|
| 73 |
SystemMessage(
|
| 74 |
content=(
|
| 75 |
"""
|
| 76 |
+
You are a strict validator for LLM responses to scripture queries.
|
| 77 |
+
|
| 78 |
+
Your tasks:
|
| 79 |
+
0. Treat your input as `original_llm_response`.
|
| 80 |
+
1. Compare the original user query to the LLM’s answer.
|
| 81 |
+
2. Identify the scripture context (e.g., Divya Prabandham, Bhagavad Gita, Upanishads, Ramayana, etc.).
|
| 82 |
+
3. Based on the scripture context, dynamically choose the appropriate entity columns for validation:
|
| 83 |
+
- **Divya Prabandham** → azhwar, prabandham, location/deity
|
| 84 |
+
- **Bhagavad Gita** → chapter, verse number(s), speaker, listener
|
| 85 |
+
- **Upanishads** → section, mantra number, rishi, deity
|
| 86 |
+
- **Ramayana/Mahabharata** → book/kanda, section/sarga, character(s), location
|
| 87 |
+
- **Other** → pick the 3–4 most relevant contextual entities from the scripture’s metadata.
|
| 88 |
+
4. Verify (from `original_llm_response`):
|
| 89 |
+
- Correct verse number(s)
|
| 90 |
+
- Keyword/context match
|
| 91 |
+
- All scripture-specific entity fields
|
| 92 |
+
- Native verse text quality
|
| 93 |
+
5. **Repair any garbled Tamil/Sanskrit characters** in the verse:
|
| 94 |
+
- Restore correct letters, diacritics, and punctuation.
|
| 95 |
+
- Replace broken Unicode with proper characters.
|
| 96 |
+
- Correct vowel signs, consonants, and pulli markers.
|
| 97 |
+
- Preserve original spacing and line breaks.
|
| 98 |
+
The repaired version is `fixed_llm_response`.
|
| 99 |
+
6. Output in this exact order:
|
| 100 |
+
---
|
| 101 |
+
<!-- **Step 1 – Repaired LLM Response in Markdown:** -->
|
| 102 |
+
<!-- BEGIN_MARKDOWN -->
|
| 103 |
+
fixed_llm_response
|
| 104 |
+
<!-- END_MARKDOWN -->
|
| 105 |
+
|
| 106 |
+
<!-- **Step 2 – Validation Table:** -->
|
| 107 |
+
<div style="font-size: small; opacity: 0.6;">
|
| 108 |
+
<hr>
|
| 109 |
+
<b>Original user query:</b> {{original_user_query}}
|
| 110 |
+
|
| 111 |
+
<table border="1" cellpadding="4" cellspacing="0" style="border-collapse: collapse; width: 100%;">
|
| 112 |
+
<tr>
|
| 113 |
+
<th>Expected</th>
|
| 114 |
+
<th>Found</th>
|
| 115 |
+
<th>Match?</th>
|
| 116 |
+
</tr>
|
| 117 |
+
<tr>
|
| 118 |
+
<td>verse number(s)</td>
|
| 119 |
+
<td>{{found_verse_numbers}}</td>
|
| 120 |
+
<td>{{match_status_for_verse}}</td>
|
| 121 |
+
</tr>
|
| 122 |
+
<tr>
|
| 123 |
+
<td>keyword/context</td>
|
| 124 |
+
<td>{{found_keyword}}</td>
|
| 125 |
+
<td>{{match_status_for_keyword}}</td>
|
| 126 |
+
</tr>
|
| 127 |
+
{{dynamic_entity_rows}}
|
| 128 |
+
<tr>
|
| 129 |
+
<td>native verse text</td>
|
| 130 |
+
<td style="white-space: normal; word-break: break-word;">{{cleaned_native_text}}</td>
|
| 131 |
+
<td>{{garbled_fix_status}}</td>
|
| 132 |
+
</tr>
|
| 133 |
+
</table>
|
| 134 |
+
|
| 135 |
+
<p><b>Verdict:</b> {{Verdict}}<br>
|
| 136 |
+
<b>Confidence score:</b> {{Confidence}}% – {{Justification}}<br>
|
| 137 |
+
<span style="background-color:{{badge_color_code}}; color:white; padding:2px 6px; border-radius:4px;">{{badge_emoji}}</span></p>
|
| 138 |
+
</div>
|
| 139 |
+
|
| 140 |
+
---
|
| 141 |
+
|
| 142 |
+
Where:
|
| 143 |
+
- `{{dynamic_entity_rows}}` is context-specific entity rows.
|
| 144 |
+
- `{{cleaned_native_text}}` must be taken from the repaired `fixed_llm_response`.
|
| 145 |
+
- ✅, ❌, ⚠️ remain for matches.
|
| 146 |
+
- Hidden markers (`<!-- BEGIN_MARKDOWN -->`) make it parseable without showing literal marker text.
|
| 147 |
+
|
| 148 |
+
|
| 149 |
"""
|
| 150 |
)
|
| 151 |
),
|
sanatan_assistant.py
CHANGED
|
@@ -94,14 +94,14 @@ Respond in **Markdown** format only. Ensure Sanskrit/Tamil verses are always cle
|
|
| 94 |
return prompt
|
| 95 |
|
| 96 |
|
| 97 |
-
def query(collection_name: allowedCollections, query: str, n_results=
|
| 98 |
"""
|
| 99 |
Search a scripture collection.
|
| 100 |
|
| 101 |
Parameters:
|
| 102 |
- collection_name (str): The name of the scripture collection to search. ...
|
| 103 |
- query (str): The search query.
|
| 104 |
-
- n_results (int): Number of results to return. Default is
|
| 105 |
|
| 106 |
Returns:
|
| 107 |
- A list of matching results.
|
|
@@ -126,7 +126,7 @@ def query_by_metadata_field(
|
|
| 126 |
"$in", "$eq", "$gt", "$gte", "$lt", "$lte", "$ne", "$nin"
|
| 127 |
],
|
| 128 |
metadata_value: Any,
|
| 129 |
-
n_results=
|
| 130 |
):
|
| 131 |
"""
|
| 132 |
Search a scripture collection by metadata. Do NOT use this for semantic search. Only use when a specific metadata field is provided.
|
|
@@ -137,7 +137,7 @@ def query_by_metadata_field(
|
|
| 137 |
- metadata_field (str) : The name of the metadata field. e.g. azhwar_name
|
| 138 |
- metadata_search_operator (str) : The search operator e.g. $eq or $in. DO NOT use $regex.
|
| 139 |
- metadata_value : Value to search for can be any primitive datatype like str or int (or a list[str] if metadata_search_operator = '$in'). for e.g. Thirumangai Azhwar or '2233' or 2233
|
| 140 |
-
- n_results (int): Number of results to return. Default is
|
| 141 |
|
| 142 |
Returns:
|
| 143 |
- A list of matching results.
|
|
@@ -169,7 +169,7 @@ def query_by_metadata_field(
|
|
| 169 |
def query_by_literal_text(
|
| 170 |
collection_name: allowedCollections,
|
| 171 |
literal_to_search_for: str,
|
| 172 |
-
n_results=
|
| 173 |
):
|
| 174 |
"""
|
| 175 |
Search a scripture collection by a literal. Do NOT use this for semantic search. Only use when the user specifically asks for literal search.
|
|
@@ -177,7 +177,7 @@ def query_by_literal_text(
|
|
| 177 |
Parameters:
|
| 178 |
- collection_name (str): The name of the scripture collection to search. ...
|
| 179 |
- literal_to_search_for (str): The search query.
|
| 180 |
-
- n_results (int): Number of results to return. Default is
|
| 181 |
|
| 182 |
Returns:
|
| 183 |
- A list of matching results.
|
|
|
|
| 94 |
return prompt
|
| 95 |
|
| 96 |
|
| 97 |
+
def query(collection_name: allowedCollections, query: str, n_results=3):
|
| 98 |
"""
|
| 99 |
Search a scripture collection.
|
| 100 |
|
| 101 |
Parameters:
|
| 102 |
- collection_name (str): The name of the scripture collection to search. ...
|
| 103 |
- query (str): The search query.
|
| 104 |
+
- n_results (int): Number of results to return. Default is 3.
|
| 105 |
|
| 106 |
Returns:
|
| 107 |
- A list of matching results.
|
|
|
|
| 126 |
"$in", "$eq", "$gt", "$gte", "$lt", "$lte", "$ne", "$nin"
|
| 127 |
],
|
| 128 |
metadata_value: Any,
|
| 129 |
+
n_results=3,
|
| 130 |
):
|
| 131 |
"""
|
| 132 |
Search a scripture collection by metadata. Do NOT use this for semantic search. Only use when a specific metadata field is provided.
|
|
|
|
| 137 |
- metadata_field (str) : The name of the metadata field. e.g. azhwar_name
|
| 138 |
- metadata_search_operator (str) : The search operator e.g. $eq or $in. DO NOT use $regex.
|
| 139 |
- metadata_value : Value to search for can be any primitive datatype like str or int (or a list[str] if metadata_search_operator = '$in'). for e.g. Thirumangai Azhwar or '2233' or 2233
|
| 140 |
+
- n_results (int): Number of results to return. Default is 3.
|
| 141 |
|
| 142 |
Returns:
|
| 143 |
- A list of matching results.
|
|
|
|
| 169 |
def query_by_literal_text(
|
| 170 |
collection_name: allowedCollections,
|
| 171 |
literal_to_search_for: str,
|
| 172 |
+
n_results=3,
|
| 173 |
):
|
| 174 |
"""
|
| 175 |
Search a scripture collection by a literal. Do NOT use this for semantic search. Only use when the user specifically asks for literal search.
|
|
|
|
| 177 |
Parameters:
|
| 178 |
- collection_name (str): The name of the scripture collection to search. ...
|
| 179 |
- literal_to_search_for (str): The search query.
|
| 180 |
+
- n_results (int): Number of results to return. Default is 3.
|
| 181 |
|
| 182 |
Returns:
|
| 183 |
- A list of matching results.
|