Maximofn commited on
Commit
21368bb
·
1 Parent(s): cbc3ea4

feat(SRC): :rocket: Add logs as output to get from API

Browse files
Files changed (1) hide show
  1. app.py +56 -3
app.py CHANGED
@@ -22,6 +22,9 @@ def calcular_macros(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento,
22
  train_day_protein_initial, train_day_carbs_initial, train_day_fat_initial,
23
  intratrain_protein_initial, intratrain_carbs_initial,
24
  rest_day_protein_initial, rest_day_carbs_initial, rest_day_fat_initial):
 
 
 
25
  # Obtenemos los valores correspondientes a cada selección
26
  valor_esfuerzo = next(list(opcion.values())[0]["value"]
27
  for opcion in opciones_esfuerzo
@@ -46,6 +49,7 @@ def calcular_macros(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento,
46
  # Clustering
47
  variacion_peso = peso_final - peso_inicial
48
  print(f"\n\nVariación de peso: {variacion_peso}")
 
49
  (cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta,
50
  cluster_compromiso, diff_peso_min, diff_peso_max) = clustering_responses(valor_esfuerzo, valor_objetivo,
51
  valor_cumplimiento_entr,
@@ -54,28 +58,40 @@ def calcular_macros(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento,
54
 
55
  # Imprimimos los resultados
56
  print(f"Respuestas formulario:")
 
57
  print(f"\tEsfuerzo para cumplir dieta: {cluster_esfuerzo_dieta}")
 
58
  print(f"\tObjetivo: {cluster_objetivo}")
 
59
  print(f"\tEntrenamiento: {cluster_entrenamiento}")
 
60
  print(f"\tCumplimiento dieta: {cluster_cumplimiento_dieta}")
 
61
  print(f"\tCompromiso: {cluster_compromiso}")
 
62
  print(f"\tPeso inicial: {peso_inicial}, peso final: {peso_final} --> Diferencia: {variacion_peso:.1f}")
 
63
  print(f"\t{diff_peso_min} <= Diferencia peso <= {diff_peso_max}")
 
64
 
65
  # Crear query
66
  query = make_query(cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta, cluster_compromiso, diff_peso_min, diff_peso_max)
67
 
68
  # Print query
69
  print(f"Query:")
 
70
  for q in query:
71
  print(f"\t{list(q.keys())[0]}", end=" --> ")
 
72
  for primary_key in q.keys():
73
  primary_key_dict = q[primary_key]
74
  for i, secondary_key in enumerate(primary_key_dict.keys()):
75
  if i == len(primary_key_dict.keys()) - 1:
76
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end="")
 
77
  else:
78
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end=", ")
 
79
  print()
80
 
81
  # Crear diccionario de matches
@@ -85,38 +101,52 @@ def calcular_macros(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento,
85
  # Si no hay matches, intentamos con una query más básica
86
  if num_matches == 0:
87
  print("No se han encontrado coincidencias con la query original, intentamos con una query más básica")
 
88
  query = make_query(cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta, cluster_compromiso, diff_peso_min, diff_peso_max, basic_query=True)
89
 
90
  # Print query
91
  print(f"Query:")
 
92
  for q in query:
93
  print(f"\t{list(q.keys())[0]}", end=" --> ")
 
94
  for primary_key in q.keys():
95
  primary_key_dict = q[primary_key]
96
  for i, secondary_key in enumerate(primary_key_dict.keys()):
97
  if i == len(primary_key_dict.keys()) - 1:
98
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end="")
 
99
  else:
100
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end=", ")
101
-
 
 
 
 
102
  matches_dict = find_user_dates_matches(query)
103
  num_matches = len(matches_dict)
104
 
105
  # Print matches
106
  print(f"Matches ({num_matches})")
 
107
  for user, dates in matches_dict.items():
108
  print(f"\t{user}", end=" --> ")
 
109
  for date in dates:
110
  print(f"{date}", end=", ")
 
111
  print()
 
112
 
113
  # Find macros that match dates of users
114
  macros_differences_list, macros_differences_dict = find_macros_that_match_dates_of_users(matches_dict)
115
 
116
  # Print macros
117
  print(f"Diferencia de macros ({len(macros_differences_dict)}):")
 
118
  for user, macros_difference in macros_differences_dict.items():
119
  print(f"\t{user} --> fecha de match: {macros_difference['date_of_match']}, fecha de asignación de macros: {macros_difference['date_of_macros_asignation']}, días entre match y asignación: {macros_difference['days_between_match_and_macros_asignation']}, diferencia de macros: {macros_difference['macros_difference']}")
 
120
 
121
  # Calculate macros min, max and mean
122
  if len(macros_differences_list) > 0:
@@ -134,14 +164,23 @@ def calcular_macros(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento,
134
 
135
  # Print macros min, max and mean
136
  print("Variación de macros:")
 
137
  print(f"\tproteína día de entreno: 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]}")
 
138
  print(f"\tcarbohidratos día de entreno: 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]}")
 
139
  print(f"\tgrasas día de entreno: 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]}")
 
140
  print(f"\tproteína intraentreno: Min: {intratrain_protein_std[0]}, Max: {intratrain_protein_std[1]}, Mean: {intratrain_protein_std[2]:.2f}, Mode: {intratrain_protein_std[3]}")
 
141
  print(f"\tcarbohidratos intraentreno: Min: {intratrain_carbs_std[0]}, Max: {intratrain_carbs_std[1]}, Mean: {intratrain_carbs_std[2]:.2f}, Mode: {intratrain_carbs_std[3]}")
 
142
  print(f"\tproteína día de descanso: 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]}")
 
143
  print(f"\tcarbohidratos día de descanso: 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]}")
 
144
  print(f"\tgrasas día de descanso: 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]}")
 
145
 
146
  # Get macros mode
147
  train_day_protein_mode = train_day_protein_std[3]
@@ -165,18 +204,28 @@ def calcular_macros(esfuerzo_dieta, objetivo, cumplimiento_entrenamiento,
165
 
166
  # Print macros
167
  print("Macros finales:")
 
168
  print(f"\tProteína día de entreno inicial: {train_day_protein_initial} --> final: {train_day_protein_final}")
 
169
  print(f"\tCarbohidratos día de entreno inicial: {train_day_carbs_initial} --> final: {train_day_carbs_final}")
 
170
  print(f"\tGrasas día de entreno inicial: {train_day_fat_initial} --> final: {train_day_fat_final}")
 
171
  print(f"\tProteína intraentreno inicial: {intratrain_protein_initial} --> final: {intratrain_protein_final}")
 
172
  print(f"\tCarbohidratos intraentreno inicial: {intratrain_carbs_initial} --> final: {intratrain_carbs_final}")
 
173
  print(f"\tProteína día de descanso inicial: {rest_day_protein_initial} --> final: {rest_day_protein_final}")
 
174
  print(f"\tCarbohidratos día de descanso inicial: {rest_day_carbs_initial} --> final: {rest_day_carbs_final}")
 
175
  print(f"\tGrasas día de descanso inicial: {rest_day_fat_initial} --> final: {rest_day_fat_final}")
 
176
 
177
  return (train_day_protein_final, train_day_carbs_final, train_day_fat_final,
178
  intratrain_protein_final, intratrain_carbs_final,
179
- rest_day_protein_final, rest_day_carbs_final, rest_day_fat_final)
 
180
 
181
  # Definimos el color naranja
182
  naranja = "#ea580b"
@@ -391,6 +440,9 @@ with gr.Blocks(css=style_css, elem_classes="demo") as demo:
391
  rest_day_carbs_final = gr.Number(label="Carbohidratos día de descanso Final (g)", precision=0, elem_classes="output-number")
392
  rest_day_fat_final = gr.Number(label="Grasas día de descanso Final (g)", precision=0, elem_classes="output-number")
393
 
 
 
 
394
  # Conectamos el botón con la función
395
  calcular_btn.click(
396
  fn=calcular_macros,
@@ -401,7 +453,8 @@ with gr.Blocks(css=style_css, elem_classes="demo") as demo:
401
  rest_day_protein_initial, rest_day_carbs_initial, rest_day_fat_initial],
402
  outputs=[train_day_protein_final, train_day_carbs_final, train_day_fat_final,
403
  intratrain_protein_final, intratrain_carbs_final,
404
- rest_day_protein_final, rest_day_carbs_final, rest_day_fat_final]
 
405
  )
406
 
407
  demo.launch()
 
22
  train_day_protein_initial, train_day_carbs_initial, train_day_fat_initial,
23
  intratrain_protein_initial, intratrain_carbs_initial,
24
  rest_day_protein_initial, rest_day_carbs_initial, rest_day_fat_initial):
25
+ # Logs
26
+ logs = ""
27
+
28
  # Obtenemos los valores correspondientes a cada selección
29
  valor_esfuerzo = next(list(opcion.values())[0]["value"]
30
  for opcion in opciones_esfuerzo
 
49
  # Clustering
50
  variacion_peso = peso_final - peso_inicial
51
  print(f"\n\nVariación de peso: {variacion_peso}")
52
+ logs += f"\n\nVariación de peso: {variacion_peso}"
53
  (cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta,
54
  cluster_compromiso, diff_peso_min, diff_peso_max) = clustering_responses(valor_esfuerzo, valor_objetivo,
55
  valor_cumplimiento_entr,
 
58
 
59
  # Imprimimos los resultados
60
  print(f"Respuestas formulario:")
61
+ logs += f"\n\nRespuestas formulario:"
62
  print(f"\tEsfuerzo para cumplir dieta: {cluster_esfuerzo_dieta}")
63
+ logs += f"\n\tEsfuerzo para cumplir dieta: {cluster_esfuerzo_dieta}"
64
  print(f"\tObjetivo: {cluster_objetivo}")
65
+ logs += f"\n\tObjetivo: {cluster_objetivo}"
66
  print(f"\tEntrenamiento: {cluster_entrenamiento}")
67
+ logs += f"\n\tEntrenamiento: {cluster_entrenamiento}"
68
  print(f"\tCumplimiento dieta: {cluster_cumplimiento_dieta}")
69
+ logs += f"\n\tCumplimiento dieta: {cluster_cumplimiento_dieta}"
70
  print(f"\tCompromiso: {cluster_compromiso}")
71
+ logs += f"\n\tCompromiso: {cluster_compromiso}"
72
  print(f"\tPeso inicial: {peso_inicial}, peso final: {peso_final} --> Diferencia: {variacion_peso:.1f}")
73
+ logs += f"\n\tPeso inicial: {peso_inicial}, peso final: {peso_final} --> Diferencia: {variacion_peso:.1f}"
74
  print(f"\t{diff_peso_min} <= Diferencia peso <= {diff_peso_max}")
75
+ logs += f"\n\t{diff_peso_min} <= Diferencia peso <= {diff_peso_max}"
76
 
77
  # Crear query
78
  query = make_query(cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta, cluster_compromiso, diff_peso_min, diff_peso_max)
79
 
80
  # Print query
81
  print(f"Query:")
82
+ logs += f"\n\nQuery:"
83
  for q in query:
84
  print(f"\t{list(q.keys())[0]}", end=" --> ")
85
+ logs += f"\n\t{list(q.keys())[0]} --> "
86
  for primary_key in q.keys():
87
  primary_key_dict = q[primary_key]
88
  for i, secondary_key in enumerate(primary_key_dict.keys()):
89
  if i == len(primary_key_dict.keys()) - 1:
90
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end="")
91
+ logs += f"{secondary_key}: \"{primary_key_dict[secondary_key]}\""
92
  else:
93
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end=", ")
94
+ logs += f"{secondary_key}: \"{primary_key_dict[secondary_key]}\", "
95
  print()
96
 
97
  # Crear diccionario de matches
 
101
  # Si no hay matches, intentamos con una query más básica
102
  if num_matches == 0:
103
  print("No se han encontrado coincidencias con la query original, intentamos con una query más básica")
104
+ logs += "\n\nNo se han encontrado coincidencias con la query original, intentamos con una query más básica"
105
  query = make_query(cluster_esfuerzo_dieta, cluster_objetivo, cluster_entrenamiento, cluster_cumplimiento_dieta, cluster_compromiso, diff_peso_min, diff_peso_max, basic_query=True)
106
 
107
  # Print query
108
  print(f"Query:")
109
+ logs += "\n\nQuery:"
110
  for q in query:
111
  print(f"\t{list(q.keys())[0]}", end=" --> ")
112
+ logs += f"\n\t{list(q.keys())[0]} --> "
113
  for primary_key in q.keys():
114
  primary_key_dict = q[primary_key]
115
  for i, secondary_key in enumerate(primary_key_dict.keys()):
116
  if i == len(primary_key_dict.keys()) - 1:
117
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end="")
118
+ logs += f"{secondary_key}: \"{primary_key_dict[secondary_key]}\""
119
  else:
