Spaces:
Sleeping
Sleeping
Marcos12886
commited on
Commit
•
4e570c9
1
Parent(s):
d99bc5f
Update app.py
Browse files
app.py
CHANGED
@@ -1,176 +1,110 @@
|
|
1 |
-
import os
|
2 |
-
import torch
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
token = os.getenv("HF_TOKEN")
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
model
|
|
|
|
|
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
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
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]:
|
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 |
token = message.choices[0].delta.content
|
37 |
response += token
|
38 |
yield response
|
39 |
|
40 |
-
|
41 |
-
|
42 |
|
43 |
my_theme = gr.themes.Soft(
|
44 |
primary_hue="emerald",
|
45 |
secondary_hue="green",
|
46 |
-
neutral_hue="slate",
|
47 |
-
text_size="sm",
|
48 |
-
spacing_size="sm",
|
49 |
-
font=[gr.themes.GoogleFont('Nunito'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
|
50 |
-
font_mono=[gr.themes.GoogleFont('Nunito'), 'ui-monospace', 'Consolas', 'monospace'],
|
51 |
-
).set(
|
52 |
-
body_background_fill='*neutral_50',
|
53 |
-
body_text_color='*neutral_600',
|
54 |
-
body_text_size='*text_sm',
|
55 |
-
embed_radius='*radius_md',
|
56 |
-
shadow_drop='*shadow_spread',
|
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(
|
63 |
-
"""
|
64 |
-
<style>
|
65 |
-
@import url('https://fonts.googleapis.com/css2?family=Lobster&display=swap');
|
66 |
-
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
|
67 |
-
|
68 |
-
h1 {
|
69 |
-
font-family: 'Lobster', cursive;
|
70 |
-
font-size: 5em !important;
|
71 |
-
text-align: center;
|
72 |
-
margin: 0;
|
73 |
-
}
|
74 |
-
|
75 |
-
.gr-button {
|
76 |
-
background-color: #4CAF50 !important;
|
77 |
-
color: white !important;
|
78 |
-
border: none;
|
79 |
-
padding: 15px 32px;
|
80 |
-
text-align: center;
|
81 |
-
text-decoration: none;
|
82 |
-
display: inline-block;
|
83 |
-
font-size: 16px;
|
84 |
-
margin: 4px 2px;
|
85 |
-
cursor: pointer;
|
86 |
-
border-radius: 12px;
|
87 |
-
}
|
88 |
-
|
89 |
-
.gr-button:hover {
|
90 |
-
background-color: #45a049;
|
91 |
-
}
|
92 |
-
h2 {
|
93 |
-
font-family: 'Lobster', cursive;
|
94 |
-
font-size: 3em !important;
|
95 |
-
text-align: center;
|
96 |
-
margin: 0;
|
97 |
-
}
|
98 |
-
p.slogan, h4, p, h3 {
|
99 |
-
font-family: 'Roboto', sans-serif;
|
100 |
-
text-align: center;
|
101 |
-
}
|
102 |
-
</style>
|
103 |
-
<h1>Iremia</h1>
|
104 |
-
<h4 style='text-align: center; font-size: 1.5em'>Tu aliado para el bienestar de tu bebé</h4>
|
105 |
-
"""
|
106 |
-
)
|
107 |
-
gr.Markdown(
|
108 |
-
"<h4 style='text-align: left; font-size: 1.5em;'>¿Qué es Iremia?</h4>"
|
109 |
-
)
|
110 |
-
gr.Markdown(
|
111 |
-
"<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>"
|
112 |
-
)
|
113 |
-
gr.Markdown(
|
114 |
-
"<h4 style='text-align: left; font-size: 1.5em;'>Nuestra misión</h4>"
|
115 |
-
)
|
116 |
-
gr.Markdown(
|
117 |
-
"<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>"
|
118 |
-
)
|
119 |
-
gr.Markdown(
|
120 |
-
"<h4 style='text-align: left; font-size: 1.5em;'>¿Qué ofrece Iremia?</h4>"
|
121 |
-
)
|
122 |
-
gr.Markdown(
|
123 |
-
"<p style='text-align: left'>Iremia ofrece dos funcionalidades muy interesantes:</p>"
|
124 |
-
)
|
125 |
-
gr.Markdown(
|
126 |
-
"<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>"
|
127 |
-
)
|
128 |
-
gr.Markdown(
|
129 |
-
"<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>"
|
130 |
-
)
|
131 |
-
with gr.Row():
|
132 |
-
with gr.Column():
|
133 |
-
gr.Markdown("<h2>Predictor</h2>")
|
134 |
-
boton_pagina_1 = gr.Button("Prueba el predictor")
|
135 |
-
gr.Markdown("<p>Descubre por qué llora tu bebé y resuelve dudas sobre su cuidado con nuestro Iremia assistant</p>")
|
136 |
-
with gr.Column():
|
137 |
-
gr.Markdown("<h2>Monitor</h2>")
|
138 |
-
boton_pagina_2 = gr.Button("Prueba el monitor")
|
139 |
-
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>")
|
140 |
-
with gr.Column(visible=False) as pagina_1:
|
141 |
-
with gr.Row():
|
142 |
-
with gr.Column():
|
143 |
gr.Markdown("<h2>Predictor</h2>")
|
144 |
audio_input = gr.Audio(
|
145 |
min_length=1.0,
|
|
|
146 |
format="wav",
|
147 |
-
|
148 |
-
|
149 |
-
)
|
150 |
classify_btn = gr.Button("¿Por qué llora?")
|
151 |
classification_output = gr.Textbox(label="Tu bebé llora por:")
|
152 |
-
classify_btn.click(
|
153 |
with gr.Column():
|
154 |
gr.Markdown("<h2>Assistant</h2>")
|
155 |
system_message = "You are a Chatbot specialized in baby health and care."
|
156 |
-
max_tokens = 512
|
157 |
temperature = 0.7
|
158 |
top_p = 0.95
|
159 |
chatbot = gr.ChatInterface(
|
160 |
-
respond,
|
161 |
additional_inputs=[
|
162 |
gr.State(value=system_message),
|
163 |
gr.State(value=max_tokens),
|
164 |
-
gr.State(value=temperature),
|
165 |
-
gr.State(value=top_p)
|
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")
|
|
|
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")
|
174 |
-
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
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 |
+
|
28 |
+
|
29 |
|
|
|
30 |
messages = [{"role": "system", "content": system_message}]
|
31 |
for val in history:
|
32 |
if val[0]:
|
|
|
|
|
33 |
messages.append({"role": "assistant", "content": val[1]})
|
34 |
messages.append({"role": "user", "content": message})
|
35 |
response = ""
|
36 |
+
for message in client.chat_completion(
|
37 |
+
messages,
|
38 |
+
max_tokens=max_tokens,
|
39 |
+
stream=True,
|
40 |
+
temperature=temperature,
|
41 |
+
top_p=top_p,
|
42 |
+
):
|
43 |
token = message.choices[0].delta.content
|
44 |
response += token
|
45 |
yield response
|
46 |
|
47 |
+
|
48 |
+
|
49 |
|
50 |
my_theme = gr.themes.Soft(
|
51 |
primary_hue="emerald",
|
52 |
secondary_hue="green",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
shadow_spread='*button_shadow_active'
|
54 |
)
|
55 |
|
56 |
+
def mostrar_pagina_1():
|
57 |
+
return gr.update(visible=False), gr.update(visible=True)
|
58 |
+
|
59 |
+
def mostrar_pagina_2():
|
60 |
+
return gr.update(visible=False), gr.update(visible=True)
|
61 |
+
|
62 |
+
def redirigir_a_pantalla_inicial():
|
63 |
+
return gr.update(visible=True), gr.update(visible=False)
|
64 |
+
|
65 |
+
def transcribe(audio):
|
66 |
+
_, y = audio
|
67 |
+
y = y.astype(np.float32) # con torch.float32 da error
|
68 |
+
y /= np.max(np.abs(y))
|
69 |
+
results = pipe({"sampling_rate": SAMPLING_RATE, "raw": y})
|
70 |
+
top_result = results[0] # Get the top result (most likely classification)
|
71 |
+
label = top_result["label"] # Extract the label from the top result
|
72 |
+
return label
|
73 |
+
|
74 |
with gr.Blocks(theme=my_theme) as demo:
|
75 |
with gr.Column(visible=True, elem_id="pantalla-inicial") as pantalla_inicial:
|
76 |
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
gr.Markdown("<h2>Predictor</h2>")
|
78 |
audio_input = gr.Audio(
|
79 |
min_length=1.0,
|
80 |
+
# max_length=10.0,
|
81 |
format="wav",
|
82 |
+
# type="numpy",
|
83 |
+
label="Baby recorder"
|
84 |
+
),
|
85 |
classify_btn = gr.Button("¿Por qué llora?")
|
86 |
classification_output = gr.Textbox(label="Tu bebé llora por:")
|
87 |
+
classify_btn.click(transcribe, inputs=audio_input, outputs=classification_output)
|
88 |
with gr.Column():
|
89 |
gr.Markdown("<h2>Assistant</h2>")
|
90 |
system_message = "You are a Chatbot specialized in baby health and care."
|
|
|
91 |
temperature = 0.7
|
92 |
top_p = 0.95
|
93 |
chatbot = gr.ChatInterface(
|
94 |
+
respond,
|
95 |
additional_inputs=[
|
96 |
gr.State(value=system_message),
|
97 |
gr.State(value=max_tokens),
|
|
|
|
|
98 |
],
|
99 |
)
|
100 |
gr.Markdown("Este chatbot no sustituye a un profesional de la salud. Ante cualquier preocupación o duda, consulta con tu pediatra.")
|
101 |
+
boton_volver_inicio_1 = gr.Button("Volver a la pantalla inicial")
|
102 |
+
boton_volver_inicio_1.click(redirigir_a_pantalla_inicial, inputs=None, outputs=[pantalla_inicial, pagina_1])
|
103 |
with gr.Column(visible=False) as pagina_2:
|
104 |
gr.Markdown("<h2>Monitor</h2>")
|
105 |
gr.Markdown("Contenido de la Página 2")
|
106 |
+
boton_volver_inicio_2 = gr.Button("Volver a la pantalla inicial")
|
107 |
+
boton_volver_inicio_2.click(redirigir_a_pantalla_inicial, inputs=None, outputs=[pantalla_inicial, pagina_2])
|
108 |
+
boton_pagina_1.click(mostrar_pagina_1, inputs=None, outputs=[pantalla_inicial, pagina_1])
|
109 |
+
boton_pagina_2.click(mostrar_pagina_2, inputs=None, outputs=[pantalla_inicial, pagina_2])
|
110 |
+
demo.launch()
|