Anupam251272 commited on
Commit
86065fe
1 Parent(s): 1610563

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +259 -0
app.py ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ import logging
4
+ from sklearn.feature_extraction.text import TfidfVectorizer
5
+ from sklearn.metrics.pairwise import cosine_similarity
6
+ from transformers import MarianMTModel, MarianTokenizer
7
+ from PIL import Image, ImageDraw, ImageFont
8
+ import textwrap
9
+ import os
10
+ from gtts import gTTS
11
+ import tempfile
12
+ from diffusers import DiffusionPipeline
13
+
14
+ # Configure logging
15
+ logging.basicConfig(level=logging.INFO)
16
+ logger = logging.getLogger(__name__)
17
+
18
+ class MultilingualGitaAnalyzer:
19
+ def __init__(self, csv_path='/content/Bhagwad_Gita.csv'):
20
+ """Initialize the Bhagavad Gita Analyzer with multilingual support."""
21
+ logger.info("Initializing Multilingual Bhagavad Gita Analyzer")
22
+ self.df = None
23
+ self.vectorizer = None
24
+ self.tfidf_matrix = None
25
+ self.translation_model_to_en = None
26
+ self.translation_tokenizer_to_en = None
27
+ self.translation_models_from_en = {}
28
+ self.translation_tokenizers_from_en = {}
29
+ self.load_translation_models()
30
+ self.load_dataset(csv_path)
31
+
32
+ def load_translation_models(self):
33
+ """Load translation models for multilingual support."""
34
+ try:
35
+ # Load MarianMT for translations (multi-language to English)
36
+ logger.info("Loading MarianMT translation model for multi-language to English...")
37
+ self.translation_tokenizer_to_en = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
38
+ self.translation_model_to_en = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
39
+ logger.info("Translation model for multi-language to English loaded successfully.")
40
+
41
+ # Load MarianMT for translations from English to other languages
42
+ languages = ["hi", "es", "fr"] # Hindi, Spanish, French
43
+ for lang in languages:
44
+ model_name = f"Helsinki-NLP/opus-mt-en-{lang}"
45
+ logger.info(f"Loading MarianMT translation model for English to {lang}...")
46
+ self.translation_tokenizers_from_en[lang] = MarianTokenizer.from_pretrained(model_name)
47
+ self.translation_models_from_en[lang] = MarianMTModel.from_pretrained(model_name)
48
+ logger.info(f"Translation model for English to {lang} loaded successfully.")
49
+ except Exception as e:
50
+ logger.error(f"Error loading translation models: {e}")
51
+ self.translation_tokenizer_to_en = None
52
+ self.translation_model_to_en = None
53
+ self.translation_models_from_en = {}
54
+ self.translation_tokenizers_from_en = {}
55
+
56
+ def translate_to_english(self, text):
57
+ """Translate text to English using MarianMT."""
58
+ try:
59
+ if not self.translation_model_to_en or not self.translation_tokenizer_to_en:
60
+ return text # Fallback: return original text
61
+ inputs = self.translation_tokenizer_to_en(text, return_tensors="pt", truncation=True)
62
+ outputs = self.translation_model_to_en.generate(**inputs)
63
+ return self.translation_tokenizer_to_en.decode(outputs[0], skip_special_tokens=True)
64
+ except Exception as e:
65
+ logger.error(f"Error during translation to English: {e}")
66
+ return text
67
+
68
+ def translate_from_english(self, text, target_language="en"):
69
+ """Translate from English to the selected target language."""
70
+ try:
71
+ if target_language == "en":
72
+ return text # No translation needed for English
73
+
74
+ lang_code = {"Hindi": "hi", "Spanish": "es", "French": "fr"}.get(target_language, "en")
75
+ if lang_code == "en":
76
+ return text
77
+
78
+ # If translation model is available, use it for translation
79
+ tokenizer = self.translation_tokenizers_from_en.get(lang_code)
80
+ model = self.translation_models_from_en.get(lang_code)
81
+ if not tokenizer or not model:
82
+ return text # Fallback: return original text
83
+
84
+ inputs = tokenizer(text, return_tensors="pt", truncation=True)
85
+ outputs = model.generate(**inputs)
86
+ translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
87
+
88
+ return translated_text
89
+ except Exception as e:
90
+ logger.error(f"Error during reverse translation: {e}")
91
+ return text
92
+
93
+ def load_dataset(self, csv_path):
94
+ """Load and preprocess the Bhagavad Gita dataset."""
95
+ try:
96
+ self.df = pd.read_csv(csv_path)
97
+ logger.info(f"Dataset loaded successfully with {len(self.df)} rows.")
98
+
99
+ required_columns = {'ID', 'Chapter', 'Verse', 'EngMeaning'}
100
+ missing_columns = required_columns - set(self.df.columns)
101
+ if missing_columns:
102
+ raise ValueError(f"Missing required columns: {missing_columns}")
103
+
104
+ self.df['Chapter'] = self.df['Chapter'].fillna('').astype(str)
105
+ self.df['Verse'] = self.df['Verse'].fillna('').astype(str)
106
+ self.df['EngMeaning'] = self.df['EngMeaning'].fillna('').astype(str)
107
+
108
+ self.vectorizer = TfidfVectorizer(stop_words='english')
109
+ self.tfidf_matrix = self.vectorizer.fit_transform(self.df['EngMeaning'])
110
+ except Exception as e:
111
+ logger.error(f"Error loading dataset: {e}")
112
+ self.df = None
113
+ self.vectorizer = None
114
+ self.tfidf_matrix = None
115
+
116
+ def semantic_search(self, query, top_k=3):
117
+ """Search for similar verses based on a query."""
118
+ try:
119
+ if self.df is None or self.vectorizer is None or self.tfidf_matrix is None:
120
+ return ["Dataset not loaded or vectorizer not initialized."]
121
+
122
+ query_vector = self.vectorizer.transform([query])
123
+ cosine_similarities = cosine_similarity(query_vector, self.tfidf_matrix).flatten()
124
+ top_indices = cosine_similarities.argsort()[-top_k:][::-1]
125
+
126
+ results = []
127
+ for idx in top_indices:
128
+ verse = self.df.iloc[idx]
129
+ chapter = verse['Chapter']
130
+ verse_number = verse['Verse']
131
+ eng_meaning = verse['EngMeaning']
132
+ results.append(f"Chapter {chapter} - Verse {verse_number}: {eng_meaning}")
133
+ return results
134
+ except Exception as e:
135
+ logger.error(f"Error in semantic search: {e}")
136
+ return ["An error occurred during semantic search."]
137
+
138
+ def generate_image(self, text):
139
+ """Generate an image using the DiffusionPipeline."""
140
+ try:
141
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev")
142
+ pipe.load_lora_weights("adirik/flux-cinestill")
143
+ prompt = f"in the style of CNSTLL, {text}, night time, cinestill 800T"
144
+ image = pipe(prompt).images[0]
145
+ img_path = 'generated_image.png'
146
+ image.save(img_path)
147
+ return img_path
148
+ except Exception as e:
149
+ logger.error(f"Error generating image: {e}")
150
+ return None
151
+
152
+ def generate_audio(self, text):
153
+ """Generate audio from the given text."""
154
+ try:
155
+ tts = gTTS(text=text, lang='en')
156
+ audio_path = 'generated_audio.mp3'
157
+ tts.save(audio_path)
158
+ return audio_path
159
+ except Exception as e:
160
+ logger.error(f"Error generating audio: {e}")
161
+ return None
162
+
163
+ def gradio_interface(self):
164
+ """Create a Gradio interface for the Bhagavad Gita Analyzer."""
165
+ def process_query(question, preferred_language):
166
+ try:
167
+ # Translate the query to English if it's not in English
168
+ if preferred_language != "English":
169
+ question_in_english = self.translate_to_english(question)
170
+ logger.info(f"Translated query to English: {question_in_english}")
171
+ else:
172
+ question_in_english = question
173
+
174
+ # Perform semantic search
175
+ results = self.semantic_search(question_in_english)
176
+
177
+ # Translate the results back to the preferred language if needed
178
+ translated_results = [
179
+ self.translate_from_english(res, target_language=preferred_language)
180
+ for res in results
181
+ ]
182
+
183
+ # Provide an explanation in easy language
184
+ 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."
185
+ translated_explanation = self.translate_from_english(explanation, target_language=preferred_language)
186
+
187
+ # Generate an image for the first result
188
+ if translated_results:
189
+ image_path = self.generate_image(translated_results[0])
190
+ else:
191
+ image_path = None
192
+
193
+ # Generate audio for the first result
194
+ if translated_results:
195
+ audio_path = self.generate_audio(translated_results[0])
196
+ else:
197
+ audio_path = None
198
+
199
+ return "\n\n".join(translated_results) + "\n\n" + translated_explanation, image_path, audio_path
200
+ except Exception as e:
201
+ logger.error(f"Error processing query: {e}")
202
+ return "An unexpected error occurred.", None, None
203
+
204
+ def daily_quote():
205
+ # Placeholder for daily quote logic
206
+ return "Daily Quote: 'The journey of a thousand miles begins with one step.' - Lao Tzu"
207
+
208
+ def quiz():
209
+ # Placeholder for quiz logic
210
+ questions = [
211
+ {
212
+ "question": "What is the meaning of duty?",
213
+ "options": ["A responsibility", "A hobby", "A choice", "A luxury"],
214
+ "answer": "A responsibility"
215
+ },
216
+ # Add more questions here
217
+ ]
218
+ return questions
219
+
220
+ iface = gr.Blocks()
221
+
222
+ with iface:
223
+ gr.Markdown("# Multilingual Vedas Wisdom Finder")
224
+ gr.Markdown("Ask questions in any language and get wisdom in your preferred language.")
225
+
226
+ with gr.Row():
227
+ with gr.Column():
228
+ question = gr.Textbox(label="Ask a Question")
229
+ preferred_language = gr.Dropdown(["English", "Hindi", "Spanish", "French"], label="Preferred Language")
230
+ submit_btn = gr.Button("Submit")
231
+
232
+ with gr.Column():
233
+ output = gr.Textbox(label="Vedas Wisdom")
234
+ image_output = gr.Image(label="Relevant Image")
235
+ audio_output = gr.Audio(label="Relevant Audio")
236
+
237
+ submit_btn.click(process_query, inputs=[question, preferred_language], outputs=[output, image_output, audio_output])
238
+
239
+ gr.Markdown("## Daily Quote")
240
+ daily_quote_output = gr.Textbox(label="Daily Quote", value=daily_quote())
241
+
242
+ gr.Markdown("## Interactive Quiz")
243
+ quiz_questions = quiz()
244
+ for q in quiz_questions:
245
+ gr.Markdown(f"**{q['question']}**")
246
+ gr.Radio(q['options'], label="Select your answer")
247
+
248
+ gr.Markdown("## Audio Verses")
249
+ gr.Audio(label="Listen to a Verse", value="verse1.mp3") # Ensure this file exists in the same directory
250
+
251
+ return iface
252
+
253
+ def main():
254
+ analyzer = MultilingualGitaAnalyzer('/content/Bhagwad_Gita.csv')
255
+ interface = analyzer.gradio_interface()
256
+ interface.launch(share=True)
257
+
258
+ if __name__ == "__main__":
259
+ main()