clementBE commited on
Commit
ad6113a
·
verified ·
1 Parent(s): af98792

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -19
app.py CHANGED
@@ -10,7 +10,7 @@ except ImportError:
10
  # ------------------------------
11
  # CONFIG
12
  # ------------------------------
13
- CODES = [
14
  "Communication Barrier",
15
  "Emotional Support",
16
  "Future Aspirations",
@@ -91,11 +91,10 @@ def build_transcript_html(text, df):
91
  transcript.addEventListener('mouseup', function() {{
92
  const sel = window.getSelection().toString();
93
  if(sel.length>0){{
94
- // store in hidden input
95
  const state_input = document.querySelector('#selected_segment_state');
96
  if(state_input) {{
97
  state_input.value = sel;
98
- state_input.dispatchEvent(new Event("input",{ {bubbles:true} }));
99
  }}
100
  }}
101
  }});
@@ -114,6 +113,14 @@ def apply_code(df, segment, code, file_id, *metadata_values):
114
  df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
115
  return df, f"✅ Segment coded as '{code}'"
116
 
 
 
 
 
 
 
 
 
117
  # ------------------------------
118
  # EXPORT XLSX
119
  # ------------------------------
@@ -134,6 +141,7 @@ with gr.Blocks() as demo:
134
  file_id = gr.State("")
135
  coded_df_state = gr.State(get_empty_df())
136
  selected_segment_state = gr.State("")
 
137
 
138
  # ---------------- Metadata Top ----------------
139
  with gr.Row():
@@ -146,17 +154,16 @@ with gr.Blocks() as demo:
146
  # Left: transcript
147
  with gr.Column(scale=3):
148
  transcript_html = gr.HTML()
149
- # Hidden state to store selected segment
150
- selected_segment = gr.Textbox(label="Selected segment (hidden)", interactive=False, visible=False, elem_id="selected_segment_state")
151
 
152
  # Right: code buttons + table
153
  with gr.Column(scale=2):
154
- gr.Markdown("## 🏷️ Code Categories")
155
- code_buttons = []
156
- for c in CODES:
157
- btn = gr.Button(c)
158
- code_buttons.append(btn)
159
- gr.Markdown("## 📊 Coded Segments")
160
  table = gr.Dataframe(interactive=False)
161
 
162
  export_btn = gr.Button("Export XLSX")
@@ -175,15 +182,26 @@ with gr.Blocks() as demo:
175
  full_text.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
176
  coded_df_state.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  # Apply code buttons
179
- for btn, code_name in zip(code_buttons, CODES):
180
- btn.click(
181
- apply_code,
182
- inputs=[coded_df_state, selected_segment_state, gr.State(code_name), file_id] + metadata_inputs,
183
- outputs=[coded_df_state, status]
184
- )
185
-
186
- # Update table
187
  coded_df_state.change(lambda df: df, inputs=coded_df_state, outputs=table)
188
 
189
  # Export
 
10
  # ------------------------------
11
  # CONFIG
12
  # ------------------------------
13
+ DEFAULT_CODES = [
14
  "Communication Barrier",
15
  "Emotional Support",
16
  "Future Aspirations",
 
91
  transcript.addEventListener('mouseup', function() {{
92
  const sel = window.getSelection().toString();
93
  if(sel.length>0){{
 
94
  const state_input = document.querySelector('#selected_segment_state');
95
  if(state_input) {{
96
  state_input.value = sel;
97
+ state_input.dispatchEvent(new Event("input", {{bubbles:true}}));
98
  }}
99
  }}
100
  }});
 
113
  df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
114
  return df, f"✅ Segment coded as '{code}'"
115
 
116
+ # ------------------------------
117
+ # ADD NEW CODE CATEGORY
118
+ # ------------------------------
119
+ def add_new_category(new_code, code_list):
120
+ if new_code and new_code not in code_list:
121
+ code_list.append(new_code)
122
+ return code_list
123
+
124
  # ------------------------------
125
  # EXPORT XLSX
126
  # ------------------------------
 
141
  file_id = gr.State("")
142
  coded_df_state = gr.State(get_empty_df())
143
  selected_segment_state = gr.State("")
144
+ code_categories_state = gr.State(DEFAULT_CODES)
145
 
146
  # ---------------- Metadata Top ----------------
147
  with gr.Row():
 
154
  # Left: transcript
155
  with gr.Column(scale=3):
156
  transcript_html = gr.HTML()
157
+ selected_segment_hidden = gr.Textbox(label="hidden", interactive=False, visible=False, elem_id="selected_segment_state")
 
158
 
159
  # Right: code buttons + table
160
  with gr.Column(scale=2):
161
+ gr.Markdown("## 🏷️ Add New Code Category")
162
+ new_code_input = gr.Textbox(label="New code name")
163
+ add_code_btn = gr.Button("Add Code Category")
164
+
165
+ gr.Markdown("## 🏷️ Code Categories (Click to apply)")
166
+ code_buttons_box = gr.Column()
167
  table = gr.Dataframe(interactive=False)
168
 
169
  export_btn = gr.Button("Export XLSX")
 
182
  full_text.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
183
  coded_df_state.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
184
 
185
+ # Add new code category
186
+ add_code_btn.click(add_new_category, inputs=[new_code_input, code_categories_state], outputs=[code_categories_state])
187
+
188
+ # Dynamically create code buttons
189
+ def create_buttons(codes):
190
+ buttons = []
191
+ for c in codes:
192
+ b = gr.Button(c)
193
+ buttons.append(b)
194
+ return buttons
195
+ code_buttons_state = gr.State()
196
+ def update_buttons(codes):
197
+ return gr.update(value=codes)
198
+ code_categories_state.change(update_buttons, inputs=code_categories_state, outputs=code_buttons_state)
199
+
200
  # Apply code buttons
201
+ def make_apply_func(code_name):
202
+ return lambda df, segment, fid, *metas: apply_code(df, segment, code_name, fid, *metas)
203
+
204
+ # Table update
 
 
 
 
205
  coded_df_state.change(lambda df: df, inputs=coded_df_state, outputs=table)
206
 
207
  # Export