Offex commited on
Commit
e93ca2c
Β·
verified Β·
1 Parent(s): dd74ab5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -25
app.py CHANGED
@@ -2,45 +2,104 @@ import gradio as gr
2
  from TTS.api import TTS
3
  import os
4
 
5
- # Model download aur load karna (Pehli baar thoda time lega)
6
- # Agree strictly to Coqui terms if prompted in logs
7
- tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
8
 
9
- def generate_voice(text, speaker_audio_path, language):
10
- output_path = "output.wav"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Text-to-Speech generate karna
13
- tts.tts_to_file(
14
- text=text,
15
- file_path=output_path,
16
- speaker_wav=speaker_audio_path, # Voice cloning ke liye reference audio
17
- language=language
18
- )
19
- return output_path
20
 
21
- # Gradio UI Design
22
- with gr.Blocks() as demo:
23
- gr.Markdown("# πŸš€ My AI Voice Tool (XTTS v2)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  with gr.Row():
26
  with gr.Column():
27
- text_input = gr.Textbox(label="Text likhein", placeholder="Hello, kaise ho aap?")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  language_input = gr.Dropdown(
29
- choices=["en", "hi", "es", "fr", "de", "it"],
30
  value="hi",
31
  label="Language"
32
  )
33
- # Voice cloning ke liye audio upload
34
- audio_input = gr.Audio(label="Reference Voice (Cloning ke liye)", type="filepath")
35
- generate_btn = gr.Button("Generate Audio")
36
 
37
  with gr.Column():
38
- audio_output = gr.Audio(label="Generated Audio")
 
39
 
 
 
 
 
 
 
 
40
  generate_btn.click(
41
  fn=generate_voice,
42
- inputs=[text_input, audio_input, language_input],
43
- outputs=audio_output
44
  )
45
 
46
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
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
+