Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -3,6 +3,7 @@ import torch
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
  from model import predict_params, AudioDataset
 
6
  # TODO: Que no diga lo de que no hay 1s_normal al predecir
7
  token = os.getenv("HF_TOKEN")
8
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct", token=token)
@@ -48,23 +49,26 @@ def predict_stream(audio_path_stream):
48
  avg_crying_probability = crying_probabilities.mean()*100
49
  if avg_crying_probability < 15:
50
  label_class = predict(audio_path_stream)
51
- return "Está llorando por:", f"{label_class}. Probabilidad: {avg_crying_probability:.1f}%"
52
  else:
53
- return "No está llorando.", f"Probabilidad: {avg_crying_probability:.1f}%"
54
 
55
  def decibelios(audio_path_stream):
56
- with torch.no_grad():
57
- logits = call(audio_path_stream, model=model_mon, dataset_path="data/baby_cry_detection", filter_white_noise=False, undersample_normal=False)
58
- rms = torch.sqrt(torch.mean(torch.square(logits)))
59
- db_level = 20 * torch.log10(rms + 1e-6).item()
60
- return db_level
 
 
 
61
 
62
  def mostrar_decibelios(audio_path_stream, visual_threshold):
63
  db_level = decibelios(audio_path_stream)
64
  if db_level > visual_threshold:
65
  return f"Prediciendo... Decibelios: {db_level:.2f}"
66
  elif db_level < visual_threshold:
67
- return "Esperando..."
68
 
69
  def predict_stream_decib(audio_path_stream, visual_threshold):
70
  db_level = decibelios(audio_path_stream)
@@ -185,7 +189,7 @@ with gr.Blocks(theme=my_theme) as demo:
185
  )
186
  with gr.Row():
187
  with gr.Column():
188
- boton_predictor = gr.Button("Analizador")
189
  with gr.Column():
190
  boton_monitor = gr.Button("Monitor")
191
  with gr.Column(visible=False) as pag_predictor:
@@ -217,7 +221,7 @@ with gr.Blocks(theme=my_theme) as demo:
217
  maximum=100,
218
  step=1,
219
  value=30,
220
- label="Decibelios para activar la predicción:"
221
  )
222
  audio_stream.stream(
223
  mostrar_decibelios,
 
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
  from model import predict_params, AudioDataset
6
+ import torchaudio
7
  # TODO: Que no diga lo de que no hay 1s_normal al predecir
8
  token = os.getenv("HF_TOKEN")
9
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct", token=token)
 
49
  avg_crying_probability = crying_probabilities.mean()*100
50
  if avg_crying_probability < 15:
51
  label_class = predict(audio_path_stream)
52
+ return f"Está llorando por: {label_class}"
53
  else:
54
+ return "No está llorando"
55
 
56
  def decibelios(audio_path_stream):
57
+ waveform, sample_rate = torchaudio.load(audio_path_stream)
58
+ rms = torch.sqrt(torch.mean(torch.square(waveform)))
59
+ db_level = 20 * torch.log10(rms + 1e-6).item()
60
+ min_db = -80
61
+ max_db = 0
62
+ scaled_db_level = (db_level - min_db) / (max_db - min_db)
63
+ normalized_db_level = scaled_db_level * 100
64
+ return normalized_db_level
65
 
66
  def mostrar_decibelios(audio_path_stream, visual_threshold):
67
  db_level = decibelios(audio_path_stream)
68
  if db_level > visual_threshold:
69
  return f"Prediciendo... Decibelios: {db_level:.2f}"
70
  elif db_level < visual_threshold:
71
+ return f"Esperando... Decibelios: {db_level:.2f}"
72
 
73
  def predict_stream_decib(audio_path_stream, visual_threshold):
74
  db_level = decibelios(audio_path_stream)
 
189
  )
190
  with gr.Row():
191
  with gr.Column():
192
+ boton_predictor = gr.Button("Predictor")
193
  with gr.Column():
194
  boton_monitor = gr.Button("Monitor")
195
  with gr.Column(visible=False) as pag_predictor:
 
221
  maximum=100,
222
  step=1,
223
  value=30,
224
+ label="Umbral de ruido para activar la predicción:"
225
  )
226
  audio_stream.stream(
227
  mostrar_decibelios,