Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -120,36 +120,26 @@ def detect_onnx_models(path):
|
|
120 |
onnx_models = glob.glob(path + '/*.onnx')
|
121 |
onnx_configs = glob.glob(path + '/*.json')
|
122 |
if len(onnx_models) > 1:
|
123 |
-
return onnx_models, onnx_configs
|
124 |
elif len(onnx_models) == 1:
|
125 |
return onnx_models[0], onnx_configs[0]
|
126 |
else:
|
127 |
return None
|
128 |
|
129 |
-
|
130 |
-
renamed_audio_file = None
|
131 |
-
|
132 |
-
# Define an endpoint to serve the speaker_id_map
|
133 |
@app.get("/get_speaker_id_map")
|
134 |
-
async def get_speaker_id_map():
|
135 |
-
|
136 |
-
# For example, you can use the selected_model value from the request
|
137 |
-
# to determine which model's config to load and extract the speaker_id_map.
|
138 |
-
|
139 |
-
# Replace this with the actual code to load the speaker_id_map
|
140 |
-
selected_model = "model_name" # Replace with the selected model name
|
141 |
-
config = model_configurations[selected_model]
|
142 |
|
143 |
if config:
|
144 |
speaker_id_map = config.get("speaker_id_map", {})
|
145 |
return {"speaker_id_map": speaker_id_map}
|
146 |
|
147 |
-
# Handle the case where the config is not available
|
148 |
return {"speaker_id_map": {}}
|
149 |
|
150 |
@app.on_event("startup")
|
151 |
async def load_model_data():
|
152 |
-
global onnx_models # Make
|
153 |
# Load data for all models in the directory upon startup
|
154 |
sys.path.append('./content/piper/src/python')
|
155 |
models_path = "./content/piper/src/python"
|
@@ -162,18 +152,17 @@ async def load_model_data():
|
|
162 |
sess_options = onnxruntime.SessionOptions()
|
163 |
|
164 |
# Collect data for all models in the directory and populate model_configurations
|
165 |
-
model_names = detect_onnx_models(models_path)
|
166 |
onnx_models = model_names # Populate onnx_models here
|
167 |
-
for
|
168 |
# Load the configuration data for each model (including speaker_id_map)
|
169 |
-
config = load_model_configuration(
|
170 |
-
model_configurations[
|
171 |
-
|
172 |
-
|
173 |
-
def load_model_configuration(model_name):
|
174 |
-
# Assuming model_name is the path to the ONNX model file, e.g., 'model.onnx'
|
175 |
-
config_file_path = model_name.replace('.onnx', '.json')
|
176 |
|
|
|
|
|
|
|
|
|
177 |
try:
|
178 |
with open(config_file_path, 'r') as config_file:
|
179 |
config_data = json.load(config_file)
|
|
|
120 |
onnx_models = glob.glob(path + '/*.onnx')
|
121 |
onnx_configs = glob.glob(path + '/*.json')
|
122 |
if len(onnx_models) > 1:
|
123 |
+
return onnx_models, onnx_configs # Return both lists as a tuple
|
124 |
elif len(onnx_models) == 1:
|
125 |
return onnx_models[0], onnx_configs[0]
|
126 |
else:
|
127 |
return None
|
128 |
|
|
|
|
|
|
|
|
|
129 |
@app.get("/get_speaker_id_map")
|
130 |
+
async def get_speaker_id_map(selected_model: str):
|
131 |
+
config = model_configurations.get(selected_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
if config:
|
134 |
speaker_id_map = config.get("speaker_id_map", {})
|
135 |
return {"speaker_id_map": speaker_id_map}
|
136 |
|
137 |
+
# Handle the case where the config is not available for the selected model
|
138 |
return {"speaker_id_map": {}}
|
139 |
|
140 |
@app.on_event("startup")
|
141 |
async def load_model_data():
|
142 |
+
global onnx_models, model_configurations # Make these variables available globally
|
143 |
# Load data for all models in the directory upon startup
|
144 |
sys.path.append('./content/piper/src/python')
|
145 |
models_path = "./content/piper/src/python"
|
|
|
152 |
sess_options = onnxruntime.SessionOptions()
|
153 |
|
154 |
# Collect data for all models in the directory and populate model_configurations
|
155 |
+
model_names, config_names = detect_onnx_models(models_path)
|
156 |
onnx_models = model_names # Populate onnx_models here
|
157 |
+
for config_name in config_names:
|
158 |
# Load the configuration data for each model (including speaker_id_map)
|
159 |
+
config = load_model_configuration(config_name) # Use config_name as the argument
|
160 |
+
model_configurations[config_name] = config # Use config_name as the key
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
+
def load_model_configuration(config_name):
|
163 |
+
# Assuming config_name is the path to the ONNX model config file, e.g., 'model.json'
|
164 |
+
config_file_path = config_name
|
165 |
+
|
166 |
try:
|
167 |
with open(config_file_path, 'r') as config_file:
|
168 |
config_data = json.load(config_file)
|