Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import torchaudio
|
| 5 |
+
from transformers import AutoFeatureExtractor, HubertModel
|
| 6 |
+
from optimum.onnxruntime import ORTModelForFeatureExtraction
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# Configuration
|
| 10 |
+
SAVE_DIR = "exported_model"
|
| 11 |
+
HF_HUBERT = "facebook/hubert-base-ls960" # RVC Standard
|
| 12 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
+
|
| 14 |
+
def convert_to_onnx(audio_input):
|
| 15 |
+
if audio_input is None:
|
| 16 |
+
return None, "Error: No audio provided."
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
# 1. Load Audio & Preprocess (Target 16kHz for HuBERT)
|
| 20 |
+
waveform, sr = torchaudio.load(audio_input)
|
| 21 |
+
if sr != 16000:
|
| 22 |
+
resampler = torchaudio.transforms.Resample(sr, 16000)
|
| 23 |
+
waveform = resampler(waveform)
|
| 24 |
+
|
| 25 |
+
# 2. Export the HF Backbone to ONNX using Optimum
|
| 26 |
+
# This compiles the 'voice feature extractor' part of the RVC pipeline
|
| 27 |
+
print("Compiling HF Backbone to ONNX...")
|
| 28 |
+
onnx_model = ORTModelForFeatureExtraction.from_pretrained(
|
| 29 |
+
HF_HUBERT,
|
| 30 |
+
export=True,
|
| 31 |
+
torch_dtype=torch.float32
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Save the compiled model locally
|
| 35 |
+
onnx_model.save_pretrained(SAVE_DIR)
|
| 36 |
+
|
| 37 |
+
# In a real FastRVC3.0 app, you would also export the Generator .pth here
|
| 38 |
+
# For this demo, we provide the compiled HuBERT backbone as the result
|
| 39 |
+
model_path = os.path.join(SAVE_DIR, "model.onnx")
|
| 40 |
+
|
| 41 |
+
return model_path, "Successfully compiled Voice-to-ONNX backbone!"
|
| 42 |
+
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return None, f"Status: Error - {str(e)}"
|
| 45 |
+
|
| 46 |
+
# --- Gradio UI ---
|
| 47 |
+
with gr.Blocks(title="FastRVC 3.0 HF Compiler") as demo:
|
| 48 |
+
gr.Markdown("# 🤗 FastRVC 3.0 + HF Optimum")
|
| 49 |
+
gr.Markdown("Transform Hugging Face Voice Transformers into high-speed **ONNX** binaries.")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
audio_in = gr.Audio(label="Source Voice Sample", type="filepath")
|
| 54 |
+
btn = gr.Button("Compile & Export", variant="primary")
|
| 55 |
+
|
| 56 |
+
with gr.Column():
|
| 57 |
+
status = gr.Textbox(label="Status")
|
| 58 |
+
file_out = gr.File(label="Download .onnx Model")
|
| 59 |
+
|
| 60 |
+
btn.click(
|
| 61 |
+
fn=convert_to_onnx,
|
| 62 |
+
inputs=[audio_in],
|
| 63 |
+
outputs=[file_out, status]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|