File size: 10,670 Bytes
86065fe 7aa5f4c 86065fe 17e3432 86065fe 17e3432 86065fe 17e3432 86065fe 17e3432 86065fe 17e3432 86065fe 17e3432 86065fe 7aa5f4c 86065fe |
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
import pandas as pd
import gradio as gr
import logging
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from transformers import MarianMTModel, MarianTokenizer
from gtts import gTTS
import tempfile
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class MultilingualGitaAnalyzer:
def __init__(self, csv_path='Bhagwad_Gita.csv'):
"""Initialize the Bhagavad Gita Analyzer with multilingual support."""
logger.info("Initializing Multilingual Bhagavad Gita Analyzer")
self.df = None
self.vectorizer = None
self.tfidf_matrix = None
self.translation_model_to_en = None
self.translation_tokenizer_to_en = None
self.translation_models_from_en = {}
self.translation_tokenizers_from_en = {}
self.load_translation_models()
self.load_dataset(csv_path)
def load_translation_models(self):
"""Load translation models for multilingual support."""
try:
# Load MarianMT for translations (multi-language to English)
logger.info("Loading MarianMT translation model for multi-language to English...")
self.translation_tokenizer_to_en = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
self.translation_model_to_en = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
logger.info("Translation model for multi-language to English loaded successfully.")
# Load MarianMT for translations from English to other languages
languages = ["hi", "es", "fr"] # Hindi, Spanish, French
for lang in languages:
model_name = f"Helsinki-NLP/opus-mt-en-{lang}"
logger.info(f"Loading MarianMT translation model for English to {lang}...")
self.translation_tokenizers_from_en[lang] = MarianTokenizer.from_pretrained(model_name)
self.translation_models_from_en[lang] = MarianMTModel.from_pretrained(model_name)
logger.info(f"Translation model for English to {lang} loaded successfully.")
except Exception as e:
logger.error(f"Error loading translation models: {e}")
self.translation_tokenizer_to_en = None
self.translation_model_to_en = None
self.translation_models_from_en = {}
self.translation_tokenizers_from_en = {}
def translate_to_english(self, text):
"""Translate text to English using MarianMT."""
try:
if not self.translation_model_to_en or not self.translation_tokenizer_to_en:
return text # Fallback: return original text
inputs = self.translation_tokenizer_to_en(text, return_tensors="pt", truncation=True)
outputs = self.translation_model_to_en.generate(**inputs)
return self.translation_tokenizer_to_en.decode(outputs[0], skip_special_tokens=True)
except Exception as e:
logger.error(f"Error during translation to English: {e}")
return text
def translate_from_english(self, text, target_language="en"):
"""Translate from English to the selected target language."""
try:
if target_language == "en":
return text # No translation needed for English
lang_code = {"Hindi": "hi", "Spanish": "es", "French": "fr"}.get(target_language, "en")
if lang_code == "en":
return text
# If translation model is available, use it for translation
tokenizer = self.translation_tokenizers_from_en.get(lang_code)
model = self.translation_models_from_en.get(lang_code)
if not tokenizer or not model:
return text # Fallback: return original text
inputs = tokenizer(text, return_tensors="pt", truncation=True)
outputs = model.generate(**inputs)
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return translated_text
except Exception as e:
logger.error(f"Error during reverse translation: {e}")
return text
def load_dataset(self, csv_path):
"""Load and preprocess the Bhagavad Gita dataset."""
try:
self.df = pd.read_csv(csv_path)
logger.info(f"Dataset loaded successfully with {len(self.df)} rows.")
required_columns = {'ID', 'Chapter', 'Verse', 'EngMeaning'}
missing_columns = required_columns - set(self.df.columns)
if missing_columns:
raise ValueError(f"Missing required columns: {missing_columns}")
self.df['Chapter'] = self.df['Chapter'].fillna('').astype(str)
self.df['Verse'] = self.df['Verse'].fillna('').astype(str)
self.df['EngMeaning'] = self.df['EngMeaning'].fillna('').astype(str)
self.vectorizer = TfidfVectorizer(stop_words='english')
self.tfidf_matrix = self.vectorizer.fit_transform(self.df['EngMeaning'])
except Exception as e:
logger.error(f"Error loading dataset: {e}")
self.df = None
self.vectorizer = None
self.tfidf_matrix = None
def semantic_search(self, query, top_k=3):
"""Search for similar verses based on a query."""
try:
if self.df is None or self.vectorizer is None or self.tfidf_matrix is None:
return ["Dataset not loaded or vectorizer not initialized."]
query_vector = self.vectorizer.transform([query])
cosine_similarities = cosine_similarity(query_vector, self.tfidf_matrix).flatten()
top_indices = cosine_similarities.argsort()[-top_k:][::-1]
results = []
for idx in top_indices:
verse = self.df.iloc[idx]
chapter = verse['Chapter']
verse_number = verse['Verse']
eng_meaning = verse['EngMeaning']
results.append(f"Chapter {chapter} - Verse {verse_number}: {eng_meaning}")
return results
except Exception as e:
logger.error(f"Error in semantic search: {e}")
return ["An error occurred during semantic search."]
def generate_audio(self, text):
"""Generate audio from the given text."""
try:
tts = gTTS(text=text, lang='en')
audio_path = 'generated_audio.mp3'
tts.save(audio_path)
return audio_path
except Exception as e:
logger.error(f"Error generating audio: {e}")
return None
def gradio_interface(self):
"""Create a Gradio interface for the Bhagavad Gita Analyzer."""
def process_query(question, preferred_language):
try:
# Translate the query to English if it's not in English
if preferred_language != "English":
question_in_english = self.translate_to_english(question)
logger.info(f"Translated query to English: {question_in_english}")
else:
question_in_english = question
# Perform semantic search
results = self.semantic_search(question_in_english)
# Translate the results back to the preferred language if needed
translated_results = [
self.translate_from_english(res, target_language=preferred_language)
for res in results
]
# Provide an explanation in easy language
explanation = "In simple terms, duty means doing what you are supposed to do. It's like your responsibilities or tasks that you need to complete. In today's world, it could be your job, taking care of your family, or helping others. It's about doing the right thing, even when it's hard."
translated_explanation = self.translate_from_english(explanation, target_language=preferred_language)
# Combine results into a story format
story = "\n\n".join(translated_results) + "\n\n" + translated_explanation
# Generate audio for the story
audio_path = self.generate_audio(story)
return story, audio_path
except Exception as e:
logger.error(f"Error processing query: {e}")
return "An unexpected error occurred.", None
def daily_quote():
# Placeholder for daily quote logic
return "Daily Quote: 'The journey of a thousand miles begins with one step.' - Lao Tzu"
def quiz():
# Placeholder for quiz logic
questions = [
{
"question": "What is the meaning of duty?",
"options": ["A responsibility", "A hobby", "A choice", "A luxury"],
"answer": "A responsibility"
},
# Add more questions here
]
return questions
iface = gr.Blocks()
with iface:
gr.Markdown("# Multilingual Vedas Wisdom Finder")
gr.Markdown("Ask questions in any language and get wisdom in your preferred language.")
with gr.Row():
with gr.Column():
question = gr.Textbox(label="Ask a Question")
preferred_language = gr.Dropdown(["English", "Hindi", "Spanish", "French"], label="Preferred Language")
submit_btn = gr.Button("Submit")
with gr.Column():
output = gr.Textbox(label="Vedas Wisdom Story")
audio_output = gr.Audio(label="Relevant Audio")
submit_btn.click(process_query, inputs=[question, preferred_language], outputs=[output, audio_output])
gr.Markdown("## Daily Quote")
daily_quote_output = gr.Textbox(label="Daily Quote", value=daily_quote())
gr.Markdown("## Interactive Quiz")
quiz_questions = quiz()
for q in quiz_questions:
gr.Markdown(f"**{q['question']}**")
gr.Radio(q['options'], label="Select your answer")
gr.Markdown("## Audio Verses")
gr.Audio(label="Listen to a Verse", value="verse1.mp3") # Ensure this file exists in the same directory
return iface
def main():
analyzer = MultilingualGitaAnalyzer('Bhagwad_Gita.csv')
interface = analyzer.gradio_interface()
interface.launch(share=True)
if __name__ == "__main__":
main() |