Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -170,7 +170,7 @@ def map_csv_for_review(file):
|
|
| 170 |
if pd.isna(text) or str(text).strip() == "":
|
| 171 |
continue
|
| 172 |
|
| 173 |
-
top_matches = classify_transaction(str(text), top_k=
|
| 174 |
if not top_matches:
|
| 175 |
continue
|
| 176 |
|
|
@@ -183,10 +183,6 @@ def map_csv_for_review(file):
|
|
| 183 |
"Corrected_Cat1": top_matches[0]["cat1"], # Default to predicted
|
| 184 |
"Corrected_Cat2": top_matches[0]["cat2"], # Default to predicted
|
| 185 |
"Status": "β
OK" if top_matches[0]["score"] > 0.8 else "β οΈ Review",
|
| 186 |
-
"Alt1_Cat1": top_matches[1]["cat1"] if len(top_matches) > 1 else "",
|
| 187 |
-
"Alt1_Cat2": top_matches[1]["cat2"] if len(top_matches) > 1 else "",
|
| 188 |
-
"Alt2_Cat1": top_matches[2]["cat1"] if len(top_matches) > 2 else "",
|
| 189 |
-
"Alt2_Cat2": top_matches[2]["cat2"] if len(top_matches) > 2 else "",
|
| 190 |
})
|
| 191 |
|
| 192 |
if not results:
|
|
@@ -200,6 +196,30 @@ def map_csv_for_review(file):
|
|
| 200 |
except Exception as e:
|
| 201 |
return None, f"β Error: {str(e)}"
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
# ==================================================
|
| 204 |
# πΎ Save Corrections from Edited Table
|
| 205 |
# ==================================================
|
|
@@ -402,8 +422,9 @@ with gr.Blocks(title="Transaction Category Classifier", theme=gr.themes.Soft())
|
|
| 402 |
gr.Markdown("""
|
| 403 |
## π How to use:
|
| 404 |
1. **Upload CSV** with 'transaction' column
|
| 405 |
-
2. **
|
| 406 |
-
3. **
|
|
|
|
| 407 |
""")
|
| 408 |
|
| 409 |
csv_input = gr.File(label="π Upload CSV file", file_types=[".csv"])
|
|
@@ -411,6 +432,30 @@ with gr.Blocks(title="Transaction Category Classifier", theme=gr.themes.Soft())
|
|
| 411 |
|
| 412 |
process_status = gr.Textbox(label="Status", lines=3, interactive=False)
|
| 413 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
gr.Markdown("### βοΈ Editable Results - Modify corrections directly in the table:")
|
| 415 |
review_table = gr.DataFrame(
|
| 416 |
label="Review & Edit Predictions (Edit 'Corrected_Cat1' and 'Corrected_Cat2' columns)",
|
|
@@ -418,7 +463,7 @@ with gr.Blocks(title="Transaction Category Classifier", theme=gr.themes.Soft())
|
|
| 418 |
wrap=True
|
| 419 |
)
|
| 420 |
|
| 421 |
-
gr.Markdown("**Instructions:** Click any cell in 'Corrected_Cat1' or 'Corrected_Cat2' to edit. Use
|
| 422 |
|
| 423 |
with gr.Row():
|
| 424 |
btn_save = gr.Button("πΎ Save All Corrections", variant="primary", size="lg")
|
|
@@ -427,12 +472,21 @@ with gr.Blocks(title="Transaction Category Classifier", theme=gr.themes.Soft())
|
|
| 427 |
|
| 428 |
save_status = gr.Textbox(label="Save Status", lines=4, interactive=False)
|
| 429 |
|
|
|
|
| 430 |
btn_process.click(
|
| 431 |
fn=map_csv_for_review,
|
| 432 |
inputs=csv_input,
|
| 433 |
outputs=[review_table, process_status]
|
| 434 |
)
|
| 435 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
btn_save.click(
|
| 437 |
fn=save_batch_corrections,
|
| 438 |
inputs=review_table,
|
|
|
|
| 170 |
if pd.isna(text) or str(text).strip() == "":
|
| 171 |
continue
|
| 172 |
|
| 173 |
+
top_matches = classify_transaction(str(text), top_k=1)
|
| 174 |
if not top_matches:
|
| 175 |
continue
|
| 176 |
|
|
|
|
| 183 |
"Corrected_Cat1": top_matches[0]["cat1"], # Default to predicted
|
| 184 |
"Corrected_Cat2": top_matches[0]["cat2"], # Default to predicted
|
| 185 |
"Status": "β
OK" if top_matches[0]["score"] > 0.8 else "β οΈ Review",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
})
|
| 187 |
|
| 188 |
if not results:
|
|
|
|
| 196 |
except Exception as e:
|
| 197 |
return None, f"β Error: {str(e)}"
|
| 198 |
|
| 199 |
+
# ==================================================
|
| 200 |
+
# π Filter Data Function
|
| 201 |
+
# ==================================================
|
| 202 |
+
def filter_dataframe(df, cat1_filter, cat2_filter, confidence_threshold):
|
| 203 |
+
"""Filter the dataframe based on category and confidence filters"""
|
| 204 |
+
if df is None or len(df) == 0:
|
| 205 |
+
return df
|
| 206 |
+
|
| 207 |
+
filtered_df = df.copy()
|
| 208 |
+
|
| 209 |
+
# Apply Cat1 filter
|
| 210 |
+
if cat1_filter and cat1_filter != "All":
|
| 211 |
+
filtered_df = filtered_df[filtered_df["Predicted_Cat1"] == cat1_filter]
|
| 212 |
+
|
| 213 |
+
# Apply Cat2 filter
|
| 214 |
+
if cat2_filter and cat2_filter != "All":
|
| 215 |
+
filtered_df = filtered_df[filtered_df["Predicted_Cat2"] == cat2_filter]
|
| 216 |
+
|
| 217 |
+
# Apply confidence filter
|
| 218 |
+
if confidence_threshold is not None:
|
| 219 |
+
filtered_df = filtered_df[filtered_df["Confidence"] <= confidence_threshold]
|
| 220 |
+
|
| 221 |
+
return filtered_df
|
| 222 |
+
|
| 223 |
# ==================================================
|
| 224 |
# πΎ Save Corrections from Edited Table
|
| 225 |
# ==================================================
|
|
|
|
| 422 |
gr.Markdown("""
|
| 423 |
## π How to use:
|
| 424 |
1. **Upload CSV** with 'transaction' column
|
| 425 |
+
2. **Use filters** to focus on specific categories or low-confidence predictions
|
| 426 |
+
3. **Review & Edit** the table below - modify 'Corrected_Cat1' and 'Corrected_Cat2' columns
|
| 427 |
+
4. **Save** corrections for model training
|
| 428 |
""")
|
| 429 |
|
| 430 |
csv_input = gr.File(label="π Upload CSV file", file_types=[".csv"])
|
|
|
|
| 432 |
|
| 433 |
process_status = gr.Textbox(label="Status", lines=3, interactive=False)
|
| 434 |
|
| 435 |
+
gr.Markdown("### π Filters")
|
| 436 |
+
with gr.Row():
|
| 437 |
+
cat1_filter = gr.Dropdown(
|
| 438 |
+
choices=["All"] + unique_cat1,
|
| 439 |
+
label="Filter by Category 1",
|
| 440 |
+
value="All",
|
| 441 |
+
interactive=True
|
| 442 |
+
)
|
| 443 |
+
cat2_filter = gr.Dropdown(
|
| 444 |
+
choices=["All"] + unique_cat2,
|
| 445 |
+
label="Filter by Category 2",
|
| 446 |
+
value="All",
|
| 447 |
+
interactive=True
|
| 448 |
+
)
|
| 449 |
+
confidence_filter = gr.Slider(
|
| 450 |
+
minimum=0.0,
|
| 451 |
+
maximum=1.0,
|
| 452 |
+
value=1.0,
|
| 453 |
+
step=0.1,
|
| 454 |
+
label="Show predictions with confidence β€ (set to 1.0 to see all)",
|
| 455 |
+
interactive=True
|
| 456 |
+
)
|
| 457 |
+
btn_apply_filters = gr.Button("π Apply Filters", variant="secondary")
|
| 458 |
+
|
| 459 |
gr.Markdown("### βοΈ Editable Results - Modify corrections directly in the table:")
|
| 460 |
review_table = gr.DataFrame(
|
| 461 |
label="Review & Edit Predictions (Edit 'Corrected_Cat1' and 'Corrected_Cat2' columns)",
|
|
|
|
| 463 |
wrap=True
|
| 464 |
)
|
| 465 |
|
| 466 |
+
gr.Markdown("**Instructions:** Click any cell in 'Corrected_Cat1' or 'Corrected_Cat2' to edit. Use filters to focus on specific categories.")
|
| 467 |
|
| 468 |
with gr.Row():
|
| 469 |
btn_save = gr.Button("πΎ Save All Corrections", variant="primary", size="lg")
|
|
|
|
| 472 |
|
| 473 |
save_status = gr.Textbox(label="Save Status", lines=4, interactive=False)
|
| 474 |
|
| 475 |
+
# Process CSV and load data
|
| 476 |
btn_process.click(
|
| 477 |
fn=map_csv_for_review,
|
| 478 |
inputs=csv_input,
|
| 479 |
outputs=[review_table, process_status]
|
| 480 |
)
|
| 481 |
|
| 482 |
+
# Apply filters to the current data
|
| 483 |
+
btn_apply_filters.click(
|
| 484 |
+
fn=filter_dataframe,
|
| 485 |
+
inputs=[review_table, cat1_filter, cat2_filter, confidence_filter],
|
| 486 |
+
outputs=review_table
|
| 487 |
+
)
|
| 488 |
+
|
| 489 |
+
# Save corrections
|
| 490 |
btn_save.click(
|
| 491 |
fn=save_batch_corrections,
|
| 492 |
inputs=review_table,
|