Create rvc_service.py
Browse files- rvc_service.py +90 -0
rvc_service.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from rvc_service import RVCService
|
3 |
+
import asyncio
|
4 |
+
import logging
|
5 |
+
import numpy as np
|
6 |
+
from scipy.io import wavfile
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Initialize logging
|
10 |
+
logging.basicConfig(level=logging.DEBUG)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
+
|
13 |
+
# Initialize RVC Service
|
14 |
+
rvc_service = RVCService()
|
15 |
+
|
16 |
+
async def convert_tts(model_name, audio_file, slang_rate):
|
17 |
+
try:
|
18 |
+
logger.debug(f"Received request - model: {model_name}, audio: {type(audio_file)}, slang: {slang_rate}")
|
19 |
+
|
20 |
+
if audio_file is None:
|
21 |
+
logger.error("No audio file provided")
|
22 |
+
return {"error": "No audio file uploaded."}, None
|
23 |
+
|
24 |
+
# Load and preprocess audio
|
25 |
+
if hasattr(audio_file, 'name'):
|
26 |
+
logger.debug(f"Audio file name: {audio_file.name}")
|
27 |
+
# Load audio file
|
28 |
+
sr, audio = wavfile.read(audio_file.name)
|
29 |
+
# Convert to mono if stereo
|
30 |
+
if len(audio.shape) > 1:
|
31 |
+
audio = np.mean(audio, axis=1)
|
32 |
+
|
33 |
+
# Submit job to RVC service
|
34 |
+
job_id = await rvc_service.submit_job(
|
35 |
+
audio_data=audio,
|
36 |
+
model_name=model_name,
|
37 |
+
priority=1 # Default priority
|
38 |
+
)
|
39 |
+
|
40 |
+
if not job_id:
|
41 |
+
return {"error": "Service queue is full"}, None
|
42 |
+
|
43 |
+
# Wait for result (you might want to implement a better waiting mechanism)
|
44 |
+
for _ in range(30): # Maximum 30 seconds wait
|
45 |
+
if job_id in rvc_service.job_queue.processing:
|
46 |
+
await asyncio.sleep(1)
|
47 |
+
continue
|
48 |
+
|
49 |
+
# Check if job completed successfully
|
50 |
+
output_path = f"outputs/output_{job_id}.wav"
|
51 |
+
if os.path.exists(output_path):
|
52 |
+
return {"info": f"Processed with job ID: {job_id}"}, output_path
|
53 |
+
|
54 |
+
return {"error": "Processing timeout"}, None
|
55 |
+
|
56 |
+
return {"error": "Invalid audio file"}, None
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
logger.error(f"Error in convert_tts: {str(e)}", exc_info=True)
|
60 |
+
return {"error": str(e)}, None
|
61 |
+
|
62 |
+
# Modified interface with queue settings for better concurrency
|
63 |
+
iface = gr.Interface(
|
64 |
+
fn=convert_tts,
|
65 |
+
inputs=[
|
66 |
+
gr.Dropdown(choices=get_model_names(), label="Model", interactive=True),
|
67 |
+
gr.Audio(label="Upload Audio", type="filepath"),
|
68 |
+
gr.Slider(minimum=0, maximum=1, step=0.01, label="Slang Rate"),
|
69 |
+
],
|
70 |
+
outputs=[
|
71 |
+
gr.JSON(label="Info"),
|
72 |
+
gr.Audio(label="Converted Audio")
|
73 |
+
],
|
74 |
+
title="Voice Conversion",
|
75 |
+
concurrency_limit=5, # Limit concurrent requests
|
76 |
+
batch=False, # Process requests individually
|
77 |
+
max_batch_size=1,
|
78 |
+
).queue()
|
79 |
+
|
80 |
+
if __name__ == "__main__":
|
81 |
+
# Start RVC service
|
82 |
+
rvc_service.start()
|
83 |
+
|
84 |
+
# Launch Gradio interface
|
85 |
+
iface.launch(
|
86 |
+
debug=True,
|
87 |
+
show_error=True,
|
88 |
+
max_threads=10,
|
89 |
+
share=False
|
90 |
+
)
|