Marcos12886 commited on
Commit
fe16cc3
1 Parent(s): 81672a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -33
app.py CHANGED
@@ -1,29 +1,29 @@
1
- import os
2
- import torch
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
- from model import model_params, AudioDataset
 
 
 
6
 
7
  token = os.getenv("HF_TOKEN")
8
- dataset_path = f"A-POR-LOS-8000/cry-detector" # PARA MONITOR
9
- # dataset_path = f"A-POR-LOS-8000/data-mixed" # PARA CLASIFICADOR
10
- model, _, _, id2label = model_params(dataset_path)
11
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")# Usar a GPU o CPU
12
- model.to(device)# Usar a GPU o CPU
 
 
13
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct", token=token)
14
  # client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407", token=token)
15
 
16
- def predict(audio_path):
17
- audio_dataset = AudioDataset(dataset_path, {})
18
- inputs = audio_dataset.preprocess_audio(audio_path)
19
- inputs = {"input_values": inputs.to(device).unsqueeze(0)}
20
- with torch.no_grad():
21
- outputs = model(**inputs)
22
- predicted_class_ids = outputs.logits.argmax(-1)
23
- label = id2label[predicted_class_ids.item()]
24
- return label
25
-
26
- def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
27
  messages = [{"role": "system", "content": system_message}]
28
  for val in history:
29
  if val[0]:
@@ -32,14 +32,17 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
32
  messages.append({"role": "assistant", "content": val[1]})
33
  messages.append({"role": "user", "content": message})
34
  response = ""
35
- for message in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p): # Creo que lo importante para el modelo
 
 
 
 
 
 
36
  token = message.choices[0].delta.content
37
  response += token
38
  yield response
39
 
