Last commit not found
import gradio as gr | |
from queries import (clustering_esfuerzo_dieta_response, clustering_objetivo_response, clustering_entrenamiento_response, | |
clustering_cumplimiento_dieta_response, clustering_compromiso_response, clustering_diferencia_peso_response, | |
make_query, get_min_max_mean_mode_macros_differences) | |
from find_matches import find_user_dates_matches, find_macros_that_match_dates_of_users | |
def clustering_responses(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento, | |
cumplimiento_dieta, compromiso, variacion_peso): | |
cluster_esfuerzo_dieta = clustering_esfuerzo_dieta_response(esfuerzo_dieta) | |
cluster_objetivo = clustering_objetivo_response(objetivo) | |
cluster_entrenamiento = clustering_entrenamiento_response(cumplimiento_entrenamiento) | |
cluster_cumplimiento_dieta = clustering_cumplimiento_dieta_response(cumplimiento_dieta) | |
cluster_compromiso = clustering_compromiso_response(compromiso) | |
diff_peso_min, diff_peso_max = clustering_diferencia_peso_response(variacion_peso) | |
return cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta, cluster_compromiso, diff_peso_min, diff_peso_max | |
def calcular_macros(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento, | |
cumplimiento_dieta, compromiso, variacion_peso): | |
# Obtenemos los valores correspondientes a cada selecci贸n | |
valor_esfuerzo = next(list(opcion.values())[0]["value"] | |
for opcion in opciones_esfuerzo | |
if list(opcion.values())[0]["text"] == esfuerzo_dieta) | |
valor_objetivo = next(list(opcion.values())[0]["value"] | |
for opcion in opciones_objetivo | |
if list(opcion.values())[0]["text"] == objetivo) | |
valor_cumplimiento_entr = next(list(opcion.values())[0]["value"] | |
for opcion in opciones_cumplimiento_entrenamiento | |
if list(opcion.values())[0]["text"] == cumplimiento_entrenamiento) | |
valor_cumplimiento_dieta = next(list(opcion.values())[0]["value"] | |
for opcion in opciones_cumplimiento_dieta | |
if list(opcion.values())[0]["text"] == cumplimiento_dieta) | |
valor_compromiso = next(list(opcion.values())[0]["value"] | |
for opcion in opciones_compromiso | |
if list(opcion.values())[0]["text"] == compromiso) | |
# Clustering | |
(cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta, | |
cluster_compromiso, diff_peso_min, diff_peso_max) = clustering_responses(valor_esfuerzo, valor_objetivo, | |
valor_cumplimiento_entr, | |
valor_cumplimiento_dieta, valor_compromiso, | |
variacion_peso) | |
# Imprimimos los resultados | |
print(f"Consulta:") | |
print(f"\tEsfuerzo para cumplir dieta: {cluster_esfuerzo_dieta}") | |
print(f"\tObjetivo: {cluster_objetivo}") | |
print(f"\tEntrenamiento: {cluster_entrenamiento}") | |
print(f"\tCumplimiento dieta: {cluster_cumplimiento_dieta}") | |
print(f"\tCompromiso: {cluster_compromiso}") | |
print(f"\tVariaci贸n de peso: {variacion_peso}") | |
print(f"\t{diff_peso_min} <= Diferencia peso <= {diff_peso_max}") | |
# Crear query | |
query = make_query(cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta, cluster_compromiso, diff_peso_min, diff_peso_max) | |
# Print query | |
print(f"Query: {query}") | |
# # Crear diccionario de matches | |
# matches_dict = find_user_dates_matches(query) | |
# # Print matches | |
# print(f"Matches:\n{matches_dict}") | |
# # Find macros that match dates of users | |
# macros_differences_list = find_macros_that_match_dates_of_users(matches_dict) | |
# # Print macros | |
# print(f"Macros:\n{macros_differences_list}") | |
# # Calculate macros min, max and mean | |
# (train_day_protein_std, train_day_carbs_std, train_day_fat_std, intratrain_protein_std, intratrain_carbs_std, | |
# rest_day_protein_std, rest_day_carbs_std, rest_day_fat_std) = get_min_max_mean_mode_macros_differences(macros_differences_list) | |
# # Print macros min, max and mean | |
# print(f"Macros min, max and mean:\n{train_day_protein_std}, {train_day_carbs_std}, {train_day_fat_std}, {intratrain_protein_std}, {intratrain_carbs_std}, {rest_day_protein_std}, {rest_day_carbs_std}, {rest_day_fat_std}") | |
# # Create strings for the outputs | |
# train_day_protein_str = f"min: {train_day_protein_std[0]}, max: {train_day_protein_std[1]}, mean: {train_day_protein_std[2]:.2f}, mode: {train_day_protein_std[3]}" | |
# train_day_carbs_str = f"min: {train_day_carbs_std[0]}, max: {train_day_carbs_std[1]}, mean: {train_day_carbs_std[2]:.2f}, mode: {train_day_carbs_std[3]}" | |
# train_day_fat_str = f"min: {train_day_fat_std[0]}, max: {train_day_fat_std[1]}, mean: {train_day_fat_std[2]:.2f}, mode: {train_day_fat_std[3]}" | |
# intratrain_protein_str = f"min: {intratrain_protein_std[0]}, max: {intratrain_protein_std[1]}, mean: {intratrain_protein_std[2]:.2f}, mode: {intratrain_protein_std[3]}" | |
# intratrain_carbs_str = f"min: {intratrain_carbs_std[0]}, max: {intratrain_carbs_std[1]}, mean: {intratrain_carbs_std[2]:.2f}, mode: {intratrain_carbs_std[3]}" | |
# rest_day_protein_str = f"min: {rest_day_protein_std[0]}, max: {rest_day_protein_std[1]}, mean: {rest_day_protein_std[2]:.2f}, mode: {rest_day_protein_std[3]}" | |
# rest_day_carbs_str = f"min: {rest_day_carbs_std[0]}, max: {rest_day_carbs_std[1]}, mean: {rest_day_carbs_std[2]:.2f}, mode: {rest_day_carbs_std[3]}" | |
# rest_day_fat_str = f"min: {rest_day_fat_std[0]}, max: {rest_day_fat_std[1]}, mean: {rest_day_fat_std[2]:.2f}, mode: {rest_day_fat_std[3]}" | |
train_day_protein_str = "0" | |
train_day_carbs_str = "0" | |
train_day_fat_str = "0" | |
intratrain_protein_str = "0" | |
intratrain_carbs_str = "0" | |
rest_day_protein_str = "0" | |
rest_day_carbs_str = "0" | |
rest_day_fat_str = "0" | |
return train_day_protein_str, train_day_carbs_str, train_day_fat_str, intratrain_protein_str, intratrain_carbs_str, rest_day_protein_str, rest_day_carbs_str, rest_day_fat_str | |
# Definimos las opciones para cada men煤 desplegable | |
opciones_esfuerzo = [ | |
{ | |
"No entiendo la calculadora, quiero men煤s tipo": { | |
"text": "No entiendo la calculadora, quiero men煤s tipo", | |
"value": " | no data" | |
} | |
}, | |
{ | |
"No cost贸 nada": { | |
"text": "No cost贸 nada", | |
"value": " | no costo" | |
} | |
}, | |
{ | |
"Cost贸 demasiado, s煤beme macros": { | |
"text": "Cost贸 demasiado, s煤beme macros", | |
"value": " | costo subir macros" | |
} | |
}, | |
{ | |
"Cost贸 demasiado, b谩jame macros": { | |
"text": "Cost贸 demasiado, b谩jame macros", | |
"value": " | costo bajar macros" | |
} | |
}, | |
{ | |
"Cost贸, pero me adapto a nuevos ajustes": { | |
"text": "Cost贸, pero me adapto a nuevos ajustes", | |
"value": " | costo y me adapto a nuevos ajustes" | |
} | |
}, | |
{ | |
"Iba a coger men煤s tipo, pero al final por precio no": { | |
"text": "Iba a coger men煤s tipo, pero al final por precio no", | |
"value": " | no data" | |
} | |
} | |
] | |
opciones_objetivo = [ | |
{ | |
"definici贸n (nada cambia)": { | |
"text": "definici贸n (nada cambia)", | |
"value": " | definicion" | |
} | |
}, | |
{ | |
"empezamos a definir (cambia)": { | |
"text": "empezamos a definir (cambia)", | |
"value": " | definicion" | |
} | |
}, | |
{ | |
"perder peso (nada cambia)": { | |
"text": "perder peso (nada cambia)", | |
"value": " | definicion" | |
} | |
}, | |
{ | |
"volumen (nada cambia)": { | |
"text": "volumen (nada cambia)", | |
"value": " | volumen" | |
} | |
}, | |
{ | |
"empezamos a coger volumen (cambia)": { | |
"text": "empezamos a coger volumen (cambia)", | |
"value": " | volumen" | |
} | |
}, | |
{ | |
"empezamos a coger volumen, sobre todo tren inferior (cambia)": { | |
"text": "empezamos a coger volumen, sobre todo tren inferior (cambia)", | |
"value": " | volumen" | |
} | |
}, | |
{ | |
"empezamos a coger volumen, en todo el cuerpo (cambia)": { | |
"text": "empezamos a coger volumen, en todo el cuerpo (cambia)", | |
"value": " | volumen" | |
} | |
} | |
] | |
opciones_cumplimiento_entrenamiento = [ | |
{ | |
"Lo hice perfecto": { | |
"text": "Lo hice perfecto", | |
"value": " | bien" | |
} | |
}, | |
{ | |
"He fallado algunos d铆as, pero s铆": { | |
"text": "He fallado algunos d铆as, pero s铆", | |
"value": " | bien" | |
} | |
}, | |
{ | |
"Lesi贸n importante": { | |
"text": "Lesi贸n importante", | |
"value": " | mal" | |
} | |
}, | |
{ | |
"Lo hice pr谩cticamente perfecto": { | |
"text": "Lo hice pr谩cticamente perfecto", | |
"value": " | bien" | |
} | |
}, | |
{ | |
"Peque帽a lesi贸n": { | |
"text": "Peque帽a lesi贸n", | |
"value": " | mal" | |
} | |
}, | |
{ | |
"No hice nada, mantenemos la rutina un mes m谩s": { | |
"text": "No hice nada, mantenemos la rutina un mes m谩s", | |
"value": " | mal" | |
} | |
}, | |
{ | |
"Al谩rgame la rutina una semana m谩s": { | |
"text": "Al谩rgame la rutina una semana m谩s", | |
"value": " | mal" | |
} | |
} | |
] | |
opciones_cumplimiento_dieta = [ | |
{ | |
"al 70%": { | |
"text": "al 70%", | |
"value": " | bien" | |
} | |
}, | |
{ | |
"regular, me cuesta llegar": { | |
"text": "regular, me cuesta llegar", | |
"value": " | regular" | |
} | |
}, | |
{ | |
"Nada, mant茅n mis macros": { | |
"text": "Nada, mant茅n mis macros", | |
"value": " | mal" | |
} | |
}, | |
{ | |
"casi perfecta": { | |
"text": "casi perfecta", | |
"value": " | bien" | |
} | |
}, | |
{ | |
"regular, me salto la dieta": { | |
"text": "regular, me salto la dieta", | |
"value": " | regular" | |
} | |
}, | |
{ | |
"Perfecta": { | |
"text": "Perfecta", | |
"value": " | bien" | |
} | |
} | |
] | |
opciones_compromiso = [ | |
{ | |
"Bueno, pero mejorable": { | |
"text": "Bueno, pero mejorable", | |
"value": " | bueno" | |
} | |
}, | |
{ | |
"Mal, pero a partir de ahora voy a por todas": { | |
"text": "Mal, pero a partir de ahora voy a por todas", | |
"value": " | mal" | |
} | |
}, | |
{ | |
"Mal, demasiado exigente": { | |
"text": "Mal, demasiado exigente", | |
"value": " | mal" | |
} | |
}, | |
{ | |
"M谩ximo": { | |
"text": "M谩ximo", | |
"value": " | bueno" | |
} | |
} | |
] | |
# Creamos la interfaz | |
with gr.Blocks() as demo: | |
# Definimos el color naranja | |
naranja = "#FF9300" | |
# Procesamos las opciones para obtener solo los textos | |
textos_esfuerzo = [list(opcion.values())[0]["text"] for opcion in opciones_esfuerzo] | |
textos_objetivo = [list(opcion.values())[0]["text"] for opcion in opciones_objetivo] | |
textos_cumplimiento_entr = [list(opcion.values())[0]["text"] for opcion in opciones_cumplimiento_entrenamiento] | |
textos_cumplimiento_dieta = [list(opcion.values())[0]["text"] for opcion in opciones_cumplimiento_dieta] | |
textos_compromiso = [list(opcion.values())[0]["text"] for opcion in opciones_compromiso] | |
# Entradas | |
with gr.Row(): | |
esfuerzo = gr.Dropdown( | |
choices=textos_esfuerzo, | |
label="Esfuerzo dieta", | |
value="No cost贸 nada" | |
) | |
cumplimiento_dieta = gr.Dropdown( | |
choices=textos_cumplimiento_dieta, | |
label="Cumplimiento de la dieta", | |
value="Perfecta" | |
) | |
objetivo = gr.Dropdown( | |
choices=textos_objetivo, | |
label="Objetivo", | |
value="volumen (nada cambia)" | |
) | |
with gr.Row(): | |
cumplimiento_entr = gr.Dropdown( | |
choices=textos_cumplimiento_entr, | |
label="Cumplimiento del entrenamiento", | |
value="Lo hice perfecto" | |
) | |
compromiso = gr.Dropdown( | |
choices=textos_compromiso, | |
label="Compromiso", | |
value="M谩ximo" | |
) | |
variacion_peso = gr.Number( | |
label="Variaci贸n de peso", | |
precision=2, | |
value=0.7 | |
) | |
# Versi贸n simple del bot贸n | |
calcular_btn = gr.Button( | |
"Calcular", | |
variant="primary", | |
size="lg" | |
) | |
# A帽adimos el estilo CSS personalizado | |
css = f""" | |
<style> | |
#boton-naranja {{ | |
background-color: {naranja} !important; | |
border: 1px solid {naranja} !important; | |
}} | |
#boton-naranja:hover {{ | |
background-color: {naranja}DD !important; | |
border: 1px solid {naranja}DD !important; | |
}} | |
</style> | |
""" | |
css_outputs = """ | |
<style> | |
.output-row { | |
align-items: flex-end !important; | |
display: flex !important; | |
gap: 1rem !important; | |
} | |
.output-row > * { | |
flex: 1; | |
min-width: 0; | |
} | |
</style> | |
""" | |
gr.Markdown(css + css_outputs) | |
# Salidas | |
with gr.Row(elem_classes="output-row"): | |
proteina_entreno = gr.Textbox(label="Prote铆na d铆a de entreno (g)") | |
carbs_entreno = gr.Textbox(label="Carbohidratos d铆a de entreno (g)") | |
grasas_entreno = gr.Textbox(label="Grasas d铆a de entreno (g)") | |
proteina_intra = gr.Textbox(label="Prote铆na intraentreno (g)") | |
carbs_intra = gr.Textbox(label="Carbohidratos intraentreno (g)") | |
proteina_descanso = gr.Textbox(label="Prote铆na d铆a de descanso (g)") | |
carbs_descanso = gr.Textbox(label="Carbohidratos d铆a de descanso (g)") | |
grasas_descanso = gr.Textbox(label="Grasas d铆a de descanso (g)") | |
# Conectamos el bot贸n con la funci贸n | |
calcular_btn.click( | |
fn=calcular_macros, | |
inputs=[esfuerzo, objetivo, cumplimiento_entr, cumplimiento_dieta, | |
compromiso, variacion_peso], | |
outputs=[proteina_entreno, carbs_entreno, grasas_entreno, | |
proteina_intra, carbs_intra, | |
proteina_descanso, carbs_descanso, grasas_descanso] | |
) | |
demo.launch(share=True) | |