Spaces:
Sleeping
Sleeping
Commit
·
1b99dff
1
Parent(s):
c65250a
Use stable Gradio 3.50.2 and ultra-simple interface
Browse files- README.md +1 -1
- app.py +37 -75
- requirements.txt +3 -4
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🎵
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.50.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
app.py
CHANGED
|
@@ -3,39 +3,35 @@ import torch
|
|
| 3 |
import numpy as np
|
| 4 |
from transformers import AutoModel, Wav2Vec2Processor
|
| 5 |
import librosa
|
| 6 |
-
import matplotlib.pyplot as plt
|
| 7 |
|
| 8 |
# Configurar o modelo
|
| 9 |
MODEL_NAME = "marcosremar2/wavlm-large-deploy"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
return None, None
|
| 22 |
-
|
| 23 |
-
processor, model = load_model()
|
| 24 |
|
| 25 |
def process_audio(audio_file):
|
| 26 |
"""Processa o arquivo de áudio e extrai features"""
|
| 27 |
if audio_file is None:
|
| 28 |
-
return "❌ Por favor, carregue um arquivo de áudio."
|
| 29 |
|
| 30 |
if processor is None or model is None:
|
| 31 |
-
return "❌ Erro: Modelo não foi carregado corretamente.
|
| 32 |
|
| 33 |
try:
|
| 34 |
# Carregar áudio
|
| 35 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 36 |
|
| 37 |
-
# Limitar duração
|
| 38 |
-
max_duration =
|
| 39 |
if len(audio) > max_duration * sr:
|
| 40 |
audio = audio[:max_duration * sr]
|
| 41 |
|
|
@@ -48,72 +44,38 @@ def process_audio(audio_file):
|
|
| 48 |
|
| 49 |
# Informações sobre o áudio
|
| 50 |
duration = len(audio) / sr
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
-
|
| 59 |
-
-
|
| 60 |
-
-
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
"""
|
| 64 |
|
| 65 |
-
|
| 66 |
-
features = hidden_states.squeeze(0).cpu().numpy()
|
| 67 |
-
|
| 68 |
-
# Plot 1: Waveform
|
| 69 |
-
fig1, ax1 = plt.subplots(figsize=(10, 4))
|
| 70 |
-
time_axis = np.linspace(0, duration, len(audio))
|
| 71 |
-
ax1.plot(time_axis, audio, color='blue', linewidth=0.5)
|
| 72 |
-
ax1.set_title('Forma de Onda do Áudio', fontsize=14)
|
| 73 |
-
ax1.set_xlabel('Tempo (s)')
|
| 74 |
-
ax1.set_ylabel('Amplitude')
|
| 75 |
-
ax1.grid(True, alpha=0.3)
|
| 76 |
-
plt.tight_layout()
|
| 77 |
-
|
| 78 |
-
# Plot 2: Features heatmap (primeiras 32 features para visualização mais rápida)
|
| 79 |
-
fig2, ax2 = plt.subplots(figsize=(10, 6))
|
| 80 |
-
features_subset = features[:min(100, features.shape[0]), :32].T # Limitar frames também
|
| 81 |
-
im = ax2.imshow(features_subset, aspect='auto', cmap='viridis')
|
| 82 |
-
ax2.set_title('Features WavLM (primeiras 32 dimensões)', fontsize=14)
|
| 83 |
-
ax2.set_xlabel('Frame Temporal')
|
| 84 |
-
ax2.set_ylabel('Dimensão da Feature')
|
| 85 |
-
plt.colorbar(im, ax=ax2, label='Valor da Feature')
|
| 86 |
-
plt.tight_layout()
|
| 87 |
-
|
| 88 |
-
return audio_info, fig1, fig2
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
-
return f"❌
|
| 92 |
|
| 93 |
-
# Interface Gradio simples
|
| 94 |
-
|
| 95 |
fn=process_audio,
|
| 96 |
-
inputs=gr.Audio(type="filepath"
|
| 97 |
-
outputs=
|
| 98 |
-
gr.Markdown(label="Informações"),
|
| 99 |
-
gr.Plot(label="Forma de Onda"),
|
| 100 |
-
gr.Plot(label="Features WavLM")
|
| 101 |
-
],
|
| 102 |
title="🎵 WavLM-Large Demo",
|
| 103 |
-
description=""
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
**Como usar**:
|
| 107 |
-
1. Carregue um arquivo de áudio (WAV, MP3, etc.)
|
| 108 |
-
2. Veja as informações extraídas e visualizações das features
|
| 109 |
-
|
| 110 |
-
**Limitações**:
|
| 111 |
-
- Máximo 30 segundos de áudio
|
| 112 |
-
- Arquivos grandes podem demorar para processar
|
| 113 |
-
""",
|
| 114 |
-
examples=[],
|
| 115 |
-
cache_examples=False
|
| 116 |
)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
-
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from transformers import AutoModel, Wav2Vec2Processor
|
| 5 |
import librosa
|
|
|
|
| 6 |
|
| 7 |
# Configurar o modelo
|
| 8 |
MODEL_NAME = "marcosremar2/wavlm-large-deploy"
|
| 9 |
|
| 10 |
+
print("Carregando modelo...")
|
| 11 |
+
try:
|
| 12 |
+
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
| 13 |
+
model = AutoModel.from_pretrained(MODEL_NAME)
|
| 14 |
+
model.eval()
|
| 15 |
+
print("Modelo carregado com sucesso!")
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Erro ao carregar modelo: {e}")
|
| 18 |
+
processor = None
|
| 19 |
+
model = None
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def process_audio(audio_file):
|
| 22 |
"""Processa o arquivo de áudio e extrai features"""
|
| 23 |
if audio_file is None:
|
| 24 |
+
return "❌ Por favor, carregue um arquivo de áudio."
|
| 25 |
|
| 26 |
if processor is None or model is None:
|
| 27 |
+
return "❌ Erro: Modelo não foi carregado corretamente."
|
| 28 |
|
| 29 |
try:
|
| 30 |
# Carregar áudio
|
| 31 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 32 |
|
| 33 |
+
# Limitar duração
|
| 34 |
+
max_duration = 20 # segundos
|
| 35 |
if len(audio) > max_duration * sr:
|
| 36 |
audio = audio[:max_duration * sr]
|
| 37 |
|
|
|
|
| 44 |
|
| 45 |
# Informações sobre o áudio
|
| 46 |
duration = len(audio) / sr
|
| 47 |
+
|
| 48 |
+
result = f"""
|
| 49 |
+
✅ **Processamento Concluído!**
|
| 50 |
+
|
| 51 |
+
📊 **Informações do Áudio:**
|
| 52 |
+
- Duração: {duration:.2f} segundos
|
| 53 |
+
- Taxa de amostragem: {sr} Hz
|
| 54 |
+
- Amostras: {len(audio)}
|
| 55 |
|
| 56 |
+
🧠 **Saída do Modelo WavLM-Large:**
|
| 57 |
+
- Features shape: {hidden_states.shape}
|
| 58 |
+
- Frames: {hidden_states.shape[1]}
|
| 59 |
+
- Dimensões: {hidden_states.shape[2]}
|
| 60 |
+
- Modelo: {MODEL_NAME}
|
| 61 |
|
| 62 |
+
🎯 O modelo extraiu {hidden_states.shape[1]} frames temporais com {hidden_states.shape[2]} características cada um.
|
| 63 |
"""
|
| 64 |
|
| 65 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
+
return f"❌ Erro ao processar: {str(e)}"
|
| 69 |
|
| 70 |
+
# Interface Gradio ultra-simples
|
| 71 |
+
iface = gr.Interface(
|
| 72 |
fn=process_audio,
|
| 73 |
+
inputs=gr.Audio(type="filepath"),
|
| 74 |
+
outputs=gr.Textbox(label="Resultado"),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
title="🎵 WavLM-Large Demo",
|
| 76 |
+
description="Carregue um arquivo de áudio para extrair features com WavLM-Large",
|
| 77 |
+
examples=None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
iface.launch()
|
requirements.txt
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
-
gradio==
|
| 2 |
torch>=2.0.0
|
| 3 |
-
transformers>=4.
|
| 4 |
librosa>=0.10.0
|
| 5 |
-
|
| 6 |
-
numpy>=1.24.0
|
| 7 |
soundfile>=0.12.0
|
|
|
|
| 1 |
+
gradio==3.50.2
|
| 2 |
torch>=2.0.0
|
| 3 |
+
transformers>=4.30.0
|
| 4 |
librosa>=0.10.0
|
| 5 |
+
numpy>=1.20.0
|
|
|
|
| 6 |
soundfile>=0.12.0
|