120
  print(f"{secondary_key}: \"{primary_key_dict[secondary_key]}\"", end=", ")
121
+ logs += f"{secondary_key}: \"{primary_key_dict[secondary_key]}\", "
122
+ print()
123
+ logs += "\n"
124
+
125
+ # Find matches
126
  matches_dict = find_user_dates_matches(query)
127
  num_matches = len(matches_dict)
128
 
129
  # Print matches
130
  print(f"Matches ({num_matches})")
131
+ logs += f"\n\nMatches ({num_matches})"
132
  for user, dates in matches_dict.items():
133
  print(f"\t{user}", end=" --> ")
134
+ logs += f"\t{user} --> "
135
  for date in dates:
136
  print(f"{date}", end=", ")
137
+ logs += f"{date}, "
138
  print()
139
+ logs += "\n"
140
 
141
  # Find macros that match dates of users
142
  macros_differences_list, macros_differences_dict = find_macros_that_match_dates_of_users(matches_dict)
143
 
144
  # Print macros
145
  print(f"Diferencia de macros ({len(macros_differences_dict)}):")
146
+ logs += f"\n\nDiferencia de macros ({len(macros_differences_dict)}):"
147
  for user, macros_difference in macros_differences_dict.items():
148
  print(f"\t{user} --> fecha de match: {macros_difference['date_of_match']}, fecha de asignación de macros: {macros_difference['date_of_macros_asignation']}, días entre match y asignación: {macros_difference['days_between_match_and_macros_asignation']}, diferencia de macros: {macros_difference['macros_difference']}")
