Spaces:
Runtime error
Runtime error
aether-raider commited on
Commit ·
a60f44b
1
Parent(s): 4d18ab0
fixing sample audios
Browse files
backend/__pycache__/data_manager.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/data_manager.cpython-311.pyc and b/backend/__pycache__/data_manager.cpython-311.pyc differ
|
|
|
backend/__pycache__/models.cpython-311.pyc
CHANGED
|
Binary files a/backend/__pycache__/models.cpython-311.pyc and b/backend/__pycache__/models.cpython-311.pyc differ
|
|
|
backend/data_manager.py
CHANGED
|
@@ -27,19 +27,17 @@ class DataManager:
|
|
| 27 |
|
| 28 |
def _get_audio_data(self, audio_val) -> Optional[tuple]:
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
Returns tuple (
|
| 32 |
"""
|
| 33 |
try:
|
| 34 |
array = None
|
| 35 |
sr = None
|
| 36 |
-
|
| 37 |
if isinstance(audio_val, dict):
|
| 38 |
array = audio_val.get("array")
|
| 39 |
sr = audio_val.get("sampling_rate")
|
| 40 |
|
| 41 |
if array is None or sr is None:
|
| 42 |
-
# try mapping-style then attributes
|
| 43 |
try:
|
| 44 |
array = audio_val["array"]
|
| 45 |
sr = audio_val["sampling_rate"]
|
|
@@ -48,12 +46,13 @@ class DataManager:
|
|
| 48 |
sr = getattr(audio_val, "sampling_rate", None)
|
| 49 |
|
| 50 |
if array is not None and sr is not None:
|
| 51 |
-
|
| 52 |
-
return (
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
-
print(f"[WARN] Failed to
|
| 55 |
|
| 56 |
-
print("[WARN] Could not
|
| 57 |
return None
|
| 58 |
|
| 59 |
def load_clips(self) -> List[Clip]:
|
|
@@ -75,7 +74,7 @@ class DataManager:
|
|
| 75 |
|
| 76 |
audio_data = self._get_audio_data(audio_val)
|
| 77 |
if audio_data is None:
|
| 78 |
-
print(f"[WARN] Skipping clip {row.get('exercise_id')} – could not
|
| 79 |
continue
|
| 80 |
|
| 81 |
clip = Clip(
|
|
@@ -85,7 +84,7 @@ class DataManager:
|
|
| 85 |
exercise=row["exercise"],
|
| 86 |
exercise_id=row["exercise_id"],
|
| 87 |
transcript=row["rt"],
|
| 88 |
-
audio_url=audio_data,
|
| 89 |
)
|
| 90 |
clips.append(clip)
|
| 91 |
|
|
|
|
| 27 |
|
| 28 |
def _get_audio_data(self, audio_val) -> Optional[tuple]:
|
| 29 |
"""
|
| 30 |
+
Handle audio data from HuggingFace dataset with LFS files.
|
| 31 |
+
Returns tuple (sample_rate, audio_array) that Gradio can handle.
|
| 32 |
"""
|
| 33 |
try:
|
| 34 |
array = None
|
| 35 |
sr = None
|
|
|
|
| 36 |
if isinstance(audio_val, dict):
|
| 37 |
array = audio_val.get("array")
|
| 38 |
sr = audio_val.get("sampling_rate")
|
| 39 |
|
| 40 |
if array is None or sr is None:
|
|
|
|
| 41 |
try:
|
| 42 |
array = audio_val["array"]
|
| 43 |
sr = audio_val["sampling_rate"]
|
|
|
|
| 46 |
sr = getattr(audio_val, "sampling_rate", None)
|
| 47 |
|
| 48 |
if array is not None and sr is not None:
|
| 49 |
+
array_np = np.array(array, dtype=np.float32)
|
| 50 |
+
return (int(sr), array_np)
|
| 51 |
+
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"[WARN] Failed to process audio data: {e}")
|
| 54 |
|
| 55 |
+
print("[WARN] Could not process audio data for this example")
|
| 56 |
return None
|
| 57 |
|
| 58 |
def load_clips(self) -> List[Clip]:
|
|
|
|
| 74 |
|
| 75 |
audio_data = self._get_audio_data(audio_val)
|
| 76 |
if audio_data is None:
|
| 77 |
+
print(f"[WARN] Skipping clip {row.get('exercise_id')} – could not process audio data")
|
| 78 |
continue
|
| 79 |
|
| 80 |
clip = Clip(
|
|
|
|
| 84 |
exercise=row["exercise"],
|
| 85 |
exercise_id=row["exercise_id"],
|
| 86 |
transcript=row["rt"],
|
| 87 |
+
audio_url=audio_data,
|
| 88 |
)
|
| 89 |
clips.append(clip)
|
| 90 |
|
backend/models.py
CHANGED
|
@@ -19,10 +19,23 @@ def get_display_model_name(internal_name: str) -> str:
|
|
| 19 |
|
| 20 |
def audio_to_base64_url(audio_data):
|
| 21 |
"""Convert audio data to base64 URL for HTML audio elements."""
|
| 22 |
-
if isinstance(audio_data, str)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
elif isinstance(audio_data, tuple) and len(audio_data) == 2:
|
| 25 |
-
# Convert (
|
| 26 |
try:
|
| 27 |
import numpy as np
|
| 28 |
import base64
|
|
@@ -32,7 +45,7 @@ def audio_to_base64_url(audio_data):
|
|
| 32 |
except ImportError:
|
| 33 |
return None
|
| 34 |
|
| 35 |
-
|
| 36 |
if sf is not None:
|
| 37 |
buf = io.BytesIO()
|
| 38 |
sf.write(buf, np.array(array), int(sr), format="WAV")
|
|
|
|
| 19 |
|
| 20 |
def audio_to_base64_url(audio_data):
|
| 21 |
"""Convert audio data to base64 URL for HTML audio elements."""
|
| 22 |
+
if isinstance(audio_data, str):
|
| 23 |
+
if audio_data.startswith("data:audio/"):
|
| 24 |
+
return audio_data
|
| 25 |
+
elif audio_data.endswith(('.wav', '.mp3', '.flac', '.ogg')):
|
| 26 |
+
# Handle file path from LFS - convert to base64
|
| 27 |
+
try:
|
| 28 |
+
import base64
|
| 29 |
+
import os
|
| 30 |
+
if os.path.exists(audio_data):
|
| 31 |
+
with open(audio_data, "rb") as f:
|
| 32 |
+
audio_bytes = f.read()
|
| 33 |
+
b64 = base64.b64encode(audio_bytes).decode("ascii")
|
| 34 |
+
return f"data:audio/wav;base64,{b64}"
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"[WARN] Failed to convert file to base64 URL: {e}")
|
| 37 |
elif isinstance(audio_data, tuple) and len(audio_data) == 2:
|
| 38 |
+
# Convert (sample_rate, array) tuple to base64 URL - Gradio format
|
| 39 |
try:
|
| 40 |
import numpy as np
|
| 41 |
import base64
|
|
|
|
| 45 |
except ImportError:
|
| 46 |
return None
|
| 47 |
|
| 48 |
+
sr, array = audio_data # Gradio uses (sample_rate, array) order
|
| 49 |
if sf is not None:
|
| 50 |
buf = io.BytesIO()
|
| 51 |
sf.write(buf, np.array(array), int(sr), format="WAV")
|