Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
| 3 |
-
import os
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
# Usamos 'tiny' para velocidad en CPU gratis.
|
| 8 |
-
MODEL_NAME = "tiny"
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
def analizar_audio_evp(audio_path):
|
| 20 |
-
|
| 21 |
-
Analiza el audio buscando patrones lingüísticos en el ruido.
|
| 22 |
-
"""
|
| 23 |
-
if model is None:
|
| 24 |
-
return "❌ Error: El modelo no se cargó correctamente en el servidor."
|
| 25 |
|
|
|
|
| 26 |
if audio_path is None:
|
| 27 |
-
return "⚠️ No se detectó audio.
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
# --- Interfaz
|
| 55 |
-
with gr.Blocks(
|
| 56 |
gr.Markdown("""
|
| 57 |
-
#
|
| 58 |
## Sube un MP3 de "silencio" o graba tu habitación.
|
| 59 |
-
*
|
| 60 |
""")
|
| 61 |
|
| 62 |
with gr.Row():
|
|
@@ -64,16 +87,15 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple")) as demo:
|
|
| 64 |
audio_input = gr.Audio(
|
| 65 |
label="🎙️ Fuente de Audio",
|
| 66 |
type="filepath",
|
| 67 |
-
sources=["upload", "microphone"]
|
| 68 |
-
format="mp3"
|
| 69 |
)
|
| 70 |
btn_analizar = gr.Button("🔮 Analizar Ruido", variant="primary")
|
| 71 |
|
| 72 |
with gr.Column():
|
| 73 |
output_text = gr.Textbox(
|
| 74 |
label="📜 Resultado del Análisis",
|
| 75 |
-
lines=
|
| 76 |
-
max_lines=
|
| 77 |
)
|
| 78 |
|
| 79 |
btn_analizar.click(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import whisper
|
|
|
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
+
MODEL_NAME = "tiny"
|
|
|
|
|
|
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
_model_cache = None
|
| 10 |
|
| 11 |
+
def cargar_modelo():
|
| 12 |
+
global _model_cache
|
| 13 |
+
if _model_cache is None:
|
| 14 |
+
print(f"🌀 Cargando modelo Whisper ({MODEL_NAME}) en {device}...")
|
| 15 |
+
start = time.time()
|
| 16 |
+
_model_cache = whisper.load_model(MODEL_NAME, device=device)
|
| 17 |
+
end = time.time()
|
| 18 |
+
print(f"✅ Modelo cargado en {end - start:.2f} segundos")
|
| 19 |
+
return _model_cache
|
| 20 |
|
| 21 |
+
def analizar_audio_evp(audio_path, progress=gr.Progress()):
|
| 22 |
+
inicio_total = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# 1. Verificar que el archivo existe
|
| 25 |
if audio_path is None:
|
| 26 |
+
return "⚠️ No se detectó audio."
|
| 27 |
+
|
| 28 |
+
if not os.path.exists(audio_path):
|
| 29 |
+
return f"❌ ERROR: El archivo no existe en {audio_path}"
|
| 30 |
+
|
| 31 |
+
tamaño = os.path.getsize(audio_path)
|
| 32 |
+
print(f"📁 Archivo recibido: {audio_path} ({tamaño} bytes)")
|
| 33 |
+
|
| 34 |
+
# 2. Cargar modelo (con tiempo real)
|
| 35 |
+
progress(0.1, desc="Cargando modelo de IA...")
|
| 36 |
+
inicio_carga = time.time()
|
| 37 |
+
model = cargar_modelo()
|
| 38 |
+
tiempo_carga = time.time() - inicio_carga
|
| 39 |
+
print(f"⏱️ Tiempo de carga del modelo: {tiempo_carga:.2f}s")
|
| 40 |
+
|
| 41 |
+
if model is None:
|
| 42 |
+
return "❌ Error: El modelo no se cargó correctamente."
|
| 43 |
|
| 44 |
+
# 3. Transcribir (con tiempo real)
|
| 45 |
+
progress(0.4, desc="Procesando audio...")
|
| 46 |
+
inicio_transcripcion = time.time()
|
| 47 |
+
|
| 48 |
+
options = {
|
| 49 |
+
"language": "es",
|
| 50 |
+
"fp16": False if device == "cpu" else True,
|
| 51 |
+
"temperature": 0.8,
|
| 52 |
+
"condition_on_previous_text": False,
|
| 53 |
+
"verbose": False,
|
| 54 |
+
"task": "transcribe"
|
| 55 |
+
}
|
| 56 |
|
| 57 |
+
result = model.transcribe(audio_path, **options)
|
| 58 |
+
tiempo_transcripcion = time.time() - inicio_transcripcion
|
| 59 |
+
print(f"⏱️ Tiempo de transcripción: {tiempo_transcripcion:.2f}s")
|
| 60 |
+
|
| 61 |
+
texto = result["text"].strip()
|
| 62 |
+
tiempo_total = time.time() - inicio_total
|
| 63 |
+
|
| 64 |
+
progress(1.0, desc="Análisis completado")
|
| 65 |
+
|
| 66 |
+
# 4. Retorno CON TIEMPOS REALES (para que veas si es fake)
|
| 67 |
+
respuesta = f"⏱️ **TIEMPO TOTAL: {tiempo_total:.2f} segundos**\n\n"
|
| 68 |
+
|
| 69 |
+
if texto:
|
| 70 |
+
respuesta += f"👻 **PATRÓN DETECTADO:**\n\n{texto}"
|
| 71 |
+
else:
|
| 72 |
+
respuesta += "💤 **SIN PATRONES:**\n\nLa IA no encontró estructuras lingüísticas en este audio.\n\n"
|
| 73 |
+
respuesta += f"📊 **DEBUG:**\n- Tamaño archivo: {tamaño} bytes\n- Tiempo transcripción: {tiempo_transcripcion:.2f}s\n- Device: {device}"
|
| 74 |
+
|
| 75 |
+
return respuesta
|
| 76 |
|
| 77 |
+
# --- Interfaz ---
|
| 78 |
+
with gr.Blocks() as demo:
|
| 79 |
gr.Markdown("""
|
| 80 |
+
# 👻 Detector EVP - Audio a Texto
|
| 81 |
## Sube un MP3 de "silencio" o graba tu habitación.
|
| 82 |
+
*Si el análisis es instantáneo (<5s), algo está mal.*
|
| 83 |
""")
|
| 84 |
|
| 85 |
with gr.Row():
|
|
|
|
| 87 |
audio_input = gr.Audio(
|
| 88 |
label="🎙️ Fuente de Audio",
|
| 89 |
type="filepath",
|
| 90 |
+
sources=["upload", "microphone"]
|
|
|
|
| 91 |
)
|
| 92 |
btn_analizar = gr.Button("🔮 Analizar Ruido", variant="primary")
|
| 93 |
|
| 94 |
with gr.Column():
|
| 95 |
output_text = gr.Textbox(
|
| 96 |
label="📜 Resultado del Análisis",
|
| 97 |
+
lines=8,
|
| 98 |
+
max_lines=15
|
| 99 |
)
|
| 100 |
|
| 101 |
btn_analizar.click(
|