Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 118 |
# ------------------------------
|
| 119 |
-
def
|
| 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
|
| 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
|
| 155 |
with gr.Column(scale=3):
|
| 156 |
transcript_html = gr.HTML()
|
| 157 |
-
|
| 158 |
|
| 159 |
-
# Right
|
| 160 |
with gr.Column(scale=2):
|
| 161 |
-
gr.Markdown("## 🏷️
|
| 162 |
-
|
| 163 |
-
|
|
|
|
| 164 |
|
| 165 |
-
gr.
|
| 166 |
-
|
|
|
|
| 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
|
| 186 |
-
add_code_btn.click(
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 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
|