Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,40 +1,33 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn as nn
|
| 4 |
-
from transformers import HubertModel
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
class ContentVec768(nn.Module):
|
| 8 |
-
def __init__(self, model_id="lengyue233/content-vec-best"):
|
| 9 |
-
super().__init__()
|
| 10 |
-
self.m = HubertModel.from_pretrained(model_id)
|
| 11 |
-
|
| 12 |
-
@torch.no_grad()
|
| 13 |
-
def forward(self, wav_16k):
|
| 14 |
-
out = self.m(wav_16k, attention_mask=None, output_hidden_states=True)
|
| 15 |
-
return out.last_hidden_state # warstwa 12 -> [1, T, 768]
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def export():
|
| 19 |
-
enc = ContentVec768().eval()
|
| 20 |
-
dummy = torch.randn(1, 16000)
|
| 21 |
-
feats = enc(dummy)
|
| 22 |
-
if feats.shape[-1] != 768:
|
| 23 |
-
return None, f"BLAD: feats={tuple(feats.shape)} (oczekiwane 768)"
|
| 24 |
-
torch.onnx.export(
|
| 25 |
-
enc, (dummy,), "contentvec.onnx",
|
| 26 |
-
input_names=["wav_16k"], output_names=["feats"],
|
| 27 |
-
dynamic_axes={"wav_16k": {1: "L"}, "feats": {1: "T"}},
|
| 28 |
-
opset_version=17, dynamo=False,
|
| 29 |
-
)
|
| 30 |
-
return "contentvec.onnx", f"OK: feats={tuple(feats.shape)} -> contentvec.onnx gotowy do pobrania"
|
| 31 |
-
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
-
gr.Markdown("
|
| 35 |
-
btn = gr.Button("Eksportuj contentvec.onnx", variant="primary")
|
| 36 |
-
status = gr.Textbox(label="Status")
|
| 37 |
-
out = gr.File(label="Pobierz contentvec.onnx")
|
| 38 |
-
btn.click(export, outputs=[out, status])
|
| 39 |
-
|
| 40 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import os, threading, traceback
|
| 3 |
+
from huggingface_hub import HfApi
|
| 4 |
+
OUT="DriiftKing/contentvec-onnx"; TOK=os.environ.get("HF_TOKEN")
|
| 5 |
+
def job():
|
| 6 |
+
api=HfApi(token=TOK)
|
| 7 |
+
try:
|
| 8 |
+
import torch, torch.nn as nn
|
| 9 |
+
from transformers import HubertModel
|
| 10 |
+
class CV(nn.Module):
|
| 11 |
+
def __init__(s): super().__init__(); s.m=HubertModel.from_pretrained("lengyue233/content-vec-best")
|
| 12 |
+
@torch.no_grad()
|
| 13 |
+
def forward(s,w): return s.m(w,attention_mask=None,output_hidden_states=True).last_hidden_state
|
| 14 |
+
enc=CV().eval(); d=torch.randn(1,16000); f=enc(d)
|
| 15 |
+
assert f.shape[-1]==768, f"dim={tuple(f.shape)}"
|
| 16 |
+
try:
|
| 17 |
+
torch.onnx.export(enc,(d,),"contentvec.onnx",input_names=["wav_16k"],output_names=["feats"],
|
| 18 |
+
dynamic_axes={"wav_16k":{1:"L"},"feats":{1:"T"}},opset_version=17,dynamo=False)
|
| 19 |
+
except Exception:
|
| 20 |
+
torch.onnx.export(enc,(d,),"contentvec.onnx",input_names=["wav_16k"],output_names=["feats"],
|
| 21 |
+
dynamic_axes={"wav_16k":{1:"L"},"feats":{1:"T"}},opset_version=17)
|
| 22 |
+
api.upload_file(path_or_fileobj="contentvec.onnx",path_in_repo="contentvec.onnx",repo_id=OUT,repo_type="dataset")
|
| 23 |
+
print("UPLOADED OK", tuple(f.shape))
|
| 24 |
+
except Exception:
|
| 25 |
+
e=traceback.format_exc(); print(e)
|
| 26 |
+
open("error.txt","w").write(e)
|
| 27 |
+
try: api.upload_file(path_or_fileobj="error.txt",path_in_repo="error.txt",repo_id=OUT,repo_type="dataset")
|
| 28 |
+
except Exception: pass
|
| 29 |
+
threading.Thread(target=job,daemon=True).start()
|
| 30 |
+
import gradio as gr
|
| 31 |
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("Eksport contentvec.onnx leci w tle -> repo DriiftKing/contentvec-onnx")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
demo.launch()
|