Update handler.py
Browse files- handler.py +45 -43
handler.py
CHANGED
@@ -11,49 +11,51 @@ import shutil
|
|
11 |
import zipfile
|
12 |
import requests
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
class EndpointHandler:
|
15 |
def __init__(self, model_dir=None):
|
16 |
self.model_dir = model_dir
|
17 |
-
self.download_and_extract_files()
|
18 |
-
from voice_processing import tts, get_model_names, voice_mapping, get_unique_filename
|
19 |
-
self.tts = tts
|
20 |
-
self.get_model_names = get_model_names
|
21 |
-
self.voice_mapping = voice_mapping
|
22 |
-
self.get_unique_filename = get_unique_filename
|
23 |
-
|
24 |
-
def download_and_extract_files(self):
|
25 |
-
files_to_download = [
|
26 |
-
("config.py", "https://www.dropbox.com/scl/fi/xmcru76czz7m1p9c2axkg/config.py?rlkey=b1n42efljeegwngjk0xpbtqpi&st=4lyfpoor&dl=1"),
|
27 |
-
("hubert_base.pt", "https://www.dropbox.com/scl/fi/g7oohuwfzlnrbd8zic6gj/hubert_base.pt?rlkey=ddeyqex1morsm54azyakmd62e&st=rsrvf964&dl=1"),
|
28 |
-
("lib.zip", "https://www.dropbox.com/scl/fi/ia6p6cf5xvcbi78dmkbbz/lib.zip?rlkey=k3chc1nlaswsqdo7slqco56wi&st=19n9syfd&dl=1"),
|
29 |
-
("rmvpe.pt", "https://www.dropbox.com/scl/fi/7pl7u6fvydwgtz19n8nzx/rmvpe.pt?rlkey=tnbxmarogivbw3qy34hgy7r7q&st=um8d4230&dl=1"),
|
30 |
-
("rmvpe.py", "https://www.dropbox.com/scl/fi/i2shk4otwyg4ns8yod5h1/rmvpe.py?rlkey=l7313htdh1ihylb6bx91el0lv&st=xhkfog8j&dl=1"),
|
31 |
-
("vc_infer_pipeline.py", "https://www.dropbox.com/scl/fi/bvz7s2wf2y67twpg583lg/vc_infer_pipeline.py?rlkey=q4w7oww5e7e2qdfh3herofk4o&st=4sck87ny&dl=1"),
|
32 |
-
("voice_processing.py", "https://www.dropbox.com/scl/fi/emrmjsuz0mod4r2x9e43f/voice_processing.py?rlkey=6baomwowns9y3yq1pl6syer0t&st=d9u51gba&dl=1"),
|
33 |
-
("weights.zip", "https://www.dropbox.com/scl/fi/tr5a04wlow5go8cv3d6qp/weights.zip?rlkey=qvpwax97nn5a4iv79g76lcbz2&st=5ueb2gva&dl=1")
|
34 |
-
]
|
35 |
-
|
36 |
-
for file_name, url in files_to_download:
|
37 |
-
if not os.path.exists(file_name):
|
38 |
-
response = requests.get(url)
|
39 |
-
with open(file_name, "wb") as file:
|
40 |
-
file.write(response.content)
|
41 |
-
|
42 |
-
if file_name.endswith(".zip"):
|
43 |
-
with zipfile.ZipFile(file_name, "r") as zip_ref:
|
44 |
-
extract_to = os.path.splitext(file_name)[0]
|
45 |
-
for member in zip_ref.namelist():
|
46 |
-
# Extract files into the target directory without the first part of the path
|
47 |
-
member_path = os.path.join(extract_to, *member.split('/')[1:])
|
48 |
-
if member.endswith('/'):
|
49 |
-
os.makedirs(member_path, exist_ok=True)
|
50 |
-
else:
|
51 |
-
os.makedirs(os.path.dirname(member_path), exist_ok=True)
|
52 |
-
with open(member_path, 'wb') as f:
|
53 |
-
f.write(zip_ref.read(member))
|
54 |
-
|
55 |
-
# Optionally, remove the zip file after extraction
|
56 |
-
os.remove(file_name)
|
57 |
|
58 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
59 |
try:
|
@@ -75,11 +77,11 @@ class EndpointHandler:
|
|
75 |
use_uploaded_voice = json_data["use_uploaded_voice"]
|
76 |
voice_upload_file = json_data.get("voice_upload_file", None)
|
77 |
|
78 |
-
edge_tts_voice =
|
79 |
if not edge_tts_voice:
|
80 |
raise ValueError(f"Invalid voice '{selected_voice}'.")
|
81 |
|
82 |
-
info, edge_tts_output_path, tts_output_data, edge_output_file = asyncio.run(
|
83 |
model_name, tts_text, edge_tts_voice, slang_rate, use_uploaded_voice, voice_upload_file
|
84 |
))
|
85 |
|
@@ -112,6 +114,6 @@ class EndpointHandler:
|
|
112 |
return {"error": "Invalid Hugging Face JSON structure."}
|
113 |
|
114 |
def save_audio_data_to_file(self, audio_data, sample_rate=40000):
|
115 |
-
file_path =
|
116 |
wavfile.write(file_path, sample_rate, audio_data)
|
117 |
return file_path
|
|
|
11 |
import zipfile
|
12 |
import requests
|
13 |
|
14 |
+
|
15 |
+
def download_and_extract_files():
|
16 |
+
files_to_download = [
|
17 |
+
("config.py", "https://www.dropbox.com/scl/fi/zgfyhxvdnt64gkbb7m5i5/config.py?rlkey=xbq6kfmqqqm701x3c05oeef7z&st=cvm4csml&dl=1"),
|
18 |
+
("hubert_base.pt", "https://www.dropbox.com/scl/fi/g7oohuwfzlnrbd8zic6gj/hubert_base.pt?rlkey=ddeyqex1morsm54azyakmd62e&st=rsrvf964&dl=1"),
|
19 |
+
("lib.zip", "https://www.dropbox.com/scl/fi/ia6p6cf5xvcbi78dmkbbz/lib.zip?rlkey=k3chc1nlaswsqdo7slqco56wi&st=19n9syfd&dl=1"),
|
20 |
+
("rmvpe.pt", "https://www.dropbox.com/scl/fi/7pl7u6fvydwgtz19n8nzx/rmvpe.pt?rlkey=tnbxmarogivbw3qy34hgy7r7q&st=um8d4230&dl=1"),
|
21 |
+
("rmvpe.py", "https://www.dropbox.com/scl/fi/i2shk4otwyg4ns8yod5h1/rmvpe.py?rlkey=l7313htdh1ihylb6bx91el0lv&st=xhkfog8j&dl=1"),
|
22 |
+
("vc_infer_pipeline.py", "https://www.dropbox.com/scl/fi/bvz7s2wf2y67twpg583lg/vc_infer_pipeline.py?rlkey=q4w7oww5e7e2qdfh3herofk4o&st=4sck87ny&dl=1"),
|
23 |
+
("voice_processing.py", "https://www.dropbox.com/scl/fi/emrmjsuz0mod4r2x9e43f/voice_processing.py?rlkey=6baomwowns9y3yq1pl6syer0t&st=d9u51gba&dl=1"),
|
24 |
+
("weights.zip", "https://www.dropbox.com/scl/fi/tr5a04wlow5go8cv3d6qp/weights.zip?rlkey=qvpwax97nn5a4iv79g76lcbz2&st=5ueb2gva&dl=1"),
|
25 |
+
("handler.py", "https://www.dropbox.com/scl/fi/vu6uoc01ozoumj77grsqa/handler.py?rlkey=anzyn12mrc7wgtvf5lzfkzf8i&st=nn1d3iq3&dl=1")
|
26 |
+
]
|
27 |
+
|
28 |
+
for file_name, url in files_to_download:
|
29 |
+
if not os.path.exists(file_name):
|
30 |
+
response = requests.get(url)
|
31 |
+
with open(file_name, "wb") as file:
|
32 |
+
file.write(response.content)
|
33 |
+
|
34 |
+
if file_name.endswith(".zip"):
|
35 |
+
with zipfile.ZipFile(file_name, "r") as zip_ref:
|
36 |
+
extract_to = os.path.splitext(file_name)[0]
|
37 |
+
for member in zip_ref.namelist():
|
38 |
+
# Extract files into the target directory without the first part of the path
|
39 |
+
member_path = os.path.join(extract_to, *member.split('/')[1:])
|
40 |
+
if member.endswith('/'):
|
41 |
+
os.makedirs(member_path, exist_ok=True)
|
42 |
+
else:
|
43 |
+
os.makedirs(os.path.dirname(member_path), exist_ok=True)
|
44 |
+
with open(member_path, 'wb') as f:
|
45 |
+
f.write(zip_ref.read(member))
|
46 |
+
|
47 |
+
# Optionally, remove the zip file after extraction
|
48 |
+
os.remove(file_name)
|
49 |
+
|
50 |
+
# Run the function
|
51 |
+
download_and_extract_files()
|
52 |
+
|
53 |
+
from voice_processing import tts, get_model_names, voice_mapping, get_unique_filename
|
54 |
+
|
55 |
+
|
56 |
class EndpointHandler:
|
57 |
def __init__(self, model_dir=None):
|
58 |
self.model_dir = model_dir
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
61 |
try:
|
|
|
77 |
use_uploaded_voice = json_data["use_uploaded_voice"]
|
78 |
voice_upload_file = json_data.get("voice_upload_file", None)
|
79 |
|
80 |
+
edge_tts_voice = voice_mapping.get(selected_voice)
|
81 |
if not edge_tts_voice:
|
82 |
raise ValueError(f"Invalid voice '{selected_voice}'.")
|
83 |
|
84 |
+
info, edge_tts_output_path, tts_output_data, edge_output_file = asyncio.run(tts(
|
85 |
model_name, tts_text, edge_tts_voice, slang_rate, use_uploaded_voice, voice_upload_file
|
86 |
))
|
87 |
|
|
|
114 |
return {"error": "Invalid Hugging Face JSON structure."}
|
115 |
|
116 |
def save_audio_data_to_file(self, audio_data, sample_rate=40000):
|
117 |
+
file_path = get_unique_filename('wav')
|
118 |
wavfile.write(file_path, sample_rate, audio_data)
|
119 |
return file_path
|