Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ except ImportError:
|
|
| 8 |
docx = None
|
| 9 |
|
| 10 |
# ------------------------------
|
| 11 |
-
#
|
| 12 |
# ------------------------------
|
| 13 |
DEFAULT_CODES = [
|
| 14 |
"Theme: Communication Barrier",
|
|
@@ -54,7 +54,7 @@ def read_vtt(path):
|
|
| 54 |
|
| 55 |
def get_empty_df():
|
| 56 |
return pd.DataFrame(
|
| 57 |
-
columns=["File ID", "
|
| 58 |
)
|
| 59 |
|
| 60 |
# ------------------------------
|
|
@@ -77,18 +77,16 @@ def process_file(file_obj):
|
|
| 77 |
# ------------------------------
|
| 78 |
# BUILD HTML TRANSCRIPT
|
| 79 |
# ------------------------------
|
| 80 |
-
def
|
| 81 |
display_text = text
|
| 82 |
if coded_df is not None and not coded_df.empty:
|
| 83 |
for _, row in coded_df.iterrows():
|
| 84 |
seg = row["Coded Segment"]
|
| 85 |
color = COLOR_MAP.get(row["Code"], "yellow")
|
| 86 |
display_text = display_text.replace(seg, f"<span style='background-color:{color}'>{seg}</span>", 1)
|
| 87 |
-
|
| 88 |
safe_text = display_text.replace("\n", "<br>")
|
| 89 |
-
|
| 90 |
html = f"""
|
| 91 |
-
<div style='white-space: pre-wrap; font-size:16px; line-height:1.5; max-height:600px; overflow:auto; border:1px solid #ccc; padding:5px;'
|
| 92 |
{safe_text}
|
| 93 |
</div>
|
| 94 |
<script>
|
|
@@ -153,10 +151,10 @@ def export_excel(df):
|
|
| 153 |
return path, "Excel ready."
|
| 154 |
|
| 155 |
# ------------------------------
|
| 156 |
-
# GRADIO
|
| 157 |
# ------------------------------
|
| 158 |
-
with gr.Blocks(title="
|
| 159 |
-
gr.Markdown("# 📑
|
| 160 |
|
| 161 |
# States
|
| 162 |
file_id = gr.State("")
|
|
@@ -178,10 +176,10 @@ with gr.Blocks(title="Interactive Qualitative Coding") as demo:
|
|
| 178 |
|
| 179 |
# RIGHT: coding panel
|
| 180 |
with gr.Column(scale=2):
|
| 181 |
-
gr.Markdown("## 🏷️ Code Categories
|
| 182 |
code_input = gr.Textbox(label="Enter code or select from dropdown", value="", interactive=True)
|
| 183 |
code_dropdown = gr.Dropdown(label="Select existing code", choices=DEFAULT_CODES)
|
| 184 |
-
segment_box = gr.Textbox(label="Selected Segment", placeholder="Selected segment", lines=3)
|
| 185 |
code_btn = gr.Button("Apply Code")
|
| 186 |
|
| 187 |
gr.Markdown("## 📊 Coded Segments")
|
|
@@ -198,7 +196,7 @@ with gr.Blocks(title="Interactive Qualitative Coding") as demo:
|
|
| 198 |
|
| 199 |
# Update transcript when text or coded_df changes
|
| 200 |
def update_transcript(text, df):
|
| 201 |
-
return
|
| 202 |
|
| 203 |
full_text.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
|
| 204 |
coded_df_state.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
|
|
|
|
| 8 |
docx = None
|
| 9 |
|
| 10 |
# ------------------------------
|
| 11 |
+
# CONFIGURATION
|
| 12 |
# ------------------------------
|
| 13 |
DEFAULT_CODES = [
|
| 14 |
"Theme: Communication Barrier",
|
|
|
|
| 54 |
|
| 55 |
def get_empty_df():
|
| 56 |
return pd.DataFrame(
|
| 57 |
+
columns=["File ID", "Coded Segment", "Code", "Context (100 chars)"] + list(METADATA_FIELDS.keys())
|
| 58 |
)
|
| 59 |
|
| 60 |
# ------------------------------
|
|
|
|
| 77 |
# ------------------------------
|
| 78 |
# BUILD HTML TRANSCRIPT
|
| 79 |
# ------------------------------
|
| 80 |
+
def build_transcript_html(text, coded_df):
|
| 81 |
display_text = text
|
| 82 |
if coded_df is not None and not coded_df.empty:
|
| 83 |
for _, row in coded_df.iterrows():
|
| 84 |
seg = row["Coded Segment"]
|
| 85 |
color = COLOR_MAP.get(row["Code"], "yellow")
|
| 86 |
display_text = display_text.replace(seg, f"<span style='background-color:{color}'>{seg}</span>", 1)
|
|
|
|
| 87 |
safe_text = display_text.replace("\n", "<br>")
|
|
|
|
| 88 |
html = f"""
|
| 89 |
+
<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;'>
|
| 90 |
{safe_text}
|
| 91 |
</div>
|
| 92 |
<script>
|
|
|
|
| 151 |
return path, "Excel ready."
|
| 152 |
|
| 153 |
# ------------------------------
|
| 154 |
+
# GRADIO INTERFACE
|
| 155 |
# ------------------------------
|
| 156 |
+
with gr.Blocks(title="Direct Selection Coding") as demo:
|
| 157 |
+
gr.Markdown("# 📑 Direct Selection Coding Tool")
|
| 158 |
|
| 159 |
# States
|
| 160 |
file_id = gr.State("")
|
|
|
|
| 176 |
|
| 177 |
# RIGHT: coding panel
|
| 178 |
with gr.Column(scale=2):
|
| 179 |
+
gr.Markdown("## 🏷️ Code Categories")
|
| 180 |
code_input = gr.Textbox(label="Enter code or select from dropdown", value="", interactive=True)
|
| 181 |
code_dropdown = gr.Dropdown(label="Select existing code", choices=DEFAULT_CODES)
|
| 182 |
+
segment_box = gr.Textbox(label="Selected Segment", placeholder="Selected segment", lines=3, interactive=False)
|
| 183 |
code_btn = gr.Button("Apply Code")
|
| 184 |
|
| 185 |
gr.Markdown("## 📊 Coded Segments")
|
|
|
|
| 196 |
|
| 197 |
# Update transcript when text or coded_df changes
|
| 198 |
def update_transcript(text, df):
|
| 199 |
+
return build_transcript_html(text, df)
|
| 200 |
|
| 201 |
full_text.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
|
| 202 |
coded_df_state.change(update_transcript, inputs=[full_text, coded_df_state], outputs=transcript_html)
|