Kuchbhi / app.py
Offex's picture
Update app.py
e93ca2c verified
import gradio as gr
from TTS.api import TTS
import os
# 1. Environment Variable set karna (License Agreement ke liye)
os.environ["COQUI_TOS_AGREED"] = "1"
# 2. Model Load karna (CPU Mode)
print("⏳ Model load ho raha hai, kripya wait karein...")
try:
# Model download aur load
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}")
# 3. Voices folder check karna
def get_voices():
voice_folder = "voices"
# Agar folder nahi hai to bana do
if not os.path.exists(voice_folder):
os.makedirs(voice_folder)
return []
# Sirf .wav files dhoondo
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)
# Check agar file exist karti hai
if not os.path.exists(speaker_audio_path):
return None, f"⚠️ Error: '{voice_name}' file nahi mili!"
output_path = "output.wav"
# Audio generate karna
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)}"
# 4. UI Design
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
)
# Character Selection Dropdown
# Note: Agar list khali hai to error na de isliye check lagaya hai
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)
# Button Functions
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]
)
# 5. App Launch (Docker Compatibility ke liye ye zaroori hai)
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)