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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -32
app.py CHANGED
@@ -114,9 +114,9 @@ def apply_code(df, segment, code, file_id, *metadata_values):
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
@@ -143,7 +143,7 @@ with gr.Blocks() as demo:
143
  selected_segment_state = gr.State("")
144
  code_categories_state = gr.State(DEFAULT_CODES)
145
 
146
- # ---------------- Metadata Top ----------------
147
  with gr.Row():
148
  metadata_inputs = []
149
  for k,lbl in METADATA_FIELDS.items():
@@ -151,19 +151,21 @@ with gr.Blocks() as demo:
151
 
152
  # ---------------- Transcript + Coding ----------------
153
  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")
@@ -173,7 +175,6 @@ with gr.Blocks() as demo:
173
  status = gr.Textbox(label="Status", value="Ready")
174
 
175
  # ---------------- Callbacks ----------------
176
- # Load file
177
  file_input.change(fn=process_file, inputs=file_input, outputs=[full_text, file_id, coded_df_state])
178
 
179
  # Update transcript HTML
@@ -182,26 +183,14 @@ with gr.Blocks() as demo:
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
 
114
  return df, f"✅ Segment coded as '{code}'"
115
 
116
  # ------------------------------
117
+ # ADD NEW CODE TO DROPDOWN
118
  # ------------------------------
119
+ def add_new_code(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
 
143
  selected_segment_state = gr.State("")
144
  code_categories_state = gr.State(DEFAULT_CODES)
145
 
146
+ # ---------------- Metadata ----------------
147
  with gr.Row():
148
  metadata_inputs = []
149
  for k,lbl in METADATA_FIELDS.items():
 
151
 
152
  # ---------------- Transcript + Coding ----------------
153
  with gr.Row():
154
+ # Left
155
  with gr.Column(scale=3):
156
  transcript_html = gr.HTML()
157
+ hidden_segment = gr.Textbox(interactive=False, visible=False, elem_id="selected_segment_state")
158
 
159
+ # Right
160
  with gr.Column(scale=2):
161
+ gr.Markdown("## 🏷️ Code Category")
162
+ code_dropdown = gr.Dropdown(label="Select code", choices=DEFAULT_CODES)
163
+ code_input = gr.Textbox(label="Or type new code")
164
+ add_code_btn = gr.Button("Add new code")
165
 
166
+ apply_btn = gr.Button("Apply code to selected text")
167
+
168
+ gr.Markdown("## 📊 Coded Segments")
169
  table = gr.Dataframe(interactive=False)
170
 
171
  export_btn = gr.Button("Export XLSX")
 
175
  status = gr.Textbox(label="Status", value="Ready")
176
 
177
  # ---------------- Callbacks ----------------
 
178
  file_input.change(fn=process_file, inputs=file_input, outputs=[full_text, file_id, coded_df_state])
179
 
180
  # Update transcript HTML
 
183
  full_text.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
184
  coded_df_state.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
185
 
186
+ # Add new code
187
+ add_code_btn.click(add_new_code, inputs=[code_input, code_categories_state], outputs=[code_categories_state])
188
+ code_categories_state.change(lambda codes: gr.update(choices=codes), inputs=code_categories_state, outputs=code_dropdown)
189
+
190
+ # Apply code
191
+ apply_btn.click(apply_code, inputs=[coded_df_state, selected_segment_state, code_dropdown, file_id] + metadata_inputs, outputs=[coded_df_state, status])
192
+
193
+ # Update table
 
 
 
 
 
 
 
 
 
 
 
 
194
  coded_df_state.change(lambda df: df, inputs=coded_df_state, outputs=table)
195
 
196
  # Export