added random length cutting
Browse files
utils.py
CHANGED
@@ -11,6 +11,7 @@ from functools import partial
|
|
11 |
import pandas as pd
|
12 |
import numpy as np
|
13 |
from huggingface_hub import snapshot_download
|
|
|
14 |
|
15 |
def enable_buttons_side_by_side():
|
16 |
return tuple(gr.update(visible=True, interactive=True) for i in range(6))
|
@@ -94,19 +95,23 @@ class AudioStateIG:
|
|
94 |
def __init__(self, row):
|
95 |
self.conv_id = uuid4().hex
|
96 |
self.row = row
|
|
|
97 |
|
98 |
def dict(self):
|
99 |
base = {
|
100 |
"conv_id": self.conv_id,
|
101 |
"label": self.row.label,
|
102 |
"filename": self.row.filename,
|
103 |
-
"duration": self.row.duration,
|
104 |
"song_id": str(self.row.id),
|
105 |
"source": self.row.source,
|
106 |
-
"algorithm": self.row.algorithm
|
107 |
}
|
108 |
return base
|
109 |
|
|
|
|
|
|
|
110 |
def get_ip(request: gr.Request):
|
111 |
if request:
|
112 |
if "cf-connecting-ip" in request.headers:
|
@@ -124,13 +129,31 @@ def get_song(idx, df = df, filenames = filenames):
|
|
124 |
state = AudioStateIG(row)
|
125 |
return state, audio_path
|
126 |
|
127 |
-
def
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
130 |
|
|
|
|
|
131 |
|
132 |
-
|
|
|
133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
def fake_last_response(
|
135 |
state, request: gr.Request
|
136 |
):
|
|
|
11 |
import pandas as pd
|
12 |
import numpy as np
|
13 |
from huggingface_hub import snapshot_download
|
14 |
+
import librosa
|
15 |
|
16 |
def enable_buttons_side_by_side():
|
17 |
return tuple(gr.update(visible=True, interactive=True) for i in range(6))
|
|
|
95 |
def __init__(self, row):
|
96 |
self.conv_id = uuid4().hex
|
97 |
self.row = row
|
98 |
+
self.new_duration = None
|
99 |
|
100 |
def dict(self):
|
101 |
base = {
|
102 |
"conv_id": self.conv_id,
|
103 |
"label": self.row.label,
|
104 |
"filename": self.row.filename,
|
105 |
+
"duration": self.row.duration if self.new_duration is None else self.new_duration,
|
106 |
"song_id": str(self.row.id),
|
107 |
"source": self.row.source,
|
108 |
+
"algorithm": self.row.algorithm,
|
109 |
}
|
110 |
return base
|
111 |
|
112 |
+
def update_duration(self, duration):
|
113 |
+
self.new_duration = duration
|
114 |
+
|
115 |
def get_ip(request: gr.Request):
|
116 |
if request:
|
117 |
if "cf-connecting-ip" in request.headers:
|
|
|
129 |
state = AudioStateIG(row)
|
130 |
return state, audio_path
|
131 |
|
132 |
+
def random_cut_length(audio_data, max_length, sample_rate):
|
133 |
+
if max_length > 125:
|
134 |
+
options = [125, 55, 25]
|
135 |
+
|
136 |
+
elif max_length > 55:
|
137 |
+
options = [55, 25]
|
138 |
|
139 |
+
elif max_length > 25:
|
140 |
+
options = [25]
|
141 |
|
142 |
+
else:
|
143 |
+
return audio_data, max_length
|
144 |
|
145 |
+
|
146 |
+
def generate_songs(state, randomized = True):
|
147 |
+
idx= pick_and_remove_one()
|
148 |
+
state, audio = get_song(idx)
|
149 |
+
if randomized:
|
150 |
+
audio_data, sample_rate = librosa.load(audio, sr=None)
|
151 |
+
audio_cut, new_length = random_cut_length(audio_data, state.row.duration, sample_rate)
|
152 |
+
state.update_duration(new_length)
|
153 |
+
return state, (sample_rate, audio_cut), "Vote to Reveal Label",
|
154 |
+
|
155 |
+
return state, audio, "Vote to Reveal Label",
|
156 |
+
|
157 |
def fake_last_response(
|
158 |
state, request: gr.Request
|
159 |
):
|