Update handler.py
Browse files- handler.py +118 -74
handler.py
CHANGED
@@ -1,75 +1,119 @@
|
|
1 |
-
from pydantic import BaseModel
|
2 |
-
from environs import Env
|
3 |
-
from typing import List, Dict, Any
|
4 |
-
import os
|
5 |
-
import base64
|
6 |
-
import numpy as np
|
7 |
-
import librosa
|
8 |
-
from scipy.io import wavfile
|
9 |
-
import asyncio
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
return file_path
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
+
from environs import Env
|
3 |
+
from typing import List, Dict, Any
|
4 |
+
import os
|
5 |
+
import base64
|
6 |
+
import numpy as np
|
7 |
+
import librosa
|
8 |
+
from scipy.io import wavfile
|
9 |
+
import asyncio
|
10 |
+
import shutil
|
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/ls7vmjk75uou8ayfn6kj4/config.py?rlkey=4qluzl5l07zq1j9mkl9n6j66u&st=0yit9dzx&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:
|
62 |
+
if "inputs" in data: # Check if data is in Hugging Face JSON format
|
63 |
+
return self.process_hf_input(data)
|
64 |
+
else:
|
65 |
+
return self.process_json_input(data)
|
66 |
+
except ValueError as e:
|
67 |
+
return {"error": str(e)}
|
68 |
+
except Exception as e:
|
69 |
+
return {"error": str(e)}
|
70 |
+
|
71 |
+
def process_json_input(self, json_data):
|
72 |
+
if all(key in json_data for key in ["model_name", "tts_text", "selected_voice", "slang_rate", "use_uploaded_voice"]):
|
73 |
+
model_name = json_data["model_name"]
|
74 |
+
tts_text = json_data["tts_text"]
|
75 |
+
selected_voice = json_data["selected_voice"]
|
76 |
+
slang_rate = json_data["slang_rate"]
|
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 |
+
|
88 |
+
if edge_output_file and os.path.exists(edge_output_file):
|
89 |
+
os.remove(edge_output_file)
|
90 |
+
|
91 |
+
_, audio_output = tts_output_data
|
92 |
+
|
93 |
+
audio_file_path = self.save_audio_data_to_file(audio_output) if isinstance(audio_output, np.ndarray) else audio_output
|
94 |
+
|
95 |
+
try:
|
96 |
+
with open(audio_file_path, 'rb') as file:
|
97 |
+
audio_bytes = file.read()
|
98 |
+
audio_data_uri = f"data:audio/wav;base64,{base64.b64encode(audio_bytes).decode('utf-8')}"
|
99 |
+
except Exception as e:
|
100 |
+
raise Exception(f"Failed to read audio file: {e}")
|
101 |
+
finally:
|
102 |
+
if os.path.exists(audio_file_path):
|
103 |
+
os.remove(audio_file_path)
|
104 |
+
|
105 |
+
return {"info": info, "audio_data_uri": audio_data_uri}
|
106 |
+
else:
|
107 |
+
raise ValueError("Invalid JSON structure.")
|
108 |
+
|
109 |
+
def process_hf_input(self, hf_data):
|
110 |
+
if "inputs" in hf_data:
|
111 |
+
actual_data = hf_data["inputs"]
|
112 |
+
return self.process_json_input(actual_data)
|
113 |
+
else:
|
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
|