149
+ logs += f"\n\t{user} --> fecha de match: {macros_difference['date_of_match']}, fecha de asignación de macros: {macros_difference['date_of_macros_asignation']}, días entre match y asignación: {macros_difference['days_between_match_and_macros_asignation']}, diferencia de macros: {macros_difference['macros_difference']}"
150
 
151
  # Calculate macros min, max and mean
152
  if len(macros_differences_list) > 0:
 
164
 
165
  # Print macros min, max and mean
166
  print("Variación de macros:")
167
+ logs += "\n\nVariación de macros:"
168
  print(f"\tproteína día de entreno: 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]}")
169
+ logs += f"\n\tproteína día de entreno: 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]}"
170
  print(f"\tcarbohidratos día de entreno: 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]}")
171
+ logs += f"\n\tcarbohidratos día de entreno: 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]}"
172
  print(f"\tgrasas día de entreno: 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]}")
173
+ logs += f"\n\tgrasas día de entreno: 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]}"
174
  print(f"\tproteína intraentreno: Min: {intratrain_protein_std[0]}, Max: {intratrain_protein_std[1]}, Mean: {intratrain_protein_std[2]:.2f}, Mode: {intratrain_protein_std[3]}")
175
+ logs += f"\n\tproteína intraentreno: Min: {intratrain_protein_std[0]}, Max: {intratrain_protein_std[1]}, Mean: {intratrain_protein_std[2]:.2f}, Mode: {intratrain_protein_std[3]}"
176
  print(f"\tcarbohidratos intraentreno: Min: {intratrain_carbs_std[0]}, Max: {intratrain_carbs_std[1]}, Mean: {intratrain_carbs_std[2]:.2f}, Mode: {intratrain_carbs_std[3]}")
