Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,9 @@ try:
|
|
| 7 |
except ImportError:
|
| 8 |
docx = None
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
-
#
|
| 13 |
|
| 14 |
DEFAULT_CODES = [
|
| 15 |
"Theme: Communication Barrier",
|
|
@@ -26,10 +26,9 @@ METADATA_FIELDS = {
|
|
| 26 |
"age": "Participant Age",
|
| 27 |
}
|
| 28 |
|
| 29 |
-
|
| 30 |
-
# ----------------------------
|
| 31 |
# FILE READERS
|
| 32 |
-
#
|
| 33 |
|
| 34 |
def read_docx(file_path):
|
| 35 |
if not docx:
|
|
@@ -55,10 +54,9 @@ def get_initial_df():
|
|
| 55 |
cols = ["File ID", "Code", "Coded Segment", "Context (100 chars)"] + list(METADATA_FIELDS.keys())
|
| 56 |
return pd.DataFrame(columns=cols)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
# ----------------------------
|
| 60 |
# FILE PROCESSING
|
| 61 |
-
#
|
| 62 |
|
| 63 |
def process_file(file_obj):
|
| 64 |
if file_obj is None:
|
|
@@ -77,10 +75,9 @@ def process_file(file_obj):
|
|
| 77 |
|
| 78 |
return text, f"Loaded: {filename}", filename, get_initial_df()
|
| 79 |
|
| 80 |
-
|
| 81 |
-
#
|
| 82 |
-
#
|
| 83 |
-
# ----------------------------
|
| 84 |
|
| 85 |
def apply_code(coded_df, file_id, full_text, segment_text, selected_code, *metadata_values):
|
| 86 |
if not segment_text or not selected_code:
|
|
@@ -109,10 +106,9 @@ def apply_code(coded_df, file_id, full_text, segment_text, selected_code, *metad
|
|
| 109 |
new_df = pd.concat([coded_df, pd.DataFrame([new_row])], ignore_index=True)
|
| 110 |
return new_df, "Code applied!"
|
| 111 |
|
| 112 |
-
|
| 113 |
-
#
|
| 114 |
-
#
|
| 115 |
-
# ----------------------------
|
| 116 |
|
| 117 |
def generate_excel(coded_df):
|
| 118 |
if coded_df.empty:
|
|
@@ -122,10 +118,9 @@ def generate_excel(coded_df):
|
|
| 122 |
coded_df.to_excel(out_path, index=False)
|
| 123 |
return out_path, "Excel ready for download."
|
| 124 |
|
| 125 |
-
|
| 126 |
-
# ----------------------------
|
| 127 |
# GRADIO UI
|
| 128 |
-
#
|
| 129 |
|
| 130 |
with gr.Blocks(title="Qualitative Coding Interface") as demo:
|
| 131 |
|
|
@@ -137,9 +132,7 @@ with gr.Blocks(title="Qualitative Coding Interface") as demo:
|
|
| 137 |
full_text_state = gr.State("")
|
| 138 |
coded_state = gr.State(get_initial_df())
|
| 139 |
|
| 140 |
-
# --------
|
| 141 |
-
# File upload
|
| 142 |
-
# -----------------------
|
| 143 |
with gr.Row():
|
| 144 |
file_input = gr.File(label="Upload (.docx, .vtt, .txt)", file_types=[".docx", ".vtt", ".txt"])
|
| 145 |
status = gr.Textbox(label="Status", value="Ready")
|
|
@@ -150,9 +143,7 @@ with gr.Blocks(title="Qualitative Coding Interface") as demo:
|
|
| 150 |
outputs=[full_text_state, status, current_file_id, coded_state]
|
| 151 |
)
|
| 152 |
|
| 153 |
-
# --------
|
| 154 |
-
# Metadata
|
| 155 |
-
------------------------
|
| 156 |
gr.Markdown("## π Interview Metadata")
|
| 157 |
metadata_inputs = []
|
| 158 |
with gr.Row():
|
|
@@ -160,16 +151,12 @@ with gr.Blocks(title="Qualitative Coding Interface") as demo:
|
|
| 160 |
box = gr.Textbox(label=lbl)
|
| 161 |
metadata_inputs.append(box)
|
| 162 |
|
| 163 |
-
# --------
|
| 164 |
-
# Transcript
|
| 165 |
-
# -----------------------
|
| 166 |
gr.Markdown("## π Transcript")
|
| 167 |
transcript_box = gr.Textbox(label="Transcript (read-only)", lines=15, interactive=False)
|
| 168 |
full_text_state.change(lambda x: x, inputs=full_text_state, outputs=transcript_box)
|
| 169 |
|
| 170 |
-
# --------
|
| 171 |
-
# Coding controls
|
| 172 |
-
# -----------------------
|
| 173 |
gr.Markdown("## π·οΈ Apply Code")
|
| 174 |
with gr.Row():
|
| 175 |
segment_input = gr.Textbox(label="Paste segment", lines=3)
|
|
@@ -183,16 +170,12 @@ with gr.Blocks(title="Qualitative Coding Interface") as demo:
|
|
| 183 |
outputs=[coded_state, status]
|
| 184 |
)
|
| 185 |
|
| 186 |
-
# --------
|
| 187 |
-
# Show Data
|
| 188 |
-
# -----------------------
|
| 189 |
gr.Markdown("## π Coded Data")
|
| 190 |
data_table = gr.Dataframe(interactive=False)
|
| 191 |
coded_state.change(lambda x: x, inputs=coded_state, outputs=data_table)
|
| 192 |
|
| 193 |
-
# --------
|
| 194 |
-
# Download Excel
|
| 195 |
-
# -----------------------
|
| 196 |
download_btn = gr.Button("Download XLSX")
|
| 197 |
download_file = gr.File(label="Download", visible=False)
|
| 198 |
|
|
@@ -209,7 +192,6 @@ with gr.Blocks(title="Qualitative Coding Interface") as demo:
|
|
| 209 |
outputs=download_file
|
| 210 |
)
|
| 211 |
|
| 212 |
-
|
| 213 |
-
# Launch for HF Spaces
|
| 214 |
if __name__ == "__main__":
|
| 215 |
demo.launch()
|
|
|
|
| 7 |
except ImportError:
|
| 8 |
docx = None
|
| 9 |
|
| 10 |
+
# ============================
|
| 11 |
+
# CONFIGURATION
|
| 12 |
+
# ============================
|
| 13 |
|
| 14 |
DEFAULT_CODES = [
|
| 15 |
"Theme: Communication Barrier",
|
|
|
|
| 26 |
"age": "Participant Age",
|
| 27 |
}
|
| 28 |
|
| 29 |
+
# ============================
|
|
|
|
| 30 |
# FILE READERS
|
| 31 |
+
# ============================
|
| 32 |
|
| 33 |
def read_docx(file_path):
|
| 34 |
if not docx:
|
|
|
|
| 54 |
cols = ["File ID", "Code", "Coded Segment", "Context (100 chars)"] + list(METADATA_FIELDS.keys())
|
| 55 |
return pd.DataFrame(columns=cols)
|
| 56 |
|
| 57 |
+
# ============================
|
|
|
|
| 58 |
# FILE PROCESSING
|
| 59 |
+
# ============================
|
| 60 |
|
| 61 |
def process_file(file_obj):
|
| 62 |
if file_obj is None:
|
|
|
|
| 75 |
|
| 76 |
return text, f"Loaded: {filename}", filename, get_initial_df()
|
| 77 |
|
| 78 |
+
# ============================
|
| 79 |
+
# CODING
|
| 80 |
+
# ============================
|
|
|
|
| 81 |
|
| 82 |
def apply_code(coded_df, file_id, full_text, segment_text, selected_code, *metadata_values):
|
| 83 |
if not segment_text or not selected_code:
|
|
|
|
| 106 |
new_df = pd.concat([coded_df, pd.DataFrame([new_row])], ignore_index=True)
|
| 107 |
return new_df, "Code applied!"
|
| 108 |
|
| 109 |
+
# ============================
|
| 110 |
+
# EXCEL EXPORT
|
| 111 |
+
# ============================
|
|
|
|
| 112 |
|
| 113 |
def generate_excel(coded_df):
|
| 114 |
if coded_df.empty:
|
|
|
|
| 118 |
coded_df.to_excel(out_path, index=False)
|
| 119 |
return out_path, "Excel ready for download."
|
| 120 |
|
| 121 |
+
# ============================
|
|
|
|
| 122 |
# GRADIO UI
|
| 123 |
+
# ============================
|
| 124 |
|
| 125 |
with gr.Blocks(title="Qualitative Coding Interface") as demo:
|
| 126 |
|
|
|
|
| 132 |
full_text_state = gr.State("")
|
| 133 |
coded_state = gr.State(get_initial_df())
|
| 134 |
|
| 135 |
+
# ---- FILE UPLOAD ----
|
|
|
|
|
|
|
| 136 |
with gr.Row():
|
| 137 |
file_input = gr.File(label="Upload (.docx, .vtt, .txt)", file_types=[".docx", ".vtt", ".txt"])
|
| 138 |
status = gr.Textbox(label="Status", value="Ready")
|
|
|
|
| 143 |
outputs=[full_text_state, status, current_file_id, coded_state]
|
| 144 |
)
|
| 145 |
|
| 146 |
+
# ---- METADATA ----
|
|
|
|
|
|
|
| 147 |
gr.Markdown("## π Interview Metadata")
|
| 148 |
metadata_inputs = []
|
| 149 |
with gr.Row():
|
|
|
|
| 151 |
box = gr.Textbox(label=lbl)
|
| 152 |
metadata_inputs.append(box)
|
| 153 |
|
| 154 |
+
# ---- TRANSCRIPT VIEW ----
|
|
|
|
|
|
|
| 155 |
gr.Markdown("## π Transcript")
|
| 156 |
transcript_box = gr.Textbox(label="Transcript (read-only)", lines=15, interactive=False)
|
| 157 |
full_text_state.change(lambda x: x, inputs=full_text_state, outputs=transcript_box)
|
| 158 |
|
| 159 |
+
# ---- CODING ----
|
|
|
|
|
|
|
| 160 |
gr.Markdown("## π·οΈ Apply Code")
|
| 161 |
with gr.Row():
|
| 162 |
segment_input = gr.Textbox(label="Paste segment", lines=3)
|
|
|
|
| 170 |
outputs=[coded_state, status]
|
| 171 |
)
|
| 172 |
|
| 173 |
+
# ---- DISPLAY CODED DATA ----
|
|
|
|
|
|
|
| 174 |
gr.Markdown("## π Coded Data")
|
| 175 |
data_table = gr.Dataframe(interactive=False)
|
| 176 |
coded_state.change(lambda x: x, inputs=coded_state, outputs=data_table)
|
| 177 |
|
| 178 |
+
# ---- DOWNLOAD ----
|
|
|
|
|
|
|
| 179 |
download_btn = gr.Button("Download XLSX")
|
| 180 |
download_file = gr.File(label="Download", visible=False)
|
| 181 |
|
|
|
|
| 192 |
outputs=download_file
|
| 193 |
)
|
| 194 |
|
| 195 |
+
# Launch
|
|
|
|
| 196 |
if __name__ == "__main__":
|
| 197 |
demo.launch()
|