40
- def cambiar_pestaña():
41
- return gr.update(visible=False), gr.update(visible=True)
42
-
43
  my_theme = gr.themes.Soft(
44
  primary_hue="emerald",
45
  secondary_hue="green",
@@ -57,6 +60,24 @@ my_theme = gr.themes.Soft(
57
  shadow_spread='*button_shadow_active'
58
  )
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  with gr.Blocks(theme=my_theme) as demo:
61
  with gr.Column(visible=True, elem_id="pantalla-inicial") as pantalla_inicial:
62
  gr.HTML(
@@ -143,13 +164,14 @@ with gr.Blocks(theme=my_theme) as demo:
143
  gr.Markdown("<h2>Predictor</h2>")
144
  audio_input = gr.Audio(
145
  min_length=1.0,
 
146
  format="wav",
147
- label="Baby recorder",
148
- type="filepath", # Para no usar numpy y preprocesar siempre igual
149
- )
150
  classify_btn = gr.Button("¿Por qué llora?")
151
  classification_output = gr.Textbox(label="Tu bebé llora por:")
152
- classify_btn.click(predict, inputs=audio_input, outputs=classification_output)
153
  with gr.Column():
154
  gr.Markdown("<h2>Assistant</h2>")
155
  system_message = "You are a Chatbot specialized in baby health and care."
@@ -157,7 +179,7 @@ with gr.Blocks(theme=my_theme) as demo:
157
  temperature = 0.7
158
  top_p = 0.95
159
  chatbot = gr.ChatInterface(
160
- respond, # TODO: Cambiar para que argumentos estén aquí metidos
161
  additional_inputs=[
162
  gr.State(value=system_message),
163
  gr.State(value=max_tokens),
@@ -166,11 +188,13 @@ with gr.Blocks(theme=my_theme) as demo:
166
  ],
167
  )
168
  gr.Markdown("Este chatbot no sustituye a un profesional de la salud. Ante cualquier preocupación o duda, consulta con tu pediatra.")
169
- boton_volver_inicio_1 = gr.Button("Volver a la pantalla inicial").click(cambiar_pestaña, outputs=[pagina_1, pantalla_inicial])
 
170
  with gr.Column(visible=False) as pagina_2:
171
  gr.Markdown("<h2>Monitor</h2>")
172
  gr.Markdown("Contenido de la Página 2")
173
- boton_volver_inicio_2 = gr.Button("Volver a la pantalla inicial").click(cambiar_pestaña, outputs=[pagina_2, pantalla_inicial])
174
- boton_pagina_1.click(cambiar_pestaña, outputs=[pantalla_inicial, pagina_1])
175
- boton_pagina_2.click(cambiar_pestaña, outputs=[pantalla_inicial, pagina_2])
176
- demo.launch()
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import os
4
+ from transformers import pipeline
5
+ import numpy as np
6
+ from model import SAMPLING_RATE, FEATURE_EXTRACTOR
7
 
8
  token = os.getenv("HF_TOKEN")
9
+ # modelo = "mixed-data"
10
+ modelo = "cry-detector"
11
+ pipe = pipeline(
12
+ "audio-classification",
13
+ model=f"A-POR-LOS-8000/distilhubert-finetuned-{modelo}",
14
+ use_auth_token=token
15
+ )
16
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct", token=token)
17
  # client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407", token=token)
18
 
19
+ def respond(
20
+ message,
21
+ history: list[tuple[str, str]],
22
+ system_message,
23
+ max_tokens,
24
+ temperature,
25
+ top_p,
26
+ ):
 
 
 
27
  messages = [{"role": "system", "content": system_message}]
28
  for val in history:
29
  if val[0]:
 
32
  messages.append({"role": "assistant", "content": val[1]})
33
  messages.append({"role": "user", "content": message})
34
  response = ""
35
+ for message in client.chat_completion(
36
+ messages,
37
+ max_tokens=max_tokens,
38
+ stream=True,
39
+ temperature=temperature,
40
+ top_p=top_p,
41
+ ):
42
  token = message.choices[0].delta.content
43
  response += token
44
  yield response
45
 
 
 
 
46
  my_theme = gr.themes.Soft(
47
  primary_hue="emerald",
48
  secondary_hue="green",
 
60
  shadow_spread='*button_shadow_active'
61
  )
62
 
63
+ def mostrar_pagina_1():
64
+ return gr.update(visible=False), gr.update(visible=True)
65
+
66
+ def mostrar_pagina_2():
67
+ return gr.update(visible=False), gr.update(visible=True)
68
+
69
+ def redirigir_a_pantalla_inicial():
70
+ return gr.update(visible=True), gr.update(visible=False)
71
+
72
+ def transcribe(audio):
73
+ _, y = audio
74
+ y = y.astype(np.float32) # con torch.float32 da error
75
+ y /= np.max(np.abs(y))
76
+ results = pipe({"sampling_rate": SAMPLING_RATE, "raw": y})
77
+ top_result = results[0] # Get the top result (most likely classification)
78
+ label = top_result["label"] # Extract the label from the top result
79
+ return label
80
+
81
  with gr.Blocks(theme=my_theme) as demo:
82
  with gr.Column(visible=True, elem_id="pantalla-inicial") as pantalla_inicial:
83
  gr.HTML(
 
164
  gr.Markdown("<h2>Predictor</h2>")
165
  audio_input = gr.Audio(
166
  min_length=1.0,
167
+ # max_length=10.0,
168
  format="wav",
169
+ # type="numpy",
170
+ label="Baby recorder"
171
+ ),
172
  classify_btn = gr.Button("¿Por qué llora?")
173
  classification_output = gr.Textbox(label="Tu bebé llora por:")
174
+ classify_btn.click(transcribe, inputs=audio_input, outputs=classification_output)
175
  with gr.Column():
176
  gr.Markdown("<h2>Assistant</h2>")
177
  system_message = "You are a Chatbot specialized in baby health and care."
 
179
  temperature = 0.7
180
  top_p = 0.95
181
  chatbot = gr.ChatInterface(
182
+ respond,
183
  additional_inputs=[
184
  gr.State(value=system_message),
185
  gr.State(value=max_tokens),
 
188
  ],
189
  )
190
  gr.Markdown("Este chatbot no sustituye a un profesional de la salud. Ante cualquier preocupación o duda, consulta con tu pediatra.")
191
+ boton_volver_inicio_1 = gr.Button("Volver a la pantalla inicial")
192
+ boton_volver_inicio_1.click(redirigir_a_pantalla_inicial, inputs=None, outputs=[pantalla_inicial, pagina_1])
193
  with gr.Column(visible=False) as pagina_2:
194
  gr.Markdown("<h2>Monitor</h2>")
195
  gr.Markdown("Contenido de la Página 2")
196
+ boton_volver_inicio_2 = gr.Button("Volver a la pantalla inicial")
197
+ boton_volver_inicio_2.click(redirigir_a_pantalla_inicial, inputs=None, outputs=[pantalla_inicial, pagina_2])
198
+ boton_pagina_1.click(mostrar_pagina_1, inputs=None, outputs=[pantalla_inicial, pagina_1])
199
+ boton_pagina_2.click(mostrar_pagina_2, inputs=None, outputs=[pantalla_inicial, pagina_2])
200
+ demo.launch()