Spaces:
Sleeping
Sleeping
File size: 1,769 Bytes
2c2cc17 a763607 2c2cc17 a763607 2c2cc17 a763607 2c2cc17 a763607 2c2cc17 a763607 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import streamlit as st
import pandas as pd
from sentence_transformers import SentenceTransformer, util
from gtts import gTTS
import tempfile
import os
# Load the Gita dataset
@st.cache_data
def load_data():
return pd.read_csv("Bhagwad_Gita.csv")
data = load_data()
# Load the embedding model
@st.cache_resource
def load_model():
return SentenceTransformer('all-MiniLM-L6-v2')
model = load_model()
# Preprocess verses and create embeddings
@st.cache_data
def get_embeddings(data):
verses = data['Verse'].astype(str).tolist()
embeddings = model.encode(verses, convert_to_tensor=True)
return verses, embeddings
verses, verse_embeddings = get_embeddings(data)
# App Title
st.title("π GeetaGPT β Divine Wisdom from the Bhagavad Gita")
# User Input
user_question = st.text_input("Ask your question to Lord Krishna:")
if user_question:
# Embed the question
question_embedding = model.encode(user_question, convert_to_tensor=True)
# Compute cosine similarities
scores = util.pytorch_cos_sim(question_embedding, verse_embeddings)[0]
best_idx = scores.argmax().item()
# Get the best matching verse
matched_verse = verses[best_idx]
chapter = data.iloc[best_idx]['Chapter']
verse_number = data.iloc[best_idx]['Verse Number']
# Greet and show result
greeting = "ποΈ Jai Shri Krishna!\n\n"
st.markdown(f"{greeting}**Chapter {chapter}, Verse {verse_number}:**\n\n*{matched_verse}*")
# Optional: Generate audio
if st.checkbox("π Hear it aloud"):
tts = gTTS(text=matched_verse, lang='en')
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
tts.save(fp.name)
st.audio(fp.name, format="audio/mp3")
os.remove(fp.name)
|