Peeble commited on
Commit
0d8268f
·
verified ·
1 Parent(s): a251913

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ import gradio as gr
4
+ from pathlib import Path
5
+ from transformers import AutoFeatureExtractor
6
+ from optimum.onnxruntime import ORTModelForFeatureExtraction
7
+
8
+ # Backend Safety Check
9
+ try:
10
+ import torchcodec
11
+ HAS_CODEC = True
12
+ except Exception:
13
+ os.environ["TORCHAUDIO_USE_TORCHCODEC"] = "0"
14
+ HAS_CODEC = False
15
+
16
+ import torchaudio
17
+
18
+ SAVE_DIR = "fastrvc_export"
19
+ HF_MODEL = "facebook/hubert-base-ls960"
20
+
21
+ def run_compilation(audio_path):
22
+ if not audio_path:
23
+ return None, "Please upload a voice sample."
24
+
25
+ try:
26
+ # Step 1: Load Audio
27
+ waveform, sr = torchaudio.load(audio_path)
28
+ if sr != 16000:
29
+ waveform = torchaudio.transforms.Resample(sr, 16000)(waveform)
30
+
31
+ # Step 2: Compile to ONNX
32
+ # Using Optimum to handle the complex HF -> ONNX mapping
33
+ model = ORTModelForFeatureExtraction.from_pretrained(
34
+ HF_MODEL,
35
+ export=True,
36
+ torch_dtype=torch.float32
37
+ )
38
+
39
+ Path(SAVE_DIR).mkdir(parents=True, exist_ok=True)
40
+ model.save_pretrained(SAVE_DIR)
41
+
42
+ onnx_file = os.path.join(SAVE_DIR, "model.onnx")
43
+ return onnx_file, "Success! Model compiled for FastRVC 3.0."
44
+
45
+ except Exception as e:
46
+ return None, f"Error: {str(e)}"
47
+
48
+ # Gradio 6.10.0 UI
49
+ with gr.Blocks(title="FastRVC 3.0 Compiler") as demo:
50
+ gr.Markdown("# 🎙️ FastRVC 3.0 Voice-to-ONNX")
51
+
52
+ with gr.Row():
53
+ with gr.Column():
54
+ audio_in = gr.Audio(label="Input Voice", type="filepath")
55
+ btn = gr.Button("Compile Now", variant="primary")
56
+ with gr.Column():
57
+ status = gr.Textbox(label="System Log")
58
+ file_out = gr.File(label="Download ONNX")
59
+
60
+ btn.click(run_compilation, inputs=audio_in, outputs=[file_out, status])
61
+
62
+ if __name__ == "__main__":
63
+ demo.launch()