Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from sklearn.metrics.pairwise import cosine_similarity
|
|
| 7 |
import plotly.express as px
|
| 8 |
import os
|
| 9 |
import tempfile
|
|
|
|
| 10 |
|
| 11 |
# ----------------------------
|
| 12 |
# Core Functions
|
|
@@ -22,8 +23,10 @@ def segment_audio(y, sr, frame_length_ms, hop_length_ms, window_type):
|
|
| 22 |
if frame_length > len(y):
|
| 23 |
raise ValueError("Frame length is longer than audio duration.")
|
| 24 |
frames = librosa.util.frame(y, frame_length=frame_length, hop_length=hop_length)
|
|
|
|
| 25 |
if window_type != "rectangular":
|
| 26 |
-
|
|
|
|
| 27 |
frames = frames * window[:, None]
|
| 28 |
return frames, sr
|
| 29 |
|
|
@@ -94,7 +97,6 @@ def analyze_audio_pair(
|
|
| 94 |
frame_length_ms,
|
| 95 |
hop_length_ms,
|
| 96 |
window_type,
|
| 97 |
-
features_list,
|
| 98 |
n_clusters,
|
| 99 |
comparison_metrics
|
| 100 |
):
|
|
@@ -116,20 +118,18 @@ def analyze_audio_pair(
|
|
| 116 |
frames_near, _ = segment_audio(y_near, sr, frame_length_ms, hop_length_ms, window_type)
|
| 117 |
frames_far, _ = segment_audio(y_far, sr, frame_length_ms, hop_length_ms, window_type)
|
| 118 |
|
| 119 |
-
# Extract features
|
| 120 |
near_features = extract_features(frames_near, sr)
|
| 121 |
far_features = extract_features(frames_far, sr)
|
| 122 |
|
| 123 |
# Compare
|
| 124 |
comparison_df = compare_frames(near_features, far_features, comparison_metrics)
|
| 125 |
|
| 126 |
-
# Cluster near-field
|
| 127 |
clustered_df = cluster_frames(near_features, n_clusters)
|
| 128 |
|
| 129 |
# Plots
|
| 130 |
plot_comparison = None
|
| 131 |
-
plot_scatter = None
|
| 132 |
-
|
| 133 |
if "Euclidean Distance" in comparison_metrics:
|
| 134 |
plot_comparison = px.line(
|
| 135 |
comparison_df,
|
|
@@ -145,14 +145,12 @@ def analyze_audio_pair(
|
|
| 145 |
title="Cosine Similarity Between Near & Far Frames"
|
| 146 |
)
|
| 147 |
else:
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
y=comparison_df.columns[1] if len(comparison_df.columns) > 1 else [],
|
| 152 |
-
title="Frame Comparison"
|
| 153 |
-
)
|
| 154 |
|
| 155 |
-
# Scatter
|
|
|
|
| 156 |
if "rms" in clustered_df.columns and "spectral_centroid" in clustered_df.columns:
|
| 157 |
plot_scatter = px.scatter(
|
| 158 |
clustered_df,
|
|
@@ -165,25 +163,15 @@ def analyze_audio_pair(
|
|
| 165 |
else:
|
| 166 |
plot_scatter = px.scatter(title="Not enough features for scatter plot")
|
| 167 |
|
| 168 |
-
return
|
| 169 |
-
plot_comparison,
|
| 170 |
-
comparison_df,
|
| 171 |
-
plot_scatter,
|
| 172 |
-
clustered_df
|
| 173 |
-
)
|
| 174 |
|
| 175 |
def export_results(plot_comparison, comparison_df, plot_scatter, clustered_df):
|
| 176 |
temp_dir = tempfile.mkdtemp()
|
| 177 |
-
files = []
|
| 178 |
-
|
| 179 |
-
# Save DataFrames
|
| 180 |
comp_path = os.path.join(temp_dir, "frame_comparisons.csv")
|
| 181 |
cluster_path = os.path.join(temp_dir, "clustered_frames.csv")
|
| 182 |
comparison_df.to_csv(comp_path, index=False)
|
| 183 |
clustered_df.to_csv(cluster_path, index=False)
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
return files
|
| 187 |
|
| 188 |
# ----------------------------
|
| 189 |
# Gradio Interface
|
|
@@ -230,7 +218,6 @@ with gr.Blocks(title="Near vs Far Field Audio Analyzer") as demo:
|
|
| 230 |
inputs=[
|
| 231 |
near_file, far_file,
|
| 232 |
frame_length_ms, hop_length_ms, window_type,
|
| 233 |
-
gr.State([]), # features_list (not used in UI but kept for extensibility)
|
| 234 |
n_clusters,
|
| 235 |
comparison_metrics
|
| 236 |
],
|
|
|
|
| 7 |
import plotly.express as px
|
| 8 |
import os
|
| 9 |
import tempfile
|
| 10 |
+
from scipy.signal import get_window as scipy_get_window # ✅ Fix here
|
| 11 |
|
| 12 |
# ----------------------------
|
| 13 |
# Core Functions
|
|
|
|
| 23 |
if frame_length > len(y):
|
| 24 |
raise ValueError("Frame length is longer than audio duration.")
|
| 25 |
frames = librosa.util.frame(y, frame_length=frame_length, hop_length=hop_length)
|
| 26 |
+
|
| 27 |
if window_type != "rectangular":
|
| 28 |
+
# ✅ Use scipy.signal.get_window instead of librosa.get_window
|
| 29 |
+
window = scipy_get_window(window_type, frame_length)
|
| 30 |
frames = frames * window[:, None]
|
| 31 |
return frames, sr
|
| 32 |
|
|
|
|
| 97 |
frame_length_ms,
|
| 98 |
hop_length_ms,
|
| 99 |
window_type,
|
|
|
|
| 100 |
n_clusters,
|
| 101 |
comparison_metrics
|
| 102 |
):
|
|
|
|
| 118 |
frames_near, _ = segment_audio(y_near, sr, frame_length_ms, hop_length_ms, window_type)
|
| 119 |
frames_far, _ = segment_audio(y_far, sr, frame_length_ms, hop_length_ms, window_type)
|
| 120 |
|
| 121 |
+
# Extract features
|
| 122 |
near_features = extract_features(frames_near, sr)
|
| 123 |
far_features = extract_features(frames_far, sr)
|
| 124 |
|
| 125 |
# Compare
|
| 126 |
comparison_df = compare_frames(near_features, far_features, comparison_metrics)
|
| 127 |
|
| 128 |
+
# Cluster near-field
|
| 129 |
clustered_df = cluster_frames(near_features, n_clusters)
|
| 130 |
|
| 131 |
# Plots
|
| 132 |
plot_comparison = None
|
|
|
|
|
|
|
| 133 |
if "Euclidean Distance" in comparison_metrics:
|
| 134 |
plot_comparison = px.line(
|
| 135 |
comparison_df,
|
|
|
|
| 145 |
title="Cosine Similarity Between Near & Far Frames"
|
| 146 |
)
|
| 147 |
else:
|
| 148 |
+
# Fallback
|
| 149 |
+
col = comparison_df.columns[1] if len(comparison_df.columns) > 1 else "frame_index"
|
| 150 |
+
plot_comparison = px.line(comparison_df, x="frame_index", y=col, title="Frame Comparison")
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
# Scatter plot
|
| 153 |
+
plot_scatter = None
|
| 154 |
if "rms" in clustered_df.columns and "spectral_centroid" in clustered_df.columns:
|
| 155 |
plot_scatter = px.scatter(
|
| 156 |
clustered_df,
|
|
|
|
| 163 |
else:
|
| 164 |
plot_scatter = px.scatter(title="Not enough features for scatter plot")
|
| 165 |
|
| 166 |
+
return plot_comparison, comparison_df, plot_scatter, clustered_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
def export_results(plot_comparison, comparison_df, plot_scatter, clustered_df):
|
| 169 |
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
|
|
| 170 |
comp_path = os.path.join(temp_dir, "frame_comparisons.csv")
|
| 171 |
cluster_path = os.path.join(temp_dir, "clustered_frames.csv")
|
| 172 |
comparison_df.to_csv(comp_path, index=False)
|
| 173 |
clustered_df.to_csv(cluster_path, index=False)
|
| 174 |
+
return [comp_path, cluster_path]
|
|
|
|
|
|
|
| 175 |
|
| 176 |
# ----------------------------
|
| 177 |
# Gradio Interface
|
|
|
|
| 218 |
inputs=[
|
| 219 |
near_file, far_file,
|
| 220 |
frame_length_ms, hop_length_ms, window_type,
|
|
|
|
| 221 |
n_clusters,
|
| 222 |
comparison_metrics
|
| 223 |
],
|