Update evaluate.py
Browse files- evaluate.py +66 -33
evaluate.py
CHANGED
@@ -39,20 +39,41 @@ def init_reference_audio(reference_dir, output_dir):
|
|
39 |
os.makedirs(output_dir, exist_ok=True)
|
40 |
logger.info(f"π Created output directory: {output_dir}")
|
41 |
|
42 |
-
#
|
43 |
if not os.path.exists(reference_dir) or not os.access(os.path.dirname(reference_dir), os.W_OK):
|
44 |
# Use a directory in /tmp instead
|
45 |
-
|
46 |
-
logger.warning(f"β οΈ
|
47 |
-
reference_dir = new_reference_dir
|
48 |
|
49 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
if os.path.exists(reference_dir):
|
51 |
-
logger.info(f"β
Found reference audio directory: {reference_dir}")
|
52 |
-
|
53 |
-
# Log the contents to verify
|
54 |
pattern_dirs = [d for d in os.listdir(reference_dir)
|
55 |
-
|
56 |
logger.info(f"π Found reference patterns: {pattern_dirs}")
|
57 |
|
58 |
# Check each pattern directory for wav files
|
@@ -60,33 +81,12 @@ def init_reference_audio(reference_dir, output_dir):
|
|
60 |
pattern_path = os.path.join(reference_dir, pattern_dir_name)
|
61 |
wav_files = glob.glob(os.path.join(pattern_path, "*.wav"))
|
62 |
logger.info(f"π Found {len(wav_files)} wav files in {pattern_dir_name}")
|
63 |
-
else:
|
64 |
-
logger.warning(f"β οΈ Reference audio directory not found: {reference_dir}")
|
65 |
-
# Create the directory if it doesn't exist
|
66 |
-
try:
|
67 |
-
os.makedirs(reference_dir, exist_ok=True)
|
68 |
-
logger.info(f"π Created reference audio directory: {reference_dir}")
|
69 |
-
except PermissionError:
|
70 |
-
logger.error(f"β Permission denied when creating {reference_dir}")
|
71 |
-
# Try alternate location in /tmp
|
72 |
-
reference_dir = os.path.join('/tmp', 'reference_audios')
|
73 |
-
os.makedirs(reference_dir, exist_ok=True)
|
74 |
-
logger.info(f"π Created reference audio directory in alternate location: {reference_dir}")
|
75 |
|
76 |
-
# Return the (possibly updated) reference directory path
|
77 |
return reference_dir
|
78 |
|
79 |
except Exception as e:
|
80 |
logger.error(f"β Failed to set up reference audio directory: {str(e)}")
|
81 |
-
|
82 |
-
fallback_dir = os.path.join('/tmp', 'reference_audios')
|
83 |
-
try:
|
84 |
-
os.makedirs(fallback_dir, exist_ok=True)
|
85 |
-
logger.info(f"π Created fallback reference audio directory: {fallback_dir}")
|
86 |
-
return fallback_dir
|
87 |
-
except Exception as e2:
|
88 |
-
logger.critical(f"β Failed to create fallback directory: {str(e2)}")
|
89 |
-
return None
|
90 |
|
91 |
def handle_upload_reference(request, reference_dir, sample_rate):
|
92 |
"""Handle upload of reference audio files"""
|
@@ -191,9 +191,25 @@ def handle_evaluation_request(request, reference_dir, output_dir, sample_rate):
|
|
191 |
reference_files = glob.glob(os.path.join(reference_dir_path, "*.wav"))
|
192 |
logger.info(f"[{request_id}] π Found {len(reference_files)} reference files")
|
193 |
|
|
|
194 |
if not reference_files:
|
195 |
logger.warning(f"[{request_id}] β οΈ No reference audio files found in {reference_dir_path}")
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
lang_code = LANGUAGE_CODES.get(language, language)
|
199 |
logger.info(
|
@@ -371,4 +387,21 @@ def handle_evaluation_request(request, reference_dir, output_dir, sample_rate):
|
|
371 |
except:
|
372 |
pass
|
373 |
|
374 |
-
return jsonify({"error": f"Internal server error: {str(e)}"}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
os.makedirs(output_dir, exist_ok=True)
|
40 |
logger.info(f"π Created output directory: {output_dir}")
|
41 |
|
42 |
+
# Check if the reference audio directory exists
|
43 |
if not os.path.exists(reference_dir) or not os.access(os.path.dirname(reference_dir), os.W_OK):
|
44 |
# Use a directory in /tmp instead
|
45 |
+
reference_dir = os.path.join('/tmp', 'reference_audios')
|
46 |
+
logger.warning(f"β οΈ Using alternate reference directory: {reference_dir}")
|
|
|
47 |
|
48 |
+
# Make sure the reference directory exists
|
49 |
+
os.makedirs(reference_dir, exist_ok=True)
|
50 |
+
logger.info(f"π Created/verified reference audio directory: {reference_dir}")
|
51 |
+
|
52 |
+
# Create all the pattern subdirectories
|
53 |
+
setup_reference_patterns(reference_dir)
|
54 |
+
|
55 |
+
# Try to copy any existing reference files if they exist in the original location
|
56 |
+
original_ref_dir = "./reference_audios"
|
57 |
+
if os.path.exists(original_ref_dir) and reference_dir != original_ref_dir:
|
58 |
+
try:
|
59 |
+
import shutil
|
60 |
+
# Get all pattern directories
|
61 |
+
for item in os.listdir(original_ref_dir):
|
62 |
+
src_path = os.path.join(original_ref_dir, item)
|
63 |
+
dst_path = os.path.join(reference_dir, item)
|
64 |
+
|
65 |
+
if os.path.isdir(src_path):
|
66 |
+
# Copy directory and contents
|
67 |
+
if not os.path.exists(dst_path):
|
68 |
+
shutil.copytree(src_path, dst_path)
|
69 |
+
logger.info(f"π Copied reference pattern from {src_path} to {dst_path}")
|
70 |
+
except Exception as e:
|
71 |
+
logger.warning(f"β οΈ Could not copy original reference files: {str(e)}")
|
72 |
+
|
73 |
+
# Log the contents to verify
|
74 |
if os.path.exists(reference_dir):
|
|
|
|
|
|
|
75 |
pattern_dirs = [d for d in os.listdir(reference_dir)
|
76 |
+
if os.path.isdir(os.path.join(reference_dir, d))]
|
77 |
logger.info(f"π Found reference patterns: {pattern_dirs}")
|
78 |
|
79 |
# Check each pattern directory for wav files
|
|
|
81 |
pattern_path = os.path.join(reference_dir, pattern_dir_name)
|
82 |
wav_files = glob.glob(os.path.join(pattern_path, "*.wav"))
|
83 |
logger.info(f"π Found {len(wav_files)} wav files in {pattern_dir_name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
|
|
85 |
return reference_dir
|
86 |
|
87 |
except Exception as e:
|
88 |
logger.error(f"β Failed to set up reference audio directory: {str(e)}")
|
89 |
+
return reference_dir
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
def handle_upload_reference(request, reference_dir, sample_rate):
|
92 |
"""Handle upload of reference audio files"""
|
|
|
191 |
reference_files = glob.glob(os.path.join(reference_dir_path, "*.wav"))
|
192 |
logger.info(f"[{request_id}] π Found {len(reference_files)} reference files")
|
193 |
|
194 |
+
# Inside handle_evaluation_request, after checking for reference files:
|
195 |
if not reference_files:
|
196 |
logger.warning(f"[{request_id}] β οΈ No reference audio files found in {reference_dir_path}")
|
197 |
+
|
198 |
+
# Create a dummy reference file for testing
|
199 |
+
try:
|
200 |
+
dummy_file_path = os.path.join(reference_dir_path, "dummy_reference.wav")
|
201 |
+
logger.info(f"[{request_id}] π Creating dummy reference file: {dummy_file_path}")
|
202 |
+
|
203 |
+
# Create a 1-second silent WAV file
|
204 |
+
silent_audio = AudioSegment.silent(duration=1000, frame_rate=sample_rate)
|
205 |
+
silent_audio.export(dummy_file_path, format="wav")
|
206 |
+
|
207 |
+
# Add it to the list of reference files
|
208 |
+
reference_files = [dummy_file_path]
|
209 |
+
logger.info(f"[{request_id}] β
Created dummy reference file for testing")
|
210 |
+
except Exception as e:
|
211 |
+
logger.error(f"[{request_id}] β Failed to create dummy reference: {str(e)}")
|
212 |
+
return jsonify({"error": f"No reference audio found for {reference_locator}"}), 404
|
213 |
|
214 |
lang_code = LANGUAGE_CODES.get(language, language)
|
215 |
logger.info(
|
|
|
387 |
except:
|
388 |
pass
|
389 |
|
390 |
+
return jsonify({"error": f"Internal server error: {str(e)}"}), 500
|
391 |
+
|
392 |
+
def setup_reference_patterns(reference_dir):
|
393 |
+
"""Create standard reference pattern directories if they don't exist"""
|
394 |
+
reference_patterns = [
|
395 |
+
"mayap_a_abak", "mayap_a_ugtu", "mayap_a_gatpanapun", "mayap_a_bengi",
|
396 |
+
"komusta_ka", "malaus_ko_pu", "malaus_kayu", "agaganaka_da_ka",
|
397 |
+
"pagdulapan_da_ka", "kaluguran_da_ka", "dakal_a_salamat", "panapaya_mu_ku"
|
398 |
+
]
|
399 |
+
|
400 |
+
for pattern in reference_patterns:
|
401 |
+
pattern_dir = os.path.join(reference_dir, pattern)
|
402 |
+
if not os.path.exists(pattern_dir):
|
403 |
+
try:
|
404 |
+
os.makedirs(pattern_dir, exist_ok=True)
|
405 |
+
logger.info(f"π Created reference pattern directory: {pattern_dir}")
|
406 |
+
except Exception as e:
|
407 |
+
logger.error(f"β Failed to create reference pattern directory {pattern_dir}: {str(e)}")
|