Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio_client import Client, handle_file
|
| 3 |
+
from zerorvc import RVC
|
| 4 |
+
import soundfile as sf
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
from joblib import memory
|
| 7 |
+
|
| 8 |
+
memory = memory.Memory(location="cache", verbose=0)
|
| 9 |
+
|
| 10 |
+
rvc = RVC.from_pretrained(MODEL_NAME)
|
| 11 |
+
|
| 12 |
+
@memory.cache(ignore=["client"])
|
| 13 |
+
def split(client, audio):
|
| 14 |
+
result = client.predict(
|
| 15 |
+
param_0=handle_file(audio),
|
| 16 |
+
param_1="BS-RoFormer",
|
| 17 |
+
api_name="/separate"
|
| 18 |
+
)
|
| 19 |
+
return result[0], result[1]
|
| 20 |
+
|
| 21 |
+
def process_audio(client, audio, pitch_modification):
|
| 22 |
+
vocal, bgm = split(client, audio)
|
| 23 |
+
samples = rvc.convert(vocal, pitch_modification=pitch_modification)
|
| 24 |
+
|
| 25 |
+
sf.write("vocal.wav", samples, rvc.sr)
|
| 26 |
+
|
| 27 |
+
vocal = AudioSegment.from_wav("vocal.wav")
|
| 28 |
+
background = AudioSegment.from_mp3(bgm)
|
| 29 |
+
|
| 30 |
+
combined = background.overlay(vocal)
|
| 31 |
+
combined.export("combined.mp3", format="mp3")
|
| 32 |
+
return "combined.mp3", "vocal.wav", bgm
|
| 33 |
+
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=process_audio,
|
| 36 |
+
inputs=[gr.Audio(type="filepath"), gr.Slider(minimum=-36, maximum=36, value=0, step=1)],
|
| 37 |
+
outputs=[gr.Audio(label="Combined"), gr.Audio(label="Vocal"), gr.Audio(label="Background")],
|
| 38 |
+
title="Voice Conversion",
|
| 39 |
+
description="Upload an audio file and process it.",
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def set_client_for_session(request: gr.Request):
|
| 44 |
+
x_ip_token = request.headers['x-ip-token']
|
| 45 |
+
return Client("JacobLinCool/vocal-separation", headers={"X-IP-Token": x_ip_token})
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
client = gr.State()
|
| 49 |
+
btn = gr.Button("Run", variant="primary")
|
| 50 |
+
|
| 51 |
+
btn.submit(
|
| 52 |
+
process_audio,
|
| 53 |
+
[client, gr.Audio(type="filepath"), gr.Slider(minimum=-36, maximum=36, value=0, step=1)],
|
| 54 |
+
[gr.Audio(label="Combined"), gr.Audio(label="Vocal"), gr.Audio(label="Background")]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
demo.load(set_client_for_session, None, client)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|