Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,60 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
from sentence_transformers import SentenceTransformer
|
| 3 |
-
import faiss
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
from gtts import gTTS
|
| 6 |
-
import
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
@st.cache_resource
|
| 10 |
-
def load_data_and_model():
|
| 11 |
-
df = pd.read_csv("Bhagwad_Gita.csv")
|
| 12 |
-
|
| 13 |
-
documents = []
|
| 14 |
-
for _, row in df.iterrows():
|
| 15 |
-
doc = {
|
| 16 |
-
"verse": f"Chapter {row['Chapter']}, Verse {row['Verse']}",
|
| 17 |
-
"shloka": row['Shloka'],
|
| 18 |
-
"eng": row['EngMeaning'],
|
| 19 |
-
"hindi": row.get('HindiMeaning', 'हिंदी अनुवाद उपलब्ध नहीं है।')
|
| 20 |
-
}
|
| 21 |
-
documents.append(doc)
|
| 22 |
-
|
| 23 |
-
texts = [f"{doc['verse']}\n{doc['shloka']}\n{doc['eng']}" for doc in documents]
|
| 24 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 25 |
-
embeddings = model.encode(texts, show_progress_bar=False)
|
| 26 |
-
index = faiss.IndexFlatL2(embeddings[0].shape[0])
|
| 27 |
-
index.add(embeddings)
|
| 28 |
-
|
| 29 |
-
return model, index, documents
|
| 30 |
-
|
| 31 |
-
def generate_audio(text):
|
| 32 |
-
tts = gTTS(text)
|
| 33 |
-
buf = BytesIO()
|
| 34 |
-
tts.write_to_fp(buf)
|
| 35 |
-
return buf.getvalue()
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
|
| 42 |
-
</audio>
|
| 43 |
-
"""
|
| 44 |
-
st.markdown(md, unsafe_allow_html=True)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
model, index, documents = load_data_and_model()
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
st.
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sentence_transformers import SentenceTransformer, util
|
| 4 |
from gtts import gTTS
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load the Gita dataset
|
| 9 |
+
@st.cache_data
|
| 10 |
+
def load_data():
|
| 11 |
+
return pd.read_csv("Bhagwad_Gita.csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
data = load_data()
|
|
|
|
| 14 |
|
| 15 |
+
# Load the embedding model
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_model():
|
| 18 |
+
return SentenceTransformer('all-MiniLM-L6-v2')
|
| 19 |
+
|
| 20 |
+
model = load_model()
|
| 21 |
+
|
| 22 |
+
# Preprocess verses and create embeddings
|
| 23 |
+
@st.cache_data
|
| 24 |
+
def get_embeddings(data):
|
| 25 |
+
verses = data['Verse'].astype(str).tolist()
|
| 26 |
+
embeddings = model.encode(verses, convert_to_tensor=True)
|
| 27 |
+
return verses, embeddings
|
| 28 |
+
|
| 29 |
+
verses, verse_embeddings = get_embeddings(data)
|
| 30 |
+
|
| 31 |
+
# App Title
|
| 32 |
+
st.title("🙏 GeetaGPT – Divine Wisdom from the Bhagavad Gita")
|
| 33 |
+
|
| 34 |
+
# User Input
|
| 35 |
+
user_question = st.text_input("Ask your question to Lord Krishna:")
|
| 36 |
+
|
| 37 |
+
if user_question:
|
| 38 |
+
# Embed the question
|
| 39 |
+
question_embedding = model.encode(user_question, convert_to_tensor=True)
|
| 40 |
+
|
| 41 |
+
# Compute cosine similarities
|
| 42 |
+
scores = util.pytorch_cos_sim(question_embedding, verse_embeddings)[0]
|
| 43 |
+
best_idx = scores.argmax().item()
|
| 44 |
+
|
| 45 |
+
# Get the best matching verse
|
| 46 |
+
matched_verse = verses[best_idx]
|
| 47 |
+
chapter = data.iloc[best_idx]['Chapter']
|
| 48 |
+
verse_number = data.iloc[best_idx]['Verse Number']
|
| 49 |
+
|
| 50 |
+
# Greet and show result
|
| 51 |
+
greeting = "🕉️ Jai Shri Krishna!\n\n"
|
| 52 |
+
st.markdown(f"{greeting}**Chapter {chapter}, Verse {verse_number}:**\n\n*{matched_verse}*")
|
| 53 |
+
|
| 54 |
+
# Optional: Generate audio
|
| 55 |
+
if st.checkbox("🔊 Hear it aloud"):
|
| 56 |
+
tts = gTTS(text=matched_verse, lang='en')
|
| 57 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 58 |
+
tts.save(fp.name)
|
| 59 |
+
st.audio(fp.name, format="audio/mp3")
|
| 60 |
+
os.remove(fp.name)
|