Spaces:
Sleeping
Sleeping
Marcos12886
commited on
Commit
•
ace06e3
1
Parent(s):
6d1143c
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,73 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
from transformers import pipeline
|
4 |
import numpy as np
|
5 |
-
from model import SAMPLING_RATE
|
6 |
|
7 |
token = os.getenv("HF_TOKEN")
|
8 |
modelo = "mixed-data"
|
9 |
# modelo = "cry-detector"
|
10 |
-
pipe = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def transcribe(audio):
|
13 |
_, y = audio
|
@@ -18,13 +78,123 @@ def transcribe(audio):
|
|
18 |
label = top_result["label"] # Extract the label from the top result
|
19 |
return label
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
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]:
|
30 |
+
messages.append({"role": "user", "content": val[0]})
|
31 |
+
if val[1]:
|
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",
|
49 |
+
neutral_hue="slate",
|
50 |
+
text_size="sm",
|
51 |
+
spacing_size="sm",
|
52 |
+
font=[gr.themes.GoogleFont('Nunito'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
|
53 |
+
font_mono=[gr.themes.GoogleFont('Nunito'), 'ui-monospace', 'Consolas', 'monospace'],
|
54 |
+
).set(
|
55 |
+
body_background_fill='*neutral_50',
|
56 |
+
body_text_color='*neutral_600',
|
57 |
+
body_text_size='*text_sm',
|
58 |
+
embed_radius='*radius_md',
|
59 |
+
shadow_drop='*shadow_spread',
|
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
|
|
|
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(
|
84 |
+
"""
|
85 |
+
<style>
|
86 |
+
@import url('https://fonts.googleapis.com/css2?family=Lobster&display=swap');
|
87 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
|
88 |
+
|
89 |
+
h1 {
|
90 |
+
font-family: 'Lobster', cursive;
|
91 |
+
font-size: 5em !important;
|
92 |
+
text-align: center;
|
93 |
+
margin: 0;
|
94 |
+
}
|
95 |
+
|
96 |
+
.gr-button {
|
97 |
+
background-color: #4CAF50 !important;
|
98 |
+
color: white !important;
|
99 |
+
border: none;
|
100 |
+
padding: 15px 32px;
|
101 |
+
text-align: center;
|
102 |
+
text-decoration: none;
|
103 |
+
display: inline-block;
|
104 |
+
font-size: 16px;
|
105 |
+
margin: 4px 2px;
|
106 |
+
cursor: pointer;
|
107 |
+
border-radius: 12px;
|
108 |
+
}
|
109 |
+
|
110 |
+
.gr-button:hover {
|
111 |
+
background-color: #45a049;
|
112 |
+
}
|
113 |
+
h2 {
|
114 |
+
font-family: 'Lobster', cursive;
|
115 |
+
font-size: 3em !important;
|
116 |
+
text-align: center;
|
117 |
+
margin: 0;
|
118 |
+
}
|
119 |
+
p.slogan, h4, p, h3 {
|
120 |
+
font-family: 'Roboto', sans-serif;
|
121 |
+
text-align: center;
|
122 |
+
}
|
123 |
+
</style>
|
124 |
+
<h1>Iremia</h1>
|
125 |
+
<h4 style='text-align: center; font-size: 1.5em'>Tu aliado para el bienestar de tu bebé</h4>
|
126 |
+
"""
|
127 |
+
)
|
128 |
+
gr.Markdown(
|
129 |
+
"<h4 style='text-align: left; font-size: 1.5em;'>¿Qué es Iremia?</h4>"
|
130 |
+
)
|
131 |
+
gr.Markdown(
|
132 |
+
"<p style='text-align: left'>Iremia es un proyecto llevado a cabo por un grupo de estudiantes interesados en el desarrollo de modelos de inteligencia artificial, enfocados específicamente en casos de uso relevantes para ayudar a cuidar a los más pequeños de la casa.</p>"
|
133 |
+
)
|
134 |
+
gr.Markdown(
|
135 |
+
"<h4 style='text-align: left; font-size: 1.5em;'>Nuestra misión</h4>"
|
136 |
+
)
|
137 |
+
gr.Markdown(
|
138 |
+
"<p style='text-align: left'>Sabemos que la paternidad puede suponer un gran desafío. Nuestra misión es brindarles a todos los padres unas herramientas de última tecnología que los ayuden a navegar esos primeros meses de vida tan cruciales en el desarrollo de sus pequeños.</p>"
|
139 |
+
)
|
140 |
+
gr.Markdown(
|
141 |
+
"<h4 style='text-align: left; font-size: 1.5em;'>¿Qué ofrece Iremia?</h4>"
|
142 |
+
)
|
143 |
+
gr.Markdown(
|
144 |
+
"<p style='text-align: left'>Iremia ofrece dos funcionalidades muy interesantes:</p>"
|
145 |
+
)
|
146 |
+
gr.Markdown(
|
147 |
+
"<p style='text-align: left'>Predictor: Con nuestro modelo de inteligencia artificial, somos capaces de predecir por qué tu hijo de menos de 2 años está llorando. Además, tendrás acceso a un asistente personal para consultar cualquier duda que tengas sobre el cuidado de tu pequeño.</p>"
|
148 |
+
)
|
149 |
+
gr.Markdown(
|
150 |
+
"<p style='text-align: left'>Monitor: Nuestro monitor no es como otros que hay en el mercado, ya que es capaz de reconocer si un sonido es un llanto del bebé o no, y si está llorando, predice automáticamente la causa, lo cual te brindará la tranquilidad de saber siempre qué pasa con tu pequeño y te ahorrará tiempo y muchas horas de sueño.</p>"
|
151 |
+
)
|
152 |
+
with gr.Row():
|
153 |
+
with gr.Column():
|
154 |
+
gr.Markdown("<h2>Predictor</h2>")
|
155 |
+
boton_pagina_1 = gr.Button("Prueba el predictor")
|
156 |
+
gr.Markdown("<p>Descubre por qué llora tu bebé y resuelve dudas sobre su cuidado con nuestro Iremia assistant</p>")
|
157 |
+
with gr.Column():
|
158 |
+
gr.Markdown("<h2>Monitor</h2>")
|
159 |
+
boton_pagina_2 = gr.Button("Prueba el monitor")
|
160 |
+
gr.Markdown("<p>Un monitor inteligente que detecta si tu hijo está llorando y te indica el motivo antes de que puedas levantarte del sofá</p>")
|
161 |
+
with gr.Column(visible=False) as pagina_1:
|
162 |
+
with gr.Row():
|
163 |
+
with gr.Column():
|
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."
|
178 |
+
max_tokens = 512
|
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),
|
186 |
+
gr.State(value=temperature),
|
187 |
+
gr.State(value=top_p)
|
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()
|