Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import math
|
| 3 |
import time
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from pydub import AudioSegment
|
| 6 |
import io
|
| 7 |
|
|
@@ -36,25 +37,28 @@ def stream(audio, chunk_length_s):
|
|
| 36 |
start_time = time.time()
|
| 37 |
sampling_rate, array = audio
|
| 38 |
chunk_length = int(chunk_length_s * sampling_rate)
|
| 39 |
-
time_length = chunk_length_s / 2 #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
audio_length = len(array)
|
| 41 |
num_batches = math.ceil(audio_length / chunk_length)
|
| 42 |
|
| 43 |
-
# Initialize a list to store timestamps
|
| 44 |
timestamps = []
|
| 45 |
|
| 46 |
for idx in range(num_batches):
|
| 47 |
-
time.sleep(time_length)
|
| 48 |
start_pos = idx * chunk_length
|
| 49 |
end_pos = min((idx + 1) * chunk_length, audio_length)
|
| 50 |
chunk_start_time = start_pos / sampling_rate
|
| 51 |
chunk_end_time = end_pos / sampling_rate
|
| 52 |
|
| 53 |
-
# Save timestamps for
|
| 54 |
-
|
| 55 |
-
timestamps.append((chunk_start_time, chunk_end_time))
|
| 56 |
|
| 57 |
-
chunk = array[start_pos:
|
| 58 |
chunk_mp3 = numpy_to_mp3(chunk, sampling_rate=sampling_rate)
|
| 59 |
|
| 60 |
if idx == 0:
|
|
@@ -63,7 +67,13 @@ def stream(audio, chunk_length_s):
|
|
| 63 |
|
| 64 |
yield chunk_mp3, first_time, run_time
|
| 65 |
|
| 66 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
print("Timestamps for the first 30 seconds:")
|
| 68 |
for start, end in timestamps:
|
| 69 |
print(f"Start: {start:.2f}s, End: {end:.2f}s")
|
|
@@ -82,4 +92,4 @@ with gr.Blocks() as demo:
|
|
| 82 |
|
| 83 |
run_button.click(fn=stream, inputs=[audio_in, chunk_length], outputs=[audio_out, first_time, run_time])
|
| 84 |
|
| 85 |
-
demo.launch()
|
|
|
|
| 2 |
import math
|
| 3 |
import time
|
| 4 |
import numpy as np
|
| 5 |
+
import csv
|
| 6 |
from pydub import AudioSegment
|
| 7 |
import io
|
| 8 |
|
|
|
|
| 37 |
start_time = time.time()
|
| 38 |
sampling_rate, array = audio
|
| 39 |
chunk_length = int(chunk_length_s * sampling_rate)
|
| 40 |
+
time_length = chunk_length_s / 2 # Always stream outputs faster than it takes to process
|
| 41 |
+
max_duration = 30 # Only process the first 30 seconds
|
| 42 |
+
max_samples = int(max_duration * sampling_rate)
|
| 43 |
+
|
| 44 |
+
# Truncate audio array to 30 seconds
|
| 45 |
+
array = array[:max_samples]
|
| 46 |
audio_length = len(array)
|
| 47 |
num_batches = math.ceil(audio_length / chunk_length)
|
| 48 |
|
| 49 |
+
# Initialize a list to store timestamps
|
| 50 |
timestamps = []
|
| 51 |
|
| 52 |
for idx in range(num_batches):
|
|
|
|
| 53 |
start_pos = idx * chunk_length
|
| 54 |
end_pos = min((idx + 1) * chunk_length, audio_length)
|
| 55 |
chunk_start_time = start_pos / sampling_rate
|
| 56 |
chunk_end_time = end_pos / sampling_rate
|
| 57 |
|
| 58 |
+
# Save timestamps for the current chunk
|
| 59 |
+
timestamps.append((chunk_start_time, chunk_end_time))
|
|
|
|
| 60 |
|
| 61 |
+
chunk = array[start_pos:end_pos]
|
| 62 |
chunk_mp3 = numpy_to_mp3(chunk, sampling_rate=sampling_rate)
|
| 63 |
|
| 64 |
if idx == 0:
|
|
|
|
| 67 |
|
| 68 |
yield chunk_mp3, first_time, run_time
|
| 69 |
|
| 70 |
+
# Save timestamps to a CSV file
|
| 71 |
+
with open("timestamps_30sec.csv", mode="w", newline="") as file:
|
| 72 |
+
writer = csv.writer(file)
|
| 73 |
+
writer.writerow(["Start Time (s)", "End Time (s)"])
|
| 74 |
+
writer.writerows(timestamps)
|
| 75 |
+
|
| 76 |
+
print(f"Timestamps saved to 'timestamps_30sec.csv'")
|
| 77 |
print("Timestamps for the first 30 seconds:")
|
| 78 |
for start, end in timestamps:
|
| 79 |
print(f"Start: {start:.2f}s, End: {end:.2f}s")
|
|
|
|
| 92 |
|
| 93 |
run_button.click(fn=stream, inputs=[audio_in, chunk_length], outputs=[audio_out, first_time, run_time])
|
| 94 |
|
| 95 |
+
demo.launch()
|