Update app.py
Browse files
app.py
CHANGED
|
@@ -2,45 +2,104 @@ import gradio as gr
|
|
| 2 |
from TTS.api import TTS
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
with gr.Row():
|
| 26 |
with gr.Column():
|
| 27 |
-
text_input = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
language_input = gr.Dropdown(
|
| 29 |
-
choices=["en", "hi", "es", "fr", "de", "it"],
|
| 30 |
value="hi",
|
| 31 |
label="Language"
|
| 32 |
)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
generate_btn = gr.Button("Generate Audio")
|
| 36 |
|
| 37 |
with gr.Column():
|
| 38 |
-
audio_output = gr.Audio(label="
|
|
|
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
generate_btn.click(
|
| 41 |
fn=generate_voice,
|
| 42 |
-
inputs=[text_input,
|
| 43 |
-
outputs=audio_output
|
| 44 |
)
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from TTS.api import TTS
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# 1. Environment Variable set karna (License Agreement ke liye)
|
| 6 |
+
os.environ["COQUI_TOS_AGREED"] = "1"
|
|
|
|
| 7 |
|
| 8 |
+
# 2. Model Load karna (CPU Mode)
|
| 9 |
+
print("β³ Model load ho raha hai, kripya wait karein...")
|
| 10 |
+
try:
|
| 11 |
+
# Model download aur load
|
| 12 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
|
| 13 |
+
print("β
Model load ho gaya!")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"β Model Error: {e}")
|
| 16 |
+
|
| 17 |
+
# 3. Voices folder check karna
|
| 18 |
+
def get_voices():
|
| 19 |
+
voice_folder = "voices"
|
| 20 |
+
# Agar folder nahi hai to bana do
|
| 21 |
+
if not os.path.exists(voice_folder):
|
| 22 |
+
os.makedirs(voice_folder)
|
| 23 |
+
return []
|
| 24 |
|
| 25 |
+
# Sirf .wav files dhoondo
|
| 26 |
+
voices = [f for f in os.listdir(voice_folder) if f.endswith(".wav")]
|
| 27 |
+
return voices
|
| 28 |
+
|
| 29 |
+
def generate_voice(text, voice_name, language):
|
| 30 |
+
try:
|
| 31 |
+
if not voice_name:
|
| 32 |
+
return None, "β οΈ Kripya pehle koi Voice select karein!"
|
| 33 |
|
| 34 |
+
speaker_audio_path = os.path.join("voices", voice_name)
|
| 35 |
+
|
| 36 |
+
# Check agar file exist karti hai
|
| 37 |
+
if not os.path.exists(speaker_audio_path):
|
| 38 |
+
return None, f"β οΈ Error: '{voice_name}' file nahi mili!"
|
| 39 |
+
|
| 40 |
+
output_path = "output.wav"
|
| 41 |
+
|
| 42 |
+
# Audio generate karna
|
| 43 |
+
tts.tts_to_file(
|
| 44 |
+
text=text,
|
| 45 |
+
file_path=output_path,
|
| 46 |
+
speaker_wav=speaker_audio_path,
|
| 47 |
+
language=language
|
| 48 |
+
)
|
| 49 |
+
return output_path, "β
Audio Generated Successfully!"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return None, f"β Error: {str(e)}"
|
| 52 |
+
|
| 53 |
+
# 4. UI Design
|
| 54 |
+
with gr.Blocks(title="Deepak's AI Voice Tool") as demo:
|
| 55 |
+
gr.Markdown("## ποΈ AI Voice Cloning (11Labs Style)")
|
| 56 |
|
| 57 |
with gr.Row():
|
| 58 |
with gr.Column():
|
| 59 |
+
text_input = gr.Textbox(
|
| 60 |
+
label="Text likhein (Hindi/English)",
|
| 61 |
+
placeholder="Namaste, main Pappu bol raha hoon. Aaj hum AI seekhenge.",
|
| 62 |
+
lines=3
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Character Selection Dropdown
|
| 66 |
+
# Note: Agar list khali hai to error na de isliye check lagaya hai
|
| 67 |
+
initial_voices = get_voices()
|
| 68 |
+
voice_dropdown = gr.Dropdown(
|
| 69 |
+
choices=initial_voices,
|
| 70 |
+
label="Character Select Karein (voices folder se)",
|
| 71 |
+
value=initial_voices[0] if initial_voices else None,
|
| 72 |
+
interactive=True
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
refresh_btn = gr.Button("π Refresh Voice List")
|
| 76 |
+
|
| 77 |
language_input = gr.Dropdown(
|
| 78 |
+
choices=["en", "hi", "es", "fr", "de", "it", "ja"],
|
| 79 |
value="hi",
|
| 80 |
label="Language"
|
| 81 |
)
|
| 82 |
+
|
| 83 |
+
generate_btn = gr.Button("π Generate Audio", variant="primary")
|
|
|
|
| 84 |
|
| 85 |
with gr.Column():
|
| 86 |
+
audio_output = gr.Audio(label="Output Audio")
|
| 87 |
+
status_msg = gr.Textbox(label="Status", interactive=False)
|
| 88 |
|
| 89 |
+
# Button Functions
|
| 90 |
+
def refresh_dropdown():
|
| 91 |
+
new_choices = get_voices()
|
| 92 |
+
return gr.Dropdown(choices=new_choices, value=new_choices[0] if new_choices else None)
|
| 93 |
+
|
| 94 |
+
refresh_btn.click(fn=refresh_dropdown, inputs=[], outputs=voice_dropdown)
|
| 95 |
+
|
| 96 |
generate_btn.click(
|
| 97 |
fn=generate_voice,
|
| 98 |
+
inputs=[text_input, voice_dropdown, language_input],
|
| 99 |
+
outputs=[audio_output, status_msg]
|
| 100 |
)
|
| 101 |
|
| 102 |
+
# 5. App Launch (Docker Compatibility ke liye ye zaroori hai)
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 105 |
+
|