Rogerjs commited on
Commit
3dba9d4
·
verified ·
1 Parent(s): a73a846

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py CHANGED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import os
4
+ import time
5
+ from scipy.io import wavfile
6
+
7
+ # Explicitly import Bark components
8
+ from bark import generate_audio, SAMPLE_RATE
9
+ from bark.generation import preload_models
10
+
11
+ class VoiceCloningApp:
12
+ def __init__(self):
13
+ # Create working directory
14
+ self.base_dir = os.path.dirname(os.path.abspath(__file__))
15
+ self.working_dir = os.path.join(self.base_dir, "working_files")
16
+ os.makedirs(self.working_dir, exist_ok=True)
17
+
18
+ # Explicit model loading with error handling
19
+ try:
20
+ print("Attempting to load Bark models...")
21
+ preload_models()
22
+ print("Bark models loaded successfully.")
23
+ except Exception as e:
24
+ print(f"Error loading Bark models: {e}")
25
+ import traceback
26
+ traceback.print_exc()
27
+ raise RuntimeError(f"Could not load Bark models. Error: {e}")
28
+
29
+ def process_reference_audio(self, audio_data):
30
+ """Simple audio processing"""
31
+ if audio_data is None:
32
+ return "Please provide an audio input"
33
+
34
+ try:
35
+ # Unpack audio data
36
+ sample_rate, audio_array = audio_data
37
+
38
+ # Normalize audio
39
+ audio_array = audio_array / np.max(np.abs(audio_array))
40
+
41
+ # Save reference audio
42
+ filename = f"reference_{int(time.time())}.wav"
43
+ filepath = os.path.join(self.working_dir, filename)
44
+ wavfile.write(filepath, sample_rate, audio_array)
45
+
46
+ return "✅ Audio captured successfully!"
47
+
48
+ except Exception as e:
49
+ return f"Error processing audio: {str(e)}"
50
+
51
+ def generate_speech(self, text):
52
+ """Generate speech using Bark"""
53
+ if not text or not text.strip():
54
+ return None, "Please enter some text to speak"
55
+
56
+ try:
57
+ # Generate audio with explicit error handling
58
+ print(f"Generating speech for text: {text}")
59
+
60
+ # Simplified audio generation
61
+ audio_array = generate_audio(
62
+ text,
63
+ history_prompt=None,
64
+ temp=0.7
65
+ )
66
+
67
+ # Save generated audio
68
+ filename = f"generated_speech_{int(time.time())}.wav"
69
+ filepath = os.path.join(self.working_dir, filename)
70
+ wavfile.write(filepath, SAMPLE_RATE, audio_array)
71
+
72
+ return filepath, None
73
+
74
+ except Exception as e:
75
+ print(f"Speech generation error: {e}")
76
+ import traceback
77
+ traceback.print_exc()
78
+ return None, f"Error generating speech: {str(e)}"
79
+
80
+ def create_interface():
81
+ # Ensure working directory exists
82
+ working_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "working_files")
83
+ os.makedirs(working_dir, exist_ok=True)
84
+
85
+ app = VoiceCloningApp()
86
+
87
+ with gr.Blocks() as interface:
88
+ gr.Markdown("# 🎙️ Voice Cloning App")
89
+
90
+ with gr.Row():
91
+ with gr.Column():
92
+ gr.Markdown("## 1. Capture Reference Voice")
93
+ reference_audio = gr.Audio(sources=["microphone", "upload"], type="numpy")
94
+ process_btn = gr.Button("Process Reference Voice")
95
+ process_output = gr.Textbox(label="Processing Result")
96
+
97
+ with gr.Column():
98
+ gr.Markdown("## 2. Generate Speech")
99
+ text_input = gr.Textbox(label="Enter Text to Speak")
100
+ generate_btn = gr.Button("Generate Speech")
101
+ audio_output = gr.Audio(label="Generated Speech")
102
+ error_output = gr.Textbox(label="Errors", visible=True)
103
+
104
+ # Bind functions
105
+ process_btn.click(
106
+ fn=app.process_reference_audio,
107
+ inputs=reference_audio,
108
+ outputs=process_output
109
+ )
110
+
111
+ generate_btn.click(
112
+ fn=app.generate_speech,
113
+ inputs=text_input,
114
+ outputs=[audio_output, error_output]
115
+ )
116
+
117
+ return interface
118
+
119
+ if __name__ == "__main__":
120
+ interface = create_interface()
121
+ interface.launch(
122
+ share=False,
123
+ debug=True,
124
+ show_error=True,
125
+ server_name='0.0.0.0',
126
+ server_port=7860
127
+ )