Spaces:
Sleeping
Sleeping
File size: 4,190 Bytes
e36324f dda18cb 9aff940 dda18cb 9aff940 b90d89b c3e977e 8fb76d7 9aff940 c3e977e 6c732eb 9aff940 dda18cb 9aff940 c3e977e 9aff940 c3e977e 9aff940 c3e977e 9aff940 c3e977e 9aff940 c3e977e 9aff940 dda18cb 9aff940 dda18cb 9aff940 e36324f dda18cb 9aff940 860fba2 c3e977e |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
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)
|