177
+ logs += f"\n\tcarbohidratos intraentreno: Min: {intratrain_carbs_std[0]}, Max: {intratrain_carbs_std[1]}, Mean: {intratrain_carbs_std[2]:.2f}, Mode: {intratrain_carbs_std[3]}"
178
  print(f"\tproteína día de descanso: 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]}")
179
+ logs += f"\n\tproteína día de descanso: 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]}"
180
  print(f"\tcarbohidratos día de descanso: 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]}")
181
+ logs += f"\n\tcarbohidratos día de descanso: 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]}"
182
  print(f"\tgrasas día de descanso: 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]}")
183
+ logs += f"\n\tgrasas día de descanso: 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]}"
184
 
185
  # Get macros mode
186
  train_day_protein_mode = train_day_protein_std[3]
 
204
 
205
  # Print macros
206
  print("Macros finales:")
207
+ logs += "\n\nMacros finales:"
208
  print(f"\tProteína día de entreno inicial: {train_day_protein_initial} --> final: {train_day_protein_final}")
209
+ logs += f"\n\tProteína día de entreno inicial: {train_day_protein_initial} --> final: {train_day_protein_final}"
210
  print(f"\tCarbohidratos día de entreno inicial: {train_day_carbs_initial} --> final: {train_day_carbs_final}")
