Spaces:
Sleeping
Sleeping
import gradio as gr | |
import pandas as pd | |
import uuid | |
import shutil | |
import edge_tts | |
import re | |
from sentence_transformers import SentenceTransformer, util | |
import asyncio | |
# Load Gita dataset | |
df = pd.read_csv("bhagavad_gita.csv") | |
# Load model | |
model = SentenceTransformer("all-MiniLM-L6-v2") | |
verse_embeddings = model.encode(df['meaning_in_english'].tolist(), convert_to_tensor=True) | |
# Background music path | |
bg_music_path = "krishna_bg_music.mp3" | |
def shorten_explanation(text, max_sentences=2): | |
return '. '.join(text.split('. ')[:max_sentences]).strip() + "." | |
def clean_english(text): | |
return re.sub(r'[^\x00-\x7F]+', ' ', text) | |
def generate_voice_sync(text): | |
voice = "en-IN-PrabhatNeural" | |
filename = f"{uuid.uuid4()}.mp3" | |
communicate = edge_tts.Communicate(text, voice=voice) | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
loop.run_until_complete(communicate.save(filename)) | |
loop.close() | |
return filename | |
def get_unique_bgm(): | |
unique_path = f"bgm_{uuid.uuid4()}.mp3" | |
shutil.copy(bg_music_path, unique_path) | |
return unique_path | |
def versewise_bot(question, play_music): | |
if not question.strip(): | |
return "Please ask a question.", None, None | |
query_embedding = model.encode(question, convert_to_tensor=True) | |
similarity_scores = util.pytorch_cos_sim(query_embedding, verse_embeddings)[0] | |
idx = similarity_scores.argmax().item() | |
verse = df.iloc[idx] | |
sanskrit = verse['verse_in_sanskrit'] | |
translation = verse['translation_in_english'] | |
explanation = shorten_explanation(verse['meaning_in_english']) | |
verse_number = verse['verse_number'] | |
reply = f"""π *Bhagavad Gita {verse_number}* | |
π "{sanskrit[:60]}..." | |
*"{translation}"* | |
π {explanation} | |
πΌ Stay strong β Krishna walks with you.""" | |
voice_text = f"{clean_english(translation)}. {clean_english(explanation)}" | |
audio_path = generate_voice_sync(voice_text) | |
music = get_unique_bgm() if play_music else None | |
return reply, audio_path, music | |
def get_quote_of_the_day(): | |
verse = df.sample(1).iloc[0] | |
sanskrit = verse['verse_in_sanskrit'] | |
translation = verse['translation_in_english'] | |
verse_number = verse['verse_number'] | |
return f"""<div style="font-size:1.1em;padding:10px 0; color:#000;"><b>Quote of the Day (Gita {verse_number}):</b><br> | |
<span style="color:#000;"><i>{sanskrit[:60]}...</i></span><br> | |
<span style="color:#000;">"{translation}"</span></div>""" | |
custom_css = """ | |
body, .gradio-container { | |
background-image: url('https://static.vecteezy.com/system/resources/previews/022/592/272/non_2x/image-of-divine-beautiful-closed-eyes-blue-colored-krishna-generative-ai-free-photo.jpeg'); | |
background-size: cover; | |
background-repeat: no-repeat; | |
background-position: center; | |
} | |
.gradio-container, .gradio-interface { | |
background-color: rgba(255,255,255,0.90) !important; | |
border-radius: 18px; | |
padding: 25px; | |
max-width: 760px; | |
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2); | |
} | |
.gradio-container h1, .gradio-container .prose, .gradio-container .prose span, .gradio-container .prose p, .gradio-container b, .gradio-container i { | |
color: #000 !important; | |
} | |
@media only screen and (max-width: 768px) { | |
.gradio-container { | |
width: 99% !important; | |
padding: 10px !important; | |
max-width: 100vw !important; | |
} | |
} | |
input[type="text"], textarea { | |
font-size: 16px !important; | |
} | |
""" | |
interface = gr.Interface( | |
fn=versewise_bot, | |
inputs=[ | |
gr.Textbox(label="Ask Krishna", placeholder="Why am I struggling in life?", lines=2), | |
gr.Checkbox(label="Play Background Music", value=True) | |
], | |
outputs=[ | |
gr.Textbox(label="π§ββ Krishna's Answer"), | |
gr.Audio(label="π Listen to Krishna's Voice"), | |
gr.Audio(label="πΆ Background Music"), | |
], | |
title="π VerseWise - Divine Wisdom from the Gita", | |
description='<span style="color:#000;">Ask any question, and receive a Gita verse with Krishnaβs loving wisdom.</span>', | |
article=get_quote_of_the_day(), | |
allow_flagging="never", | |
theme="soft", | |
css=custom_css, | |
fill_width=True | |
) | |
if __name__ == "__main__": | |
interface.launch(show_error=True) | |