Spaces:
Sleeping
Sleeping
samlonka
commited on
Commit
•
5e70d66
1
Parent(s):
23faca8
'functions'
Browse files
Tools.py
CHANGED
@@ -5,9 +5,9 @@ import streamlit as st
|
|
5 |
import pymysql
|
6 |
import ast
|
7 |
import re
|
8 |
-
from utils import word_sentence_similarity, get_list_meaning_word,
|
9 |
from llama_index.core.tools.tool_spec.base import BaseToolSpec
|
10 |
-
from database import execute_query
|
11 |
import pandas as pd
|
12 |
import json
|
13 |
import ast
|
@@ -138,12 +138,12 @@ class MantraToolSpec(BaseToolSpec):
|
|
138 |
filter_df = self._get_mantra_details_by_scripture(scripture_name=scripture_name, KandahNumber=KandahNumber,MandalaNumber=MandalaNumber, ArchikahNumber=ArchikahNumber,
|
139 |
ShuktaNumber=ShuktaNumber, PrapatakNumber=PrapatakNumber, MantraNumber=MantraNumber,
|
140 |
AnuvakNumber=AnuvakNumber, AdhyayaNumber=AdhyayaNumber)
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
return self._get_mantra_details(query)
|
145 |
except Exception as e:
|
146 |
-
return {"error": str(e)}
|
147 |
|
148 |
def get_vedamantra_summary(self, mantraid=None, scripture_name=None, KandahNumber=None,MandalaNumber=None, ArchikahNumber=None,
|
149 |
ShuktaNumber=None, PrapatakNumber=None, MantraNumber=None,
|
@@ -163,9 +163,12 @@ class MantraToolSpec(BaseToolSpec):
|
|
163 |
else:
|
164 |
filtered_df = self._get_mantra_details_by_scripture(scripture_name=scripture_name, KandahNumber=KandahNumber,MandalaNumber=MandalaNumber, ArchikahNumber=ArchikahNumber,
|
165 |
ShuktaNumber=ShuktaNumber, PrapatakNumber=PrapatakNumber, MantraNumber=MantraNumber,
|
166 |
-
AnuvakNumber=AnuvakNumber, AdhyayaNumber=AdhyayaNumber)
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
169 |
json_dict = get_details_mantra_json(query)
|
170 |
mantra_summary = json_dict['mantraSummary']['language']
|
171 |
summary_dict = {"Roman-IAST summary of vedamantra": json_dict['mantraSummary']['language'][1]}
|
@@ -177,6 +180,7 @@ class MantraToolSpec(BaseToolSpec):
|
|
177 |
except Exception as e:
|
178 |
return {"error": str(e)}
|
179 |
|
|
|
180 |
class PadaToolSpec(BaseToolSpec):
|
181 |
'''
|
182 |
Purpose: To obtains a complete or meaningful meaning of a word or pada based on context information.
|
@@ -198,15 +202,13 @@ class PadaToolSpec(BaseToolSpec):
|
|
198 |
self.df_vedic_content = pd.read_csv(VEDAMANTRA_CSV_PATH,encoding = 'utf-8')
|
199 |
|
200 |
def _get_pada_details_by_scripture(self, pada, scripture_name=None, KandahNumber=None,MandalaNumber=None, ArchikahNumber=None,
|
201 |
-
|
202 |
-
AnuvakNumber=None, AdhyayaNumber=None):
|
203 |
try:
|
204 |
pada = iast_process(pada)
|
205 |
condition = (self.df_terms['Pada'] == pada)
|
206 |
|
207 |
if scripture_name:
|
208 |
condition &= (self.df_terms['scripture_name'].str.lower() == scripture_name.lower())
|
209 |
-
|
210 |
if KandahNumber:
|
211 |
condition &= (self.df_terms['KandahNumber'] == KandahNumber)
|
212 |
if MandalaNumber:
|
@@ -256,10 +258,10 @@ class PadaToolSpec(BaseToolSpec):
|
|
256 |
if count >= best_count:
|
257 |
best_meaning = {cat: meanings.get(cat, None) for cat in ['adibhautic', 'adidaivic', 'adhyatmic']}
|
258 |
best_count = count
|
259 |
-
return best_meaning if best_meaning else {"error": "Required meaning associated with vedamantra is not available."}
|
260 |
except Exception as e:
|
261 |
logging.error(f"Error in _get_vedamantra_meaning: {e}")
|
262 |
-
return json.
|
263 |
|
264 |
def _get_pada_morphology(self, term_details, meanings):
|
265 |
try:
|
@@ -282,7 +284,7 @@ class PadaToolSpec(BaseToolSpec):
|
|
282 |
return []
|
283 |
|
284 |
def get_pada_meaning(self, pada):
|
285 |
-
pada=iast_process(pada)
|
286 |
try:
|
287 |
pada_details = self.df_terms[self.df_terms['Pada'] == pada]
|
288 |
meanings_list = []
|
@@ -307,7 +309,7 @@ class PadaToolSpec(BaseToolSpec):
|
|
307 |
2. What is the adhyatmic meaning of the pada agnim in the context of the mantra whose id is '1.1.1.1?'
|
308 |
'''
|
309 |
try:
|
310 |
-
pada = iast_process(pada)
|
311 |
if mantraid:
|
312 |
details = self.df_terms[(self.df_terms['mantra_id'] == mantraid) & (self.df_terms['Pada'] == pada)]
|
313 |
else:
|
@@ -317,7 +319,7 @@ class PadaToolSpec(BaseToolSpec):
|
|
317 |
|
318 |
if not details.empty:
|
319 |
pada_details = details.iloc[0]
|
320 |
-
|
321 |
mantraID = pada_details['mantra_id']
|
322 |
meanings = self._get_vedamantra_meaning(mantraID,MahatmaName=MahatmaName)
|
323 |
if 'error' in meanings:
|
@@ -325,16 +327,16 @@ class PadaToolSpec(BaseToolSpec):
|
|
325 |
ab_term_morph_list = self._get_pada_morphology(pada_details, meanings['adibhautic'])
|
326 |
ad_term_morph_list = self._get_pada_morphology(pada_details, meanings['adidaivic'])
|
327 |
at_term_morph_list = self._get_pada_morphology(pada_details, meanings['adhyatmic'])
|
328 |
-
return {
|
329 |
f'adibhautic_info_{pada}': ab_term_morph_list,
|
330 |
'vedamantra_adibhautic_meaning': meanings['adibhautic'],
|
331 |
f'adidavic_info_{pada}': ad_term_morph_list,
|
332 |
'vedamantra_adidavic_meaning': meanings['adidaivic'],
|
333 |
f'adhyatmic_info_{pada}': at_term_morph_list,
|
334 |
'vedamantra_adhyatmic_meaning': meanings['adhyatmic']
|
335 |
-
}
|
336 |
else:
|
337 |
return json.dumps({"error": f"No details found for pada '{pada}'"})
|
338 |
except Exception as e:
|
339 |
logging.error(f"Error in get_adibauatic_adidaivic_adhyatmic_meaning_of_pada: {e}")
|
340 |
-
return json.dumps({"error": f"Failed to get meaning of the word {pada}. {e}"})
|
|
|
5 |
import pymysql
|
6 |
import ast
|
7 |
import re
|
8 |
+
from utils import word_sentence_similarity, get_list_meaning_word, iast_process
|
9 |
from llama_index.core.tools.tool_spec.base import BaseToolSpec
|
10 |
+
from database import execute_query, get_details_mantra_json
|
11 |
import pandas as pd
|
12 |
import json
|
13 |
import ast
|
|
|
138 |
filter_df = self._get_mantra_details_by_scripture(scripture_name=scripture_name, KandahNumber=KandahNumber,MandalaNumber=MandalaNumber, ArchikahNumber=ArchikahNumber,
|
139 |
ShuktaNumber=ShuktaNumber, PrapatakNumber=PrapatakNumber, MantraNumber=MantraNumber,
|
140 |
AnuvakNumber=AnuvakNumber, AdhyayaNumber=AdhyayaNumber)
|
141 |
+
if filter_df is not None:
|
142 |
+
mantra_id = filter_df.iloc[0]['mantra_number']
|
143 |
+
query = f"SELECT mantra_json FROM veda_content WHERE mantra_number = '{mantra_id}'"
|
144 |
return self._get_mantra_details(query)
|
145 |
except Exception as e:
|
146 |
+
return json.dumps({"error": str(e)})
|
147 |
|
148 |
def get_vedamantra_summary(self, mantraid=None, scripture_name=None, KandahNumber=None,MandalaNumber=None, ArchikahNumber=None,
|
149 |
ShuktaNumber=None, PrapatakNumber=None, MantraNumber=None,
|
|
|
163 |
else:
|
164 |
filtered_df = self._get_mantra_details_by_scripture(scripture_name=scripture_name, KandahNumber=KandahNumber,MandalaNumber=MandalaNumber, ArchikahNumber=ArchikahNumber,
|
165 |
ShuktaNumber=ShuktaNumber, PrapatakNumber=PrapatakNumber, MantraNumber=MantraNumber,
|
166 |
+
AnuvakNumber=AnuvakNumber, AdhyayaNumber=AdhyayaNumber)
|
167 |
+
if filtered_df is not None:
|
168 |
+
mantra_id = filtered_df.iloc[0]['mantra_number']
|
169 |
+
query = f"SELECT mantra_json FROM veda_content WHERE mantra_number = '{mantra_id}'"
|
170 |
+
else:
|
171 |
+
return None
|
172 |
json_dict = get_details_mantra_json(query)
|
173 |
mantra_summary = json_dict['mantraSummary']['language']
|
174 |
summary_dict = {"Roman-IAST summary of vedamantra": json_dict['mantraSummary']['language'][1]}
|
|
|
180 |
except Exception as e:
|
181 |
return {"error": str(e)}
|
182 |
|
183 |
+
|
184 |
class PadaToolSpec(BaseToolSpec):
|
185 |
'''
|
186 |
Purpose: To obtains a complete or meaningful meaning of a word or pada based on context information.
|
|
|
202 |
self.df_vedic_content = pd.read_csv(VEDAMANTRA_CSV_PATH,encoding = 'utf-8')
|
203 |
|
204 |
def _get_pada_details_by_scripture(self, pada, scripture_name=None, KandahNumber=None,MandalaNumber=None, ArchikahNumber=None,
|
205 |
+
ShuktaNumber=None, PrapatakNumber=None, MantraNumber=None,AnuvakNumber=None, AdhyayaNumber=None):
|
|
|
206 |
try:
|
207 |
pada = iast_process(pada)
|
208 |
condition = (self.df_terms['Pada'] == pada)
|
209 |
|
210 |
if scripture_name:
|
211 |
condition &= (self.df_terms['scripture_name'].str.lower() == scripture_name.lower())
|
|
|
212 |
if KandahNumber:
|
213 |
condition &= (self.df_terms['KandahNumber'] == KandahNumber)
|
214 |
if MandalaNumber:
|
|
|
258 |
if count >= best_count:
|
259 |
best_meaning = {cat: meanings.get(cat, None) for cat in ['adibhautic', 'adidaivic', 'adhyatmic']}
|
260 |
best_count = count
|
261 |
+
return best_meaning if best_meaning else json.dumps({"error": "Required meaning associated with vedamantra is not available."})
|
262 |
except Exception as e:
|
263 |
logging.error(f"Error in _get_vedamantra_meaning: {e}")
|
264 |
+
return json.dumps({"error": f"An error occurred: {e}"})
|
265 |
|
266 |
def _get_pada_morphology(self, term_details, meanings):
|
267 |
try:
|
|
|
284 |
return []
|
285 |
|
286 |
def get_pada_meaning(self, pada):
|
287 |
+
#pada=iast_process(pada)
|
288 |
try:
|
289 |
pada_details = self.df_terms[self.df_terms['Pada'] == pada]
|
290 |
meanings_list = []
|
|
|
309 |
2. What is the adhyatmic meaning of the pada agnim in the context of the mantra whose id is '1.1.1.1?'
|
310 |
'''
|
311 |
try:
|
312 |
+
#pada = iast_process(pada)
|
313 |
if mantraid:
|
314 |
details = self.df_terms[(self.df_terms['mantra_id'] == mantraid) & (self.df_terms['Pada'] == pada)]
|
315 |
else:
|
|
|
319 |
|
320 |
if not details.empty:
|
321 |
pada_details = details.iloc[0]
|
322 |
+
print(pada_details)
|
323 |
mantraID = pada_details['mantra_id']
|
324 |
meanings = self._get_vedamantra_meaning(mantraID,MahatmaName=MahatmaName)
|
325 |
if 'error' in meanings:
|
|
|
327 |
ab_term_morph_list = self._get_pada_morphology(pada_details, meanings['adibhautic'])
|
328 |
ad_term_morph_list = self._get_pada_morphology(pada_details, meanings['adidaivic'])
|
329 |
at_term_morph_list = self._get_pada_morphology(pada_details, meanings['adhyatmic'])
|
330 |
+
return json.dumps({
|
331 |
f'adibhautic_info_{pada}': ab_term_morph_list,
|
332 |
'vedamantra_adibhautic_meaning': meanings['adibhautic'],
|
333 |
f'adidavic_info_{pada}': ad_term_morph_list,
|
334 |
'vedamantra_adidavic_meaning': meanings['adidaivic'],
|
335 |
f'adhyatmic_info_{pada}': at_term_morph_list,
|
336 |
'vedamantra_adhyatmic_meaning': meanings['adhyatmic']
|
337 |
+
})
|
338 |
else:
|
339 |
return json.dumps({"error": f"No details found for pada '{pada}'"})
|
340 |
except Exception as e:
|
341 |
logging.error(f"Error in get_adibauatic_adidaivic_adhyatmic_meaning_of_pada: {e}")
|
342 |
+
return json.dumps({"error": f"Failed to get meaning of the word {pada}. {e}"})
|
app.py
CHANGED
@@ -154,7 +154,7 @@ context = """
|
|
154 |
isn't adequate, I'll indicate the insufficiency. It's important to note that responses based on "Implicit" thought are not sourced
|
155 |
from the 'Svarupa Knowledge base', and I'll caution you accordingly.
|
156 |
|
157 |
-
Additionally, I'll provide three follow-up questions tailored to the input query and context, aiming to deepen our exploration:
|
158 |
*****
|
159 |
You may also try the following questions:
|
160 |
1. Question1
|
|
|
154 |
isn't adequate, I'll indicate the insufficiency. It's important to note that responses based on "Implicit" thought are not sourced
|
155 |
from the 'Svarupa Knowledge base', and I'll caution you accordingly.
|
156 |
|
157 |
+
Additionally, I'll provide three follow-up questions tailored to the input query and context, aiming to deepen our exploration:
|
158 |
*****
|
159 |
You may also try the following questions:
|
160 |
1. Question1
|