clementBE commited on
Commit
1359c1b
·
verified ·
1 Parent(s): c0f65ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -69
app.py CHANGED
@@ -65,38 +65,6 @@ def process_file(file_obj):
65
  text = f.read()
66
  return text, name, get_empty_df()
67
 
68
- def build_transcript_html(text, df):
69
- display_text = text
70
- if df is not None and not df.empty:
71
- for _, row in df.iterrows():
72
- seg = row["Coded Segment"]
73
- color = COLOR_MAP.get(row["Code"], "yellow")
74
- display_text = display_text.replace(
75
- seg, f"<span style='background-color:{color}'>{seg}</span>", 1
76
- )
77
- safe_text = display_text.replace("\n", "<br>")
78
- html = f"""
79
- <div id='transcript' style='white-space: pre-wrap; font-size:16px; line-height:1.5; max-height:600px; overflow:auto; border:1px solid #ccc; padding:5px;'>
80
- {safe_text}
81
- </div>
82
- <script>
83
- document.addEventListener('DOMContentLoaded', function() {{
84
- const transcript = document.getElementById('transcript');
85
- transcript.addEventListener('mouseup', function() {{
86
- const sel = window.getSelection().toString();
87
- if(sel.length>0){{
88
- const state_input = document.getElementById('selected_segment_state');
89
- if(state_input) {{
90
- state_input.value = sel;
91
- state_input.dispatchEvent(new Event("input", {{bubbles:true}}));
92
- }}
93
- }}
94
- }});
95
- }});
96
- </script>
97
- """
98
- return html
99
-
100
  def apply_code(df, segment, code, file_id, *metadata_values):
101
  if not file_id:
102
  return df, "⚠️ Upload a file first"
@@ -121,13 +89,14 @@ def export_excel(df):
121
  df.to_excel(path, index=False)
122
  return path, "Excel ready"
123
 
 
 
 
124
  with gr.Blocks() as demo:
125
 
126
- # States
127
  full_text = gr.State("")
128
  file_id = gr.State("")
129
  coded_df_state = gr.State(get_empty_df())
130
- selected_segment_state = gr.State("")
131
  code_categories_state = gr.State(DEFAULT_CODES)
132
 
133
  # Metadata
@@ -136,22 +105,23 @@ with gr.Blocks() as demo:
136
  for k, lbl in METADATA_FIELDS.items():
137
  metadata_inputs.append(gr.Textbox(label=lbl))
138
 
139
- # Main row: transcript left, coding right
140
  with gr.Row():
141
- # Left: transcript
142
  with gr.Column(scale=3):
143
- transcript_html = gr.HTML()
144
- hidden_segment = gr.Textbox(
145
- interactive=False, visible=False, elem_id="selected_segment_state"
 
 
146
  )
147
 
148
- # Right: controls
149
  with gr.Column(scale=2):
150
  gr.Markdown("## 🏷️ Code Category")
151
  code_dropdown = gr.Dropdown(label="Select code", choices=DEFAULT_CODES)
152
  code_input = gr.Textbox(label="Or type new code")
153
  add_code_btn = gr.Button("Add new code")
154
-
155
  apply_btn = gr.Button("Apply code to selected text")
156
 
157
  gr.Markdown("## 📊 Coded Segments")
@@ -160,48 +130,26 @@ with gr.Blocks() as demo:
160
  export_btn = gr.Button("Export XLSX")
161
  export_file = gr.File(visible=False)
162
 
163
- file_input = gr.File(
164
- label="Upload transcript", file_types=[".docx", ".vtt", ".txt"]
165
- )
166
  status = gr.Textbox(label="Status", value="Ready")
167
 
168
  # Callbacks
169
- file_input.change(
170
- fn=process_file, inputs=file_input, outputs=[full_text, file_id, coded_df_state]
171
- )
172
-
173
- def update_transcript(text, df):
174
- return build_transcript_html(text, df)
175
 