211
+ logs += f"\n\tCarbohidratos día de entreno inicial: {train_day_carbs_initial} --> final: {train_day_carbs_final}"
212
  print(f"\tGrasas día de entreno inicial: {train_day_fat_initial} --> final: {train_day_fat_final}")
213
+ logs += f"\n\tGrasas día de entreno inicial: {train_day_fat_initial} --> final: {train_day_fat_final}"
214
  print(f"\tProteína intraentreno inicial: {intratrain_protein_initial} --> final: {intratrain_protein_final}")
215
+ logs += f"\n\tProteína intraentreno inicial: {intratrain_protein_initial} --> final: {intratrain_protein_final}"
216
  print(f"\tCarbohidratos intraentreno inicial: {intratrain_carbs_initial} --> final: {intratrain_carbs_final}")
217
+ logs += f"\n\tCarbohidratos intraentreno inicial: {intratrain_carbs_initial} --> final: {intratrain_carbs_final}"
218
  print(f"\tProteína día de descanso inicial: {rest_day_protein_initial} --> final: {rest_day_protein_final}")
219
+ logs += f"\n\tProteína día de descanso inicial: {rest_day_protein_initial} --> final: {rest_day_protein_final}"
220
  print(f"\tCarbohidratos día de descanso inicial: {rest_day_carbs_initial} --> final: {rest_day_carbs_final}")
221
+ logs += f"\n\tCarbohidratos día de descanso inicial: {rest_day_carbs_initial} --> final: {rest_day_carbs_final}"
222
  print(f"\tGrasas día de descanso inicial: {rest_day_fat_initial} --> final: {rest_day_fat_final}")
223
+ logs += f"\n\tGrasas día de descanso inicial: {rest_day_fat_initial} --> final: {rest_day_fat_final}"
224
 
225
  return (train_day_protein_final, train_day_carbs_final, train_day_fat_final,
226
  intratrain_protein_final, intratrain_carbs_final,
227
+ rest_day_protein_final, rest_day_carbs_final, rest_day_fat_final,
228
+ logs)
229
 
230
  # Definimos el color naranja
231
  naranja = "#ea580b"
 
440
  rest_day_carbs_final = gr.Number(label="Carbohidratos día de descanso Final (g)", precision=0, elem_classes="output-number")
441
  rest_day_fat_final = gr.Number(label="Grasas día de descanso Final (g)", precision=0, elem_classes="output-number")
442
 
443
+ # Logs
444
+ logs = gr.Textbox(label="Logs", lines=10, elem_classes="output-textbox", visible=False)
445
+
446
  # Conectamos el botón con la función
447
  calcular_btn.click(
448
  fn=calcular_macros,
 
453
  rest_day_protein_initial, rest_day_carbs_initial, rest_day_fat_initial],
454
  outputs=[train_day_protein_final, train_day_carbs_final, train_day_fat_final,
455
  intratrain_protein_final, intratrain_carbs_final,
456
+ rest_day_protein_final, rest_day_carbs_final, rest_day_fat_final,
457
+ logs]
458
  )
459
 
460
  demo.launch()