from gtts import gTTS from typing import Dict, Any import re from deep_translator import GoogleTranslator import io class TextToSpeechConverter: def __init__(self): pass def clean_text_for_tts(self, text: str) -> str: text = re.sub(r'http\S+', '', text) text = re.sub(r'[^\w\s.,?!;:\-\'"()]', ' ', text) text = re.sub(r'\s+', ' ', text).strip() return text def translate_to_hindi(self, text: str) -> str: try: hindi_text = GoogleTranslator(source='auto', target='hi').translate(text) return hindi_text except Exception as e: print(f"Translation error: {e}") return "अनुवाद में त्रुटि हुई। कृपया पुनः प्रयास करें।" def generate_speech(self, text: str) -> Dict[str, Any]: try: cleaned_text = self.clean_text_for_tts(text) hindi_text = self.translate_to_hindi(cleaned_text) tts = gTTS(text=hindi_text, lang='hi', slow=False) audio_buffer = io.BytesIO() tts.write_to_fp(audio_buffer) audio_buffer.seek(0) return { "success": True, "message": "Speech generated successfully", "audio_buffer": audio_buffer, "hindi_text": hindi_text } except Exception as e: print(f"Error generating speech: {str(e)}") return { "success": False, "message": f"Error generating speech: {str(e)}", "audio_buffer": None, "hindi_text": None }