|
|
import gradio as gr |
|
|
from TTS.api import TTS |
|
|
import os |
|
|
|
|
|
|
|
|
os.environ["COQUI_TOS_AGREED"] = "1" |
|
|
|
|
|
|
|
|
print("β³ Model load ho raha hai, kripya wait karein...") |
|
|
try: |
|
|
|
|
|
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False) |
|
|
print("β
Model load ho gaya!") |
|
|
except Exception as e: |
|
|
print(f"β Model Error: {e}") |
|
|
|
|
|
|
|
|
def get_voices(): |
|
|
voice_folder = "voices" |
|
|
|
|
|
if not os.path.exists(voice_folder): |
|
|
os.makedirs(voice_folder) |
|
|
return [] |
|
|
|
|
|
|
|
|
voices = [f for f in os.listdir(voice_folder) if f.endswith(".wav")] |
|
|
return voices |
|
|
|
|
|
def generate_voice(text, voice_name, language): |
|
|
try: |
|
|
if not voice_name: |
|
|
return None, "β οΈ Kripya pehle koi Voice select karein!" |
|
|
|
|
|
speaker_audio_path = os.path.join("voices", voice_name) |
|
|
|
|
|
|
|
|
if not os.path.exists(speaker_audio_path): |
|
|
return None, f"β οΈ Error: '{voice_name}' file nahi mili!" |
|
|
|
|
|
output_path = "output.wav" |
|
|
|
|
|
|
|
|
tts.tts_to_file( |
|
|
text=text, |
|
|
file_path=output_path, |
|
|
speaker_wav=speaker_audio_path, |
|
|
language=language |
|
|
) |
|
|
return output_path, "β
Audio Generated Successfully!" |
|
|
except Exception as e: |
|
|
return None, f"β Error: {str(e)}" |
|
|
|
|
|
|
|
|
with gr.Blocks(title="Deepak's AI Voice Tool") as demo: |
|
|
gr.Markdown("## ποΈ AI Voice Cloning (11Labs Style)") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
text_input = gr.Textbox( |
|
|
label="Text likhein (Hindi/English)", |
|
|
placeholder="Namaste, main Pappu bol raha hoon. Aaj hum AI seekhenge.", |
|
|
lines=3 |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
initial_voices = get_voices() |
|
|
voice_dropdown = gr.Dropdown( |
|
|
choices=initial_voices, |
|
|
label="Character Select Karein (voices folder se)", |
|
|
value=initial_voices[0] if initial_voices else None, |
|
|
interactive=True |
|
|
) |
|
|
|
|
|
refresh_btn = gr.Button("π Refresh Voice List") |
|
|
|
|
|
language_input = gr.Dropdown( |
|
|
choices=["en", "hi", "es", "fr", "de", "it", "ja"], |
|
|
value="hi", |
|
|
label="Language" |
|
|
) |
|
|
|
|
|
generate_btn = gr.Button("π Generate Audio", variant="primary") |
|
|
|
|
|
with gr.Column(): |
|
|
audio_output = gr.Audio(label="Output Audio") |
|
|
status_msg = gr.Textbox(label="Status", interactive=False) |
|
|
|
|
|
|
|
|
def refresh_dropdown(): |
|
|
new_choices = get_voices() |
|
|
return gr.Dropdown(choices=new_choices, value=new_choices[0] if new_choices else None) |
|
|
|
|
|
refresh_btn.click(fn=refresh_dropdown, inputs=[], outputs=voice_dropdown) |
|
|
|
|
|
generate_btn.click( |
|
|
fn=generate_voice, |
|
|
inputs=[text_input, voice_dropdown, language_input], |
|
|
outputs=[audio_output, status_msg] |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(server_name="0.0.0.0", server_port=7860) |
|
|
|