Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
-
import math
|
3 |
-
import time
|
4 |
import numpy as np
|
5 |
-
import csv
|
6 |
from pydub import AudioSegment
|
7 |
from pydub.silence import detect_nonsilent
|
8 |
import io
|
|
|
9 |
|
10 |
|
11 |
def numpy_to_audiosegment(audio_array, sampling_rate):
|
@@ -58,6 +56,30 @@ def format_time(milliseconds):
|
|
58 |
return f"{minutes:02}:{secs:02}"
|
59 |
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def stream(audio, chunk_length_s):
|
62 |
sampling_rate, array = audio
|
63 |
audio_segment = numpy_to_audiosegment(array, sampling_rate)
|
@@ -93,6 +115,7 @@ def stream(audio, chunk_length_s):
|
|
93 |
print(f"{start} to {end}")
|
94 |
|
95 |
|
|
|
96 |
with gr.Blocks() as demo:
|
97 |
with gr.Row():
|
98 |
with gr.Column():
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import numpy as np
|
|
|
3 |
from pydub import AudioSegment
|
4 |
from pydub.silence import detect_nonsilent
|
5 |
import io
|
6 |
+
import csv
|
7 |
|
8 |
|
9 |
def numpy_to_audiosegment(audio_array, sampling_rate):
|
|
|
56 |
return f"{minutes:02}:{secs:02}"
|
57 |
|
58 |
|
59 |
+
def numpy_to_mp3(audio_array, sampling_rate):
|
60 |
+
"""Converts a numpy audio array to MP3 format."""
|
61 |
+
# Normalize audio_array if it's floating-point
|
62 |
+
if np.issubdtype(audio_array.dtype, np.floating):
|
63 |
+
max_val = np.max(np.abs(audio_array))
|
64 |
+
audio_array = (audio_array / max_val) * 32767 # Normalize to 16-bit range
|
65 |
+
audio_array = audio_array.astype(np.int16)
|
66 |
+
|
67 |
+
audio_segment = AudioSegment(
|
68 |
+
audio_array.tobytes(),
|
69 |
+
frame_rate=sampling_rate,
|
70 |
+
sample_width=audio_array.dtype.itemsize,
|
71 |
+
channels=1
|
72 |
+
)
|
73 |
+
|
74 |
+
# Export the audio segment to MP3 bytes
|
75 |
+
mp3_io = io.BytesIO()
|
76 |
+
audio_segment.export(mp3_io, format="mp3", bitrate="320k")
|
77 |
+
mp3_bytes = mp3_io.getvalue()
|
78 |
+
mp3_io.close()
|
79 |
+
|
80 |
+
return mp3_bytes
|
81 |
+
|
82 |
+
|
83 |
def stream(audio, chunk_length_s):
|
84 |
sampling_rate, array = audio
|
85 |
audio_segment = numpy_to_audiosegment(array, sampling_rate)
|
|
|
115 |
print(f"{start} to {end}")
|
116 |
|
117 |
|
118 |
+
# Gradio Interface
|
119 |
with gr.Blocks() as demo:
|
120 |
with gr.Row():
|
121 |
with gr.Column():
|