File size: 4,025 Bytes
5cf41d0
ace06e3
4e570c9
 
 
 
cc3562b
6d1143c
4e570c9
 
 
 
 
 
 
2ca1b49
 
ace06e3
4e570c9
 
 
 
 
 
 
 
 
 
d8a3aa1
ace06e3
 
 
 
 
 
4e570c9
 
 
 
 
 
 
ace06e3
 
 
 
4e570c9
 
d8a3aa1
ace06e3
 
 
 
 
 
4e570c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ace06e3
 
 
 
 
 
4e570c9
ace06e3
4e570c9
 
 
ace06e3
 
4e570c9
ace06e3
 
 
 
 
 
4e570c9
ace06e3
 
 
 
 
 
4e570c9
 
ace06e3
 
 
4e570c9
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import gradio as gr
from huggingface_hub import InferenceClient
import os
from transformers import pipeline
import numpy as np
from model import SAMPLING_RATE, FEATURE_EXTRACTOR

token = os.getenv("HF_TOKEN")
# modelo = "mixed-data"
modelo = "cry-detector"
pipe = pipeline(
    "audio-classification",
    model=f"A-POR-LOS-8000/distilhubert-finetuned-{modelo}",
    use_auth_token=token
)
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct", token=token)
# client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407", token=token)

def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
    ):



    messages = [{"role": "system", "content": system_message}]
    for val in history:
        if val[0]:
            messages.append({"role": "assistant", "content": val[1]})
    messages.append({"role": "user", "content": message})
    response = ""
    for message in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
        ):
        token = message.choices[0].delta.content
        response += token
        yield response




my_theme = gr.themes.Soft(
    primary_hue="emerald",
    secondary_hue="green",
    shadow_spread='*button_shadow_active'
    )

def mostrar_pagina_1():
    return gr.update(visible=False), gr.update(visible=True)

def mostrar_pagina_2():
    return gr.update(visible=False), gr.update(visible=True)

def redirigir_a_pantalla_inicial():
    return gr.update(visible=True), gr.update(visible=False)

def transcribe(audio):
    _, y = audio
    y = y.astype(np.float32) # con torch.float32 da error
    y /= np.max(np.abs(y))
    results = pipe({"sampling_rate": SAMPLING_RATE, "raw": y})
    top_result = results[0]  # Get the top result (most likely classification)
    label = top_result["label"]  # Extract the label from the top result
    return label

with gr.Blocks(theme=my_theme) as demo:
    with gr.Column(visible=True, elem_id="pantalla-inicial") as pantalla_inicial:
        gr.HTML(
                gr.Markdown("<h2>Predictor</h2>")
                audio_input = gr.Audio(
                    min_length=1.0,
                    # max_length=10.0,
                    format="wav",
                    # type="numpy",
                    label="Baby recorder"
                    ),
                classify_btn = gr.Button("¿Por qué llora?")
                classification_output = gr.Textbox(label="Tu bebé llora por:")
                classify_btn.click(transcribe, inputs=audio_input, outputs=classification_output)
            with gr.Column():
                gr.Markdown("<h2>Assistant</h2>")
                system_message = "You are a Chatbot specialized in baby health and care."
                temperature = 0.7
                top_p = 0.95
                chatbot = gr.ChatInterface(
                    respond,
                    additional_inputs=[
                        gr.State(value=system_message),
                        gr.State(value=max_tokens),
                    ],
                )
                gr.Markdown("Este chatbot no sustituye a un profesional de la salud. Ante cualquier preocupación o duda, consulta con tu pediatra.")
                boton_volver_inicio_1 = gr.Button("Volver a la pantalla inicial")
        boton_volver_inicio_1.click(redirigir_a_pantalla_inicial, inputs=None, outputs=[pantalla_inicial, pagina_1])
    with gr.Column(visible=False) as pagina_2:
        gr.Markdown("<h2>Monitor</h2>")
        gr.Markdown("Contenido de la Página 2")
        boton_volver_inicio_2 = gr.Button("Volver a la pantalla inicial")
        boton_volver_inicio_2.click(redirigir_a_pantalla_inicial, inputs=None, outputs=[pantalla_inicial, pagina_2])
    boton_pagina_1.click(mostrar_pagina_1, inputs=None, outputs=[pantalla_inicial, pagina_1])
    boton_pagina_2.click(mostrar_pagina_2, inputs=None, outputs=[pantalla_inicial, pagina_2])
demo.launch()