Spaces:
Sleeping
Sleeping
GlitchGhost
commited on
Upload 2 files
Browse files
Gita.xlsx
ADDED
Binary file (265 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import textwrap
|
3 |
+
import pandas as pd
|
4 |
+
import time
|
5 |
+
from sentence_transformers import SentenceTransformer, util
|
6 |
+
from annoy import AnnoyIndex
|
7 |
+
|
8 |
+
footer = """
|
9 |
+
<p style='text-align: center; color: gray;'>Made with inspiration by Abhijeet Singh</p>
|
10 |
+
"""
|
11 |
+
|
12 |
+
shlok_keys = ['Title', 'Chapter', 'Verse', 'Hindi Anuvad' , 'Enlgish Translation']
|
13 |
+
max_line_length = 100 # Adjust as needed
|
14 |
+
|
15 |
+
@st.cache_resource
|
16 |
+
def load_data():
|
17 |
+
hn_filepath = 'Gita.xlsx'
|
18 |
+
return pd.read_excel(hn_filepath)
|
19 |
+
|
20 |
+
|
21 |
+
@st.cache_resource
|
22 |
+
def load_hn_model():
|
23 |
+
return SentenceTransformer('all-mpnet-base-v2')
|
24 |
+
|
25 |
+
hn_model = load_hn_model()
|
26 |
+
|
27 |
+
@st.cache_resource
|
28 |
+
def build_embeddings(hn_data):
|
29 |
+
return [hn_model.encode(hn_data['Enlgish Translation'][i], convert_to_tensor=True).numpy() for i in range(len(hn_data))]
|
30 |
+
|
31 |
+
@st.cache_resource
|
32 |
+
def build_annoy_index(shloka_embeddings):
|
33 |
+
embedding_size = len(shloka_embeddings[0])
|
34 |
+
annoy_index = AnnoyIndex(embedding_size, metric='angular')
|
35 |
+
for i, embedding in enumerate(shloka_embeddings):
|
36 |
+
annoy_index.add_item(i, embedding)
|
37 |
+
annoy_index.build(18) # 18 trees for faster search
|
38 |
+
return annoy_index
|
39 |
+
|
40 |
+
def wrap_text(text):
|
41 |
+
pass
|
42 |
+
# st.write("shree ganeshay namah")
|
43 |
+
|
44 |
+
hn_data = load_data()
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
shloka_embeddings = build_embeddings(hn_data)
|
49 |
+
annoy_index = build_annoy_index(shloka_embeddings)
|
50 |
+
|
51 |
+
st.title("GitaShlok Bhagavad Gita Assistant")
|
52 |
+
|
53 |
+
st.markdown(footer, unsafe_allow_html=True)
|
54 |
+
|
55 |
+
st.markdown(
|
56 |
+
"""
|
57 |
+
<style>
|
58 |
+
.reportview-container {
|
59 |
+
width: 90%;
|
60 |
+
}
|
61 |
+
</style>
|
62 |
+
""",
|
63 |
+
unsafe_allow_html=True
|
64 |
+
)
|
65 |
+
st.markdown(
|
66 |
+
"""
|
67 |
+
<style>
|
68 |
+
.streamlit-text-container {
|
69 |
+
white-space: pre-line;
|
70 |
+
}
|
71 |
+
</style>
|
72 |
+
""",
|
73 |
+
unsafe_allow_html=True
|
74 |
+
)
|
75 |
+
query = st.text_input("Ask any question related to the Bhagavad Gita: ")
|
76 |
+
|
77 |
+
if st.button('Ask'):
|
78 |
+
|
79 |
+
query_embedding = hn_model.encode(query, convert_to_tensor=True).numpy()
|
80 |
+
|
81 |
+
# Use Annoy Index for efficient similarity search
|
82 |
+
similar_indices = annoy_index.get_nns_by_vector(query_embedding, 18)
|
83 |
+
|
84 |
+
# Process and display similar Shlokas
|
85 |
+
similarities = []
|
86 |
+
for curr_index in similar_indices:
|
87 |
+
similarity = util.cos_sim(query_embedding, shloka_embeddings[curr_index])
|
88 |
+
curr_shlok_details = {key: hn_data[key][curr_index] for key in hn_data}
|
89 |
+
similarities.append({"shlok_details": curr_shlok_details, "similarity": similarity})
|
90 |
+
|
91 |
+
# Get the most similar Shloka
|
92 |
+
top_result = sorted(similarities, key=lambda x: x["similarity"], reverse=True)[0]
|
93 |
+
top_shlok_details = top_result["shlok_details"]
|
94 |
+
adhyay_number = top_shlok_details['Chapter'].split(" ")[1]
|
95 |
+
shlok_number = top_shlok_details['Verse'].split(" ")[1].split(".")[1]
|
96 |
+
|
97 |
+
st.write("------------------------------")
|
98 |
+
st.write(f"{top_shlok_details['Chapter']} , Shloka : {shlok_number}")
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
wrapped_text = textwrap.fill(top_shlok_details['Enlgish Translation'], width=max_line_length)
|
104 |
+
wrapped_hindi_text=textwrap.fill(top_shlok_details['Hindi Anuvad'], width=max_line_length)
|
105 |
+
|
106 |
+
placeholder = st.empty()
|
107 |
+
|
108 |
+
prev_text=''
|
109 |
+
for char in wrapped_text:
|
110 |
+
prev_text=prev_text+char
|
111 |
+
placeholder.text(prev_text)
|
112 |
+
time.sleep(0.01) # Adjust the sleep duration as needed
|
113 |
+
st.write("\n------------------------------")
|
114 |
+
|
115 |
+
hindi_placeholder = st.empty()
|
116 |
+
|
117 |
+
hindi_text=''
|
118 |
+
for char in wrapped_hindi_text :
|
119 |
+
hindi_text=hindi_text+char
|
120 |
+
hindi_placeholder.text(hindi_text)
|
121 |
+
time.sleep(0.005) # Adjust the sleep duration as needed
|
122 |
+
st.write("\n------------------------------")
|
123 |
+
|
124 |
+
|
125 |
+
|