Spaces:
Sleeping
Sleeping
Add librosa to requirements.txt and include ffmpeg in packages.txt
Browse files- app.py +9 -48
- packages.txt +0 -0
app.py
CHANGED
|
@@ -2,36 +2,8 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import librosa
|
| 4 |
import soundfile as sf
|
| 5 |
-
import json
|
| 6 |
-
import websocket
|
| 7 |
-
import threading
|
| 8 |
-
import time
|
| 9 |
from io import BytesIO
|
| 10 |
|
| 11 |
-
# Global variables for WebSocket communication
|
| 12 |
-
ws = None
|
| 13 |
-
clients = []
|
| 14 |
-
|
| 15 |
-
# WebSocket server to send visualization data to frontend
|
| 16 |
-
def start_websocket_server():
|
| 17 |
-
def on_open(ws):
|
| 18 |
-
print("WebSocket server opened")
|
| 19 |
-
clients.append(ws)
|
| 20 |
-
|
| 21 |
-
def on_close(ws):
|
| 22 |
-
print("WebSocket server closed")
|
| 23 |
-
clients.remove(ws)
|
| 24 |
-
|
| 25 |
-
def on_message(ws, message):
|
| 26 |
-
print(f"Received: {message}")
|
| 27 |
-
|
| 28 |
-
global ws
|
| 29 |
-
ws = websocket.WebSocketApp("ws://localhost:8765",
|
| 30 |
-
on_open=on_open,
|
| 31 |
-
on_message=on_message,
|
| 32 |
-
on_close=on_close)
|
| 33 |
-
threading.Thread(target=ws.run_forever, daemon=True).start()
|
| 34 |
-
|
| 35 |
# Process audio file or recording
|
| 36 |
def process_audio(audio_input, sample_rate=44100):
|
| 37 |
# Handle Gradio audio input (tuple of (sample_rate, numpy_array))
|
|
@@ -56,34 +28,23 @@ def process_audio(audio_input, sample_rate=44100):
|
|
| 56 |
"volume": float(np.mean(np.abs(audio_data)) * 100)
|
| 57 |
}
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
client.send(json.dumps(vis_data))
|
| 64 |
-
except:
|
| 65 |
-
pass
|
| 66 |
|
| 67 |
-
return vis_data,
|
| 68 |
|
| 69 |
# Gradio interface function
|
| 70 |
def audio_visualizer(audio_file, audio_record):
|
| 71 |
if audio_file:
|
| 72 |
-
vis_data,
|
| 73 |
elif audio_record:
|
| 74 |
-
vis_data,
|
| 75 |
else:
|
| 76 |
-
return "Please upload an audio file or record audio."
|
| 77 |
-
|
| 78 |
-
# Save audio to a temporary file for playback
|
| 79 |
-
temp_audio = BytesIO()
|
| 80 |
-
sf.write(temp_audio, audio_data, sr, format='wav')
|
| 81 |
-
temp_audio.seek(0)
|
| 82 |
-
|
| 83 |
-
return vis_data, temp_audio
|
| 84 |
|
| 85 |
-
|
| 86 |
-
start_websocket_server()
|
| 87 |
|
| 88 |
# Gradio interface
|
| 89 |
with gr.Blocks() as demo:
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import librosa
|
| 4 |
import soundfile as sf
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Process audio file or recording
|
| 8 |
def process_audio(audio_input, sample_rate=44100):
|
| 9 |
# Handle Gradio audio input (tuple of (sample_rate, numpy_array))
|
|
|
|
| 28 |
"volume": float(np.mean(np.abs(audio_data)) * 100)
|
| 29 |
}
|
| 30 |
|
| 31 |
+
# Save audio to a temporary file for playback
|
| 32 |
+
temp_audio = BytesIO()
|
| 33 |
+
sf.write(temp_audio, audio_data, sr, format='wav')
|
| 34 |
+
temp_audio.seek(0)
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
return vis_data, temp_audio
|
| 37 |
|
| 38 |
# Gradio interface function
|
| 39 |
def audio_visualizer(audio_file, audio_record):
|
| 40 |
if audio_file:
|
| 41 |
+
vis_data, audio_output = process_audio(audio_file)
|
| 42 |
elif audio_record:
|
| 43 |
+
vis_data, audio_output = process_audio(audio_record)
|
| 44 |
else:
|
| 45 |
+
return "Please upload an audio file or record audio.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
return vis_data, audio_output
|
|
|
|
| 48 |
|
| 49 |
# Gradio interface
|
| 50 |
with gr.Blocks() as demo:
|
packages.txt
ADDED
|
File without changes
|