Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,111 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
def greet(name):
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
def load_csv(file_obj):
|
| 7 |
+
if file_obj is None:
|
| 8 |
+
return None, "Upload a judged CSV to begin."
|
| 9 |
+
try:
|
| 10 |
+
df = pd.read_csv(file_obj.name)
|
| 11 |
+
return df, ""
|
| 12 |
+
except Exception as e:
|
| 13 |
+
return None, f"Failed to read CSV: {e}"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def filter_rows(file_obj, mode, similar, text_filter, max_rows):
|
| 17 |
+
df, err = load_csv(file_obj)
|
| 18 |
+
if err:
|
| 19 |
+
return err, "", None
|
| 20 |
+
if df is None or df.empty:
|
| 21 |
+
return "No data in CSV.", "", None
|
| 22 |
+
|
| 23 |
+
# Expected columns from judged-fast-accurate CSV
|
| 24 |
+
required = [
|
| 25 |
+
"frame_idx",
|
| 26 |
+
"t_sec",
|
| 27 |
+
"truth_text",
|
| 28 |
+
"fast_text",
|
| 29 |
+
"fast_similar",
|
| 30 |
+
"fast_score",
|
| 31 |
+
"fast_reason",
|
| 32 |
+
"accurate_text",
|
| 33 |
+
"accurate_similar",
|
| 34 |
+
"accurate_score",
|
| 35 |
+
"accurate_reason",
|
| 36 |
+
]
|
| 37 |
+
missing = [c for c in required if c not in df.columns]
|
| 38 |
+
if missing:
|
| 39 |
+
return f"Missing columns: {missing}", "", None
|
| 40 |
+
|
| 41 |
+
df["fast_similar"] = df["fast_similar"].astype(str)
|
| 42 |
+
df["accurate_similar"] = df["accurate_similar"].astype(str)
|
| 43 |
+
|
| 44 |
+
if mode != "both":
|
| 45 |
+
if mode == "fast":
|
| 46 |
+
df = df[df["fast_text"].notna()]
|
| 47 |
+
else:
|
| 48 |
+
df = df[df["accurate_text"].notna()]
|
| 49 |
+
|
| 50 |
+
if similar != "all":
|
| 51 |
+
val = "True" if similar == "true" else "False"
|
| 52 |
+
df = df[(df["fast_similar"] == val) | (df["accurate_similar"] == val)]
|
| 53 |
+
|
| 54 |
+
if text_filter:
|
| 55 |
+
t = text_filter.lower()
|
| 56 |
+
cols = [
|
| 57 |
+
"truth_text",
|
| 58 |
+
"fast_text",
|
| 59 |
+
"fast_reason",
|
| 60 |
+
"accurate_text",
|
| 61 |
+
"accurate_reason",
|
| 62 |
+
]
|
| 63 |
+
mask = df[cols].fillna("").apply(lambda x: x.str.lower().str.contains(t))
|
| 64 |
+
df = df[mask.any(axis=1)]
|
| 65 |
+
|
| 66 |
+
df = df.copy()
|
| 67 |
+
# Pretty display columns
|
| 68 |
+
df["fast"] = (
|
| 69 |
+
"similar=" + df["fast_similar"].fillna("")
|
| 70 |
+
+ " score=" + df["fast_score"].fillna("").astype(str)
|
| 71 |
+
+ " | " + df["fast_text"].fillna("")
|
| 72 |
+
+ " | reason: " + df["fast_reason"].fillna("")
|
| 73 |
+
)
|
| 74 |
+
df["accurate"] = (
|
| 75 |
+
"similar=" + df["accurate_similar"].fillna("")
|
| 76 |
+
+ " score=" + df["accurate_score"].fillna("").astype(str)
|
| 77 |
+
+ " | " + df["accurate_text"].fillna("")
|
| 78 |
+
+ " | reason: " + df["accurate_reason"].fillna("")
|
| 79 |
+
)
|
| 80 |
+
display_cols = ["frame_idx", "t_sec", "truth_text", "fast", "accurate"]
|
| 81 |
+
|
| 82 |
+
subset = df[display_cols].head(max_rows)
|
| 83 |
+
summary = (
|
| 84 |
+
f"Rows: {len(df)} | "
|
| 85 |
+
f"fast similar: {sum(df['fast_similar']=='True')} | "
|
| 86 |
+
f"accurate similar: {sum(df['accurate_similar']=='True')}"
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
return "", summary, subset
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
with gr.Blocks() as demo:
|
| 93 |
+
gr.Markdown("# OCR Judge Viewer")
|
| 94 |
+
with gr.Row():
|
| 95 |
+
file_in = gr.File(label="judged-fast-accurate CSV", file_types=[".csv"])
|
| 96 |
+
mode = gr.Dropdown(["both", "fast", "accurate"], value="both", label="Mode")
|
| 97 |
+
similar = gr.Dropdown(["all", "true", "false"], value="all", label="similar flag")
|
| 98 |
+
text_filter = gr.Textbox(label="Search text", placeholder="search in truth/reasons")
|
| 99 |
+
max_rows = gr.Slider(10, 500, value=100, step=10, label="Max rows")
|
| 100 |
+
err_box = gr.Markdown()
|
| 101 |
+
summary_box = gr.Markdown()
|
| 102 |
+
table = gr.Dataframe(wrap=True)
|
| 103 |
+
|
| 104 |
+
file_in.change(filter_rows, [file_in, mode, similar, text_filter, max_rows], [err_box, summary_box, table])
|
| 105 |
+
mode.change(filter_rows, [file_in, mode, similar, text_filter, max_rows], [err_box, summary_box, table])
|
| 106 |
+
similar.change(filter_rows, [file_in, mode, similar, text_filter, max_rows], [err_box, summary_box, table])
|
| 107 |
+
text_filter.change(filter_rows, [file_in, mode, similar, text_filter, max_rows], [err_box, summary_box, table])
|
| 108 |
+
max_rows.change(filter_rows, [file_in, mode, similar, text_filter, max_rows], [err_box, summary_box, table])
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|