sdbrgo commited on
Commit
7fc7cc1
Β·
verified Β·
1 Parent(s): 89e885d

updated display on interface

Browse files
Files changed (1) hide show
  1. app.py +27 -6
app.py CHANGED
@@ -26,7 +26,26 @@ def make_json_safe(obj):
26
  else:
27
  return obj
28
 
29
- # insert format_deviations_as_columns()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  def final_clustering(file, top_features):
32
  try:
@@ -66,13 +85,14 @@ def final_clustering(file, top_features):
66
  feature_names
67
  )
68
  top_drivers = identify_top_drivers(original_centroids, top_features)
69
- top_drivers_safe = make_json_safe(top_drivers)
 
70
 
71
  # debug
72
  print("βœ… Best K:", best_k, type(best_k))
73
  print("πŸ“Š Drivers sample:", top_drivers)
74
 
75
- return best_k, top_drivers_safe
76
 
77
  except Exception as e:
78
  print("πŸ”₯ ERROR IN final_clustering πŸ”₯")
@@ -89,7 +109,7 @@ with gr.Blocks(title="PERCEUL: Perception-Based Worker Profiler") as app:
89
  file_input_final = gr.File(label="Upload CSV")
90
  top_features = gr.Number(
91
  value=5,
92
- label="Show Top `n` Features",
93
  minimum=3,
94
  maximum=10,
95
  step=1,
@@ -98,12 +118,13 @@ with gr.Blocks(title="PERCEUL: Perception-Based Worker Profiler") as app:
98
 
99
  run_btn = gr.Button("Run Final Clustering")
100
  best_k_out = gr.Number(label="Selected K", interactive=False, precision=0)
101
- drivers_out = gr.JSON(label="Top Feature Drivers per Cluster")
 
102
 
103
  run_btn.click(
104
  final_clustering,
105
  inputs=[file_input_final, top_features],
106
- outputs=[best_k_out, drivers_out]
107
  )
108
 
109
  app.launch()
 
26
  else:
27
  return obj
28
 
29
+ def format_deviations_as_columns(drivers):
30
+ headers = []
31
+ cells = []
32
+
33
+ for cid, data in drivers.items():
34
+ headers.append(f"Cluster {cid + 1}")
35
+
36
+ dev_lines = [
37
+ f"{feature}: {value:.3f}"
38
+ for feature, value in data["deviations"].items()
39
+ ]
40
+
41
+ cells.append("<br>".join(dev_lines))
42
+
43
+ table = "| " + " | ".join(headers) + " |\n"
44
+ table += "|" + "|".join(["---"] * len(headers)) + "|\n"
45
+ table += "| " + " | ".join(cells) + " |"
46
+
47
+ return table
48
+
49
 
50
  def final_clustering(file, top_features):
51
  try:
 
85
  feature_names
86
  )
87
  top_drivers = identify_top_drivers(original_centroids, top_features)
88
+ deviations_markdown = format_deviations_as_columns(top_drivers)
89
+ # top_drivers_safe = make_json_safe(top_drivers)
90
 
91
  # debug
92
  print("βœ… Best K:", best_k, type(best_k))
93
  print("πŸ“Š Drivers sample:", top_drivers)
94
 
95
+ return best_k, deviations_markdown
96
 
97
  except Exception as e:
98
  print("πŸ”₯ ERROR IN final_clustering πŸ”₯")
 
109
  file_input_final = gr.File(label="Upload CSV")
110
  top_features = gr.Number(
111
  value=5,
112
+ label="Number of Features to Display",
113
  minimum=3,
114
  maximum=10,
115
  step=1,
 
118
 
119
  run_btn = gr.Button("Run Final Clustering")
120
  best_k_out = gr.Number(label="Selected K", interactive=False, precision=0)
121
+ gr.Markdown("### Cluster Characteristics")
122
+ deviations_out = gr.Markdown()
123
 
124
  run_btn.click(
125
  final_clustering,
126
  inputs=[file_input_final, top_features],
127
+ outputs=[best_k_out, deviations_out]
128
  )
129
 
130
  app.launch()