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import os
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
from deep_translator import GoogleTranslator
|
| 6 |
+
import logging
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
import numpy as np
|
| 9 |
+
from transformers import pipeline
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s')
|
| 12 |
+
|
| 13 |
+
# Initialize HuggingFace embeddings (free to use)
|
| 14 |
+
sentence_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 15 |
+
|
| 16 |
+
# Initialize the chat model
|
| 17 |
+
chat_model = pipeline("text-generation", model="gpt2")
|
| 18 |
+
|
| 19 |
+
indexed_texts = []
|
| 20 |
+
indexed_embeddings = []
|
| 21 |
+
|
| 22 |
+
# Translation languages dropdown options
|
| 23 |
+
translation_languages = {
|
| 24 |
+
"English": "en",
|
| 25 |
+
"Arabic": "ar",
|
| 26 |
+
"Hindi": "hi",
|
| 27 |
+
"Kannada": "kn",
|
| 28 |
+
"Marathi": "mr",
|
| 29 |
+
"Telugu": "te",
|
| 30 |
+
"Tamil": "ta",
|
| 31 |
+
"Gujarati": "gu",
|
| 32 |
+
"Malayalam": "ml"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# Define supported languages for Google TTS
|
| 36 |
+
audio_language_dict = {
|
| 37 |
+
"English": {"code": "en"},
|
| 38 |
+
"Arabic": {"code": "ar"},
|
| 39 |
+
"Hindi": {"code": "hi"},
|
| 40 |
+
"Kannada": {"code": "kn"},
|
| 41 |
+
"Marathi": {"code": "mr"},
|
| 42 |
+
"Telugu": {"code": "te"},
|
| 43 |
+
"Tamil": {"code": "ta"},
|
| 44 |
+
"Gujarati": {"code": "gu"},
|
| 45 |
+
"Malayalam": {"code": "ml"}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
def index_text(text: str) -> str:
|
| 49 |
+
global indexed_texts, indexed_embeddings
|
| 50 |
+
try:
|
| 51 |
+
embedding = sentence_model.encode([text])[0]
|
| 52 |
+
indexed_texts.append(text)
|
| 53 |
+
indexed_embeddings.append(embedding)
|
| 54 |
+
return "Text indexed successfully."
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error indexing text: {str(e)}"
|
| 57 |
+
|
| 58 |
+
def find_most_similar(query: str, top_k: int = 1) -> list:
|
| 59 |
+
query_embedding = sentence_model.encode([query])[0]
|
| 60 |
+
similarities = [np.dot(query_embedding, doc_embedding) for doc_embedding in indexed_embeddings]
|
| 61 |
+
top_indices = np.argsort(similarities)[-top_k:][::-1]
|
| 62 |
+
return [indexed_texts[i] for i in top_indices]
|
| 63 |
+
|
| 64 |
+
def chat_with_context(question: str) -> str:
|
| 65 |
+
if not indexed_texts:
|
| 66 |
+
return "Please index some text first."
|
| 67 |
+
|
| 68 |
+
context = find_most_similar(question)[0]
|
| 69 |
+
prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
response = chat_model(prompt, max_length=100, num_return_sequences=1)
|
| 73 |
+
return response[0]['generated_text'].split("Answer:")[1].strip()
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return f"Error in chat: {str(e)}"
|
| 76 |
+
|
| 77 |
+
# Translation function
|
| 78 |
+
def translate_text(text, target_lang_code):
|
| 79 |
+
try:
|
| 80 |
+
translator = GoogleTranslator(source='auto', target=target_lang_code)
|
| 81 |
+
return translator.translate(text)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return f"Translation Error: {str(e)}"
|
| 84 |
+
|
| 85 |
+
# Google TTS function
|
| 86 |
+
def google_tts(text, lang):
|
| 87 |
+
try:
|
| 88 |
+
tts = gTTS(text=text, lang=lang, slow=False)
|
| 89 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
| 90 |
+
tts.save(temp_audio.name)
|
| 91 |
+
return temp_audio.name, f"Speech generated with Google TTS using {lang} language"
|
| 92 |
+
except Exception as e:
|
| 93 |
+
return None, f"Error in Google TTS: {str(e)}"
|
| 94 |
+
|
| 95 |
+
# Gradio interface
|
| 96 |
+
with gr.Blocks() as iface:
|
| 97 |
+
gr.Markdown("# Free Text-to-Speech Tool with Language Translation and Chat")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
text_input = gr.Textbox(label="Enter text for translation and speech generation", lines=3)
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
translation_lang_dropdown = gr.Dropdown(list(translation_languages.keys()), label="Select Translation Language", value="English")
|
| 104 |
+
convert_button = gr.Button("Convert")
|
| 105 |
+
|
| 106 |
+
translated_text = gr.Textbox(label="Translated Text")
|
| 107 |
+
|
| 108 |
+
with gr.Row():
|
| 109 |
+
index_button = gr.Button("Index")
|
| 110 |
+
|
| 111 |
+
index_status = gr.Textbox(label="Indexing Status")
|
| 112 |
+
|
| 113 |
+
use_chat = gr.Checkbox(label="Use Chat for TTS input", value=False)
|
| 114 |
+
|
| 115 |
+
with gr.Group() as chat_group:
|
| 116 |
+
chat_input = gr.Textbox(label="Ask a question about the indexed text")
|
| 117 |
+
chat_button = gr.Button("Ask")
|
| 118 |
+
chat_output = gr.Textbox(label="Answer", interactive=False)
|
| 119 |
+
|
| 120 |
+
with gr.Group() as tts_options:
|
| 121 |
+
audio_lang_dropdown = gr.Dropdown(list(audio_language_dict.keys()), label="Select Audio Language", value="English")
|
| 122 |
+
|
| 123 |
+
generate_button = gr.Button("Generate Speech")
|
| 124 |
+
audio_output = gr.Audio(label="Generated Speech")
|
| 125 |
+
message_output = gr.Textbox(label="Message")
|
| 126 |
+
|
| 127 |
+
def update_chat_visibility(use_chat):
|
| 128 |
+
return gr.Group.update(visible=use_chat)
|
| 129 |
+
|
| 130 |
+
def convert_text(text, translation_lang):
|
| 131 |
+
target_code = translation_languages[translation_lang]
|
| 132 |
+
translated = translate_text(text, target_code)
|
| 133 |
+
return translated
|
| 134 |
+
|
| 135 |
+
def generate_speech(text, audio_lang, use_chat, chat_output):
|
| 136 |
+
if use_chat and chat_output:
|
| 137 |
+
text = chat_output
|
| 138 |
+
logging.info(f"Generating speech: lang={audio_lang}")
|
| 139 |
+
try:
|
| 140 |
+
return google_tts(text, audio_language_dict[audio_lang]["code"])
|
| 141 |
+
except Exception as e:
|
| 142 |
+
logging.error(f"Error generating speech: {str(e)}")
|
| 143 |
+
return None, f"Error generating speech: {str(e)}"
|
| 144 |
+
|
| 145 |
+
convert_button.click(convert_text, inputs=[text_input, translation_lang_dropdown], outputs=translated_text)
|
| 146 |
+
index_button.click(index_text, inputs=[translated_text], outputs=[index_status])
|
| 147 |
+
use_chat.change(update_chat_visibility, inputs=[use_chat], outputs=[chat_group])
|
| 148 |
+
chat_button.click(chat_with_context, inputs=[chat_input], outputs=[chat_output])
|
| 149 |
+
|
| 150 |
+
generate_button.click(
|
| 151 |
+
generate_speech,
|
| 152 |
+
inputs=[translated_text, audio_lang_dropdown, use_chat, chat_output],
|
| 153 |
+
outputs=[audio_output, message_output]
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
iface.launch()
|