Update app.py
Browse files
app.py
CHANGED
@@ -5,103 +5,71 @@ from scipy.io import wavfile
|
|
5 |
from voice_processing import parallel_tts, get_model_names
|
6 |
import os
|
7 |
import logging
|
8 |
-
from rvc_service import RVCService # Our new service
|
9 |
-
import asyncio
|
10 |
-
from voice_processing import parallel_tts, get_model_names
|
11 |
-
import sys
|
12 |
-
from datetime import datetime # Add this import
|
13 |
-
import traceback
|
14 |
-
import json
|
15 |
-
|
16 |
-
|
17 |
-
# Set up enhanced logging
|
18 |
-
logging.basicConfig(
|
19 |
-
level=logging.DEBUG,
|
20 |
-
format='%(asctime)s | %(levelname)s | %(name)s | %(message)s',
|
21 |
-
handlers=[
|
22 |
-
logging.FileHandler('rvc_server.log'),
|
23 |
-
logging.StreamHandler(sys.stdout)
|
24 |
-
]
|
25 |
-
)
|
26 |
-
logger = logging.getLogger('rvc_server')
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
30 |
|
31 |
-
def
|
32 |
-
"""Creates a unique logger for request handling"""
|
33 |
-
request_id = datetime.now().strftime('%Y%m%d_%H%M%S_%f')
|
34 |
-
logger = logging.getLogger(f'request_{request_id}')
|
35 |
-
return logger, request_id
|
36 |
-
|
37 |
-
def convert_tts(model_name, audio_file, slang_rate):
|
38 |
-
"""Voice conversion endpoint"""
|
39 |
-
req_logger, request_id = setup_request_logging()
|
40 |
-
|
41 |
try:
|
42 |
-
|
43 |
-
req_logger.info(f"Parameters: model={model_name}, slang_rate={slang_rate}")
|
44 |
|
45 |
if audio_file is None:
|
46 |
-
|
47 |
return {"error": "No audio file uploaded."}, None
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
return {"error": "Processing failed"}, None
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
# Check if result_tuple is in the expected format
|
68 |
-
if isinstance(result_tuple, tuple) and len(result_tuple) == 3:
|
69 |
-
info, _, (tgt_sr, audio_output) = result_tuple
|
70 |
-
|
71 |
-
if audio_output is None:
|
72 |
-
req_logger.error("No audio output generated")
|
73 |
-
return {"error": "No audio output generated"}, None
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
else:
|
84 |
-
req_logger.info("Saving raw audio output")
|
85 |
-
with open(output_path, "wb") as f:
|
86 |
-
f.write(audio_output)
|
87 |
-
|
88 |
-
req_logger.info(f"Successfully saved to {output_path}")
|
89 |
-
return {"info": info}, output_path
|
90 |
else:
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
93 |
|
94 |
-
except Exception as
|
95 |
-
|
96 |
-
return {"error": f"
|
97 |
|
98 |
except Exception as e:
|
99 |
-
|
100 |
return {"error": str(e)}, None
|
101 |
-
|
102 |
-
#
|
103 |
iface = gr.Interface(
|
104 |
-
fn=convert_tts,
|
105 |
inputs=[
|
106 |
gr.Dropdown(choices=get_model_names(), label="Model", interactive=True),
|
107 |
gr.Audio(label="Upload Audio", type="filepath"),
|
@@ -115,12 +83,8 @@ iface = gr.Interface(
|
|
115 |
).queue()
|
116 |
|
117 |
if __name__ == "__main__":
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
max_threads=10
|
124 |
-
)
|
125 |
-
except Exception as e:
|
126 |
-
logger.error(f"Error launching server: {e}", exc_info=True)
|
|
|
5 |
from voice_processing import parallel_tts, get_model_names
|
6 |
import os
|
7 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Set up logging
|
10 |
+
logging.basicConfig(level=logging.DEBUG)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
|
13 |
+
async def convert_tts(model_name, audio_file, slang_rate):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
+
logger.debug(f"Received request - model: {model_name}, audio: {type(audio_file)}, slang: {slang_rate}")
|
|
|
16 |
|
17 |
if audio_file is None:
|
18 |
+
logger.error("No audio file provided")
|
19 |
return {"error": "No audio file uploaded."}, None
|
20 |
|
21 |
+
# Log the audio file details
|
22 |
+
if hasattr(audio_file, 'name'):
|
23 |
+
logger.debug(f"Audio file name: {audio_file.name}")
|
24 |
+
logger.debug(f"Audio file type: {type(audio_file)}")
|
25 |
+
|
26 |
+
# Create task for parallel processing
|
27 |
+
task = (model_name, None, None, slang_rate, True, audio_file)
|
28 |
+
|
29 |
+
# Process the audio
|
30 |
+
logger.debug("Starting audio processing")
|
31 |
+
result = parallel_tts([task])
|
32 |
+
logger.debug(f"Processing result: {type(result)}")
|
33 |
+
|
34 |
+
if not result or result[0] is None:
|
35 |
+
logger.error("Processing failed - no result")
|
36 |
+
return {"error": "Processing failed"}, None
|
37 |
|
38 |
+
info, _, (tgt_sr, audio_output) = result[0]
|
39 |
+
logger.debug(f"Processing complete - info: {info}")
|
|
|
40 |
|
41 |
+
if audio_output is None:
|
42 |
+
logger.error("No audio output generated")
|
43 |
+
return {"error": "No audio output generated"}, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# Save the output
|
46 |
+
try:
|
47 |
+
output_filename = f"output_{os.urandom(4).hex()}.wav"
|
48 |
+
output_path = os.path.join("outputs", output_filename)
|
49 |
+
os.makedirs("outputs", exist_ok=True)
|
50 |
|
51 |
+
if isinstance(audio_output, np.ndarray):
|
52 |
+
logger.debug(f"Saving numpy array with shape {audio_output.shape}")
|
53 |
+
wavfile.write(output_path, tgt_sr, audio_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
else:
|
55 |
+
logger.debug(f"Saving raw audio data of type {type(audio_output)}")
|
56 |
+
with open(output_path, "wb") as f:
|
57 |
+
f.write(audio_output)
|
58 |
+
|
59 |
+
logger.debug(f"Successfully saved to {output_path}")
|
60 |
+
return {"info": info}, output_path
|
61 |
|
62 |
+
except Exception as save_error:
|
63 |
+
logger.error(f"Error saving output: {save_error}", exc_info=True)
|
64 |
+
return {"error": f"Error saving output: {str(save_error)}"}, None
|
65 |
|
66 |
except Exception as e:
|
67 |
+
logger.error(f"Error in convert_tts: {str(e)}", exc_info=True)
|
68 |
return {"error": str(e)}, None
|
69 |
+
|
70 |
+
# Interface definition
|
71 |
iface = gr.Interface(
|
72 |
+
fn=convert_tts,
|
73 |
inputs=[
|
74 |
gr.Dropdown(choices=get_model_names(), label="Model", interactive=True),
|
75 |
gr.Audio(label="Upload Audio", type="filepath"),
|
|
|
83 |
).queue()
|
84 |
|
85 |
if __name__ == "__main__":
|
86 |
+
iface.launch(
|
87 |
+
debug=True,
|
88 |
+
show_error=True,
|
89 |
+
max_threads=10
|
90 |
+
)
|
|
|
|
|
|
|
|