176
- full_text.change(
177
- update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html
178
- )
179
- coded_df_state.change(
180
- update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html
181
- )
182
-
183
- add_code_btn.click(
184
- add_new_code, inputs=[code_input, code_categories_state], outputs=[code_categories_state]
185
- )
186
- code_categories_state.change(
187
- lambda codes: gr.update(choices=codes),
188
- inputs=code_categories_state,
189
- outputs=code_dropdown,
190
- )
191
 
192
- # Apply button uses hidden textbox as input
193
  apply_btn.click(
194
  apply_code,
195
- inputs=[coded_df_state, hidden_segment, code_dropdown, file_id] + metadata_inputs,
196
  outputs=[coded_df_state, status],
197
  )
198
 
199
  coded_df_state.change(lambda df: df, inputs=coded_df_state, outputs=table)
200
-
201
  export_btn.click(export_excel, inputs=coded_df_state, outputs=[export_file, status]).then(
202
  lambda f: gr.update(visible=f is not None),
203
  inputs=export_file,
204
- outputs=export_file,
205
  )
206
 
207
  demo.launch()
 
65
  text = f.read()
66
  return text, name, get_empty_df()
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def apply_code(df, segment, code, file_id, *metadata_values):
69
  if not file_id:
70
  return df, "⚠️ Upload a file first"
 
89
  df.to_excel(path, index=False)
90
  return path, "Excel ready"
91
 
92
+ # ----------------------------
93
+ # GRADIO APP
94
+ # ----------------------------
95
  with gr.Blocks() as demo:
96
 
 
97
  full_text = gr.State("")
98
  file_id = gr.State("")
99
  coded_df_state = gr.State(get_empty_df())
 
100
  code_categories_state = gr.State(DEFAULT_CODES)
101
 
102
  # Metadata
 
105
  for k, lbl in METADATA_FIELDS.items():
106
  metadata_inputs.append(gr.Textbox(label=lbl))
107
 
108
+ # Main interface
109
  with gr.Row():
110
+ # Left: transcript selection
111
  with gr.Column(scale=3):
112
+ transcript_text = gr.Textbox(
113
+ label="Transcript (Select text below to code)",
114
+ lines=25,
115
+ interactive=True,
116
+ placeholder="Select a segment of this text..."
117
  )
118
 
119
+ # Right: coding
120
  with gr.Column(scale=2):
121
  gr.Markdown("## 🏷️ Code Category")
122
  code_dropdown = gr.Dropdown(label="Select code", choices=DEFAULT_CODES)
123
  code_input = gr.Textbox(label="Or type new code")
124
  add_code_btn = gr.Button("Add new code")
 
125
  apply_btn = gr.Button("Apply code to selected text")
126
 
127
  gr.Markdown("## 📊 Coded Segments")
 
130
  export_btn = gr.Button("Export XLSX")
131
  export_file = gr.File(visible=False)
132
 
133
+ file_input = gr.File(label="Upload transcript", file_types=[".docx", ".vtt", ".txt"])
 
 
134
  status = gr.Textbox(label="Status", value="Ready")
135
 
136
  # Callbacks
137
+ file_input.change(fn=process_file, inputs=file_input, outputs=[transcript_text, file_id, coded_df_state])
 
 
 
 
 
138
 
139
+ add_code_btn.click(add_new_code, inputs=[code_input, code_categories_state], outputs=[code_categories_state])
140
+ code_categories_state.change(lambda codes: gr.update(choices=codes), inputs=code_categories_state, outputs=code_dropdown)
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
 
142
  apply_btn.click(
143
  apply_code,
144
+ inputs=[coded_df_state, transcript_text, code_dropdown, file_id] + metadata_inputs,
145
  outputs=[coded_df_state, status],
146
  )
147
 
148
  coded_df_state.change(lambda df: df, inputs=coded_df_state, outputs=table)
 
149
  export_btn.click(export_excel, inputs=coded_df_state, outputs=[export_file, status]).then(
150
  lambda f: gr.update(visible=f is not None),
151
  inputs=export_file,
152
+ outputs=export_file
153
  )
154
 
155
  demo.launch()