Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1056,110 +1056,124 @@ gr.HTML("""
|
|
| 1056 |
</script> """)
|
| 1057 |
|
| 1058 |
|
| 1059 |
-
|
| 1060 |
def _split_chunks(files, csize: int):
|
| 1061 |
-
|
| 1062 |
-
|
| 1063 |
-
|
|
|
|
| 1064 |
|
| 1065 |
def _tpms():
|
| 1066 |
-
|
| 1067 |
-
|
|
|
|
| 1068 |
|
| 1069 |
def _run_click(files, rows, instr, ms, mode, csize, budget_s, no_limit):
|
| 1070 |
-
|
| 1071 |
-
|
| 1072 |
-
|
| 1073 |
-
|
| 1074 |
-
|
| 1075 |
-
|
| 1076 |
-
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
)
|
| 1081 |
-
remaining = (leftover_from_batch or []) + remaining
|
| 1082 |
-
panel_vis = gr.update(visible=bool(remaining))
|
| 1083 |
-
msg = f"{len(remaining)} files remain. Process next chunk?"
|
| 1084 |
-
prog = f"Batch progress: {done}/{total} processed in this step • Remaining overall: {len(remaining)}"
|
| 1085 |
-
return new_rows, gal, tbl, stamp, remaining, panel_vis, gr.update(value=msg), gr.update(value=prog)
|
| 1086 |
-
|
| 1087 |
-
# Auto
|
| 1088 |
-
new_rows, gal, tbl, stamp, leftover, done, total = run_batch(
|
| 1089 |
-
files, rows or [], instr, t, p, m, int(ms), budget
|
| 1090 |
)
|
| 1091 |
-
|
| 1092 |
-
|
| 1093 |
-
|
| 1094 |
-
|
| 1095 |
-
|
| 1096 |
-
|
| 1097 |
-
|
| 1098 |
-
|
| 1099 |
-
|
| 1100 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1101 |
|
| 1102 |
def _step_next(remain, rows, instr, ms, csize, budget_s, no_limit):
|
| 1103 |
-
|
| 1104 |
-
|
| 1105 |
-
|
| 1106 |
-
|
| 1107 |
-
|
| 1108 |
-
|
| 1109 |
-
|
| 1110 |
-
|
| 1111 |
-
|
| 1112 |
-
|
| 1113 |
-
|
| 1114 |
-
|
| 1115 |
-
|
| 1116 |
-
|
| 1117 |
-
)
|
| 1118 |
-
batch = remain[:int(csize)]
|
| 1119 |
-
leftover = remain[int(csize):]
|
| 1120 |
-
new_rows, gal, tbl, stamp, leftover_from_batch, done, total = run_batch(
|
| 1121 |
-
batch, rows or [], instr, t, p, m, int(ms), budget
|
| 1122 |
)
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
|
| 1129 |
-
step_next.click(
|
| 1130 |
-
_step_next,
|
| 1131 |
-
inputs=[remaining_state, rows_state, instruction_preview, max_side, chunk_size, gpu_budget, no_time_limit],
|
| 1132 |
-
outputs=[rows_state, step_msg, step_panel, remaining_state, gallery, table, autosave_md, progress_md]
|
| 1133 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1134 |
|
| 1135 |
def _step_finish():
|
| 1136 |
-
|
| 1137 |
|
| 1138 |
-
step_finish.click(_step_finish, inputs=None, outputs=[step_panel, step_msg, remaining_state])
|
| 1139 |
|
| 1140 |
-
|
|
|
|
|
|
|
|
|
|
| 1141 |
def sync_table_to_session(table_value: Any, session_rows: List[dict]) -> Tuple[List[dict], list, str]:
|
| 1142 |
-
|
| 1143 |
-
|
| 1144 |
-
|
| 1145 |
-
|
| 1146 |
-
|
| 1147 |
|
| 1148 |
-
table.change(sync_table_to_session, inputs=[table, rows_state], outputs=[rows_state, gallery, autosave_md])
|
| 1149 |
|
| 1150 |
-
|
| 1151 |
-
|
| 1152 |
-
|
| 1153 |
-
|
| 1154 |
-
|
| 1155 |
-
|
| 1156 |
-
|
| 1157 |
-
|
| 1158 |
-
|
| 1159 |
-
|
| 1160 |
-
|
| 1161 |
-
|
| 1162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1163 |
|
| 1164 |
|
| 1165 |
# ------------------------------
|
|
|
|
| 1056 |
</script> """)
|
| 1057 |
|
| 1058 |
|
| 1059 |
+
# ---- Chunking logic
|
| 1060 |
def _split_chunks(files, csize: int):
|
| 1061 |
+
files = files or []
|
| 1062 |
+
c = max(1, int(csize))
|
| 1063 |
+
return [files[i:i + c] for i in range(0, len(files), c)]
|
| 1064 |
+
|
| 1065 |
|
| 1066 |
def _tpms():
|
| 1067 |
+
s = load_settings()
|
| 1068 |
+
return s.get("temperature", 0.6), s.get("top_p", 0.9), s.get("max_tokens", 256)
|
| 1069 |
+
|
| 1070 |
|
| 1071 |
def _run_click(files, rows, instr, ms, mode, csize, budget_s, no_limit):
|
| 1072 |
+
t, p, m = _tpms()
|
| 1073 |
+
files = files or []
|
| 1074 |
+
budget = None if no_limit else float(budget_s)
|
| 1075 |
+
|
| 1076 |
+
if mode == "Manual (step)" and files:
|
| 1077 |
+
chunks = _split_chunks(files, int(csize))
|
| 1078 |
+
batch = chunks[0]
|
| 1079 |
+
remaining = sum(chunks[1:], [])
|
| 1080 |
+
new_rows, gal, tbl, stamp, leftover_from_batch, done, total = run_batch(
|
| 1081 |
+
batch, rows or [], instr, t, p, m, int(ms), budget
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1082 |
)
|
| 1083 |
+
remaining = (leftover_from_batch or []) + remaining
|
| 1084 |
+
panel_vis = gr.update(visible=bool(remaining))
|
| 1085 |
+
msg = f"{len(remaining)} files remain. Process next chunk?"
|
| 1086 |
+
prog = f"Batch progress: {done}/{total} processed in this step • Remaining overall: {len(remaining)}"
|
| 1087 |
+
return new_rows, gal, tbl, stamp, remaining, panel_vis, gr.update(value=msg), gr.update(value=prog)
|
| 1088 |
+
|
| 1089 |
+
# Auto
|
| 1090 |
+
new_rows, gal, tbl, stamp, leftover, done, total = run_batch(
|
| 1091 |
+
files, rows or [], instr, t, p, m, int(ms), budget
|
| 1092 |
)
|
| 1093 |
+
panel_vis = gr.update(visible=bool(leftover))
|
| 1094 |
+
msg = f"{len(leftover)} files remain. Process next chunk?" if leftover else ""
|
| 1095 |
+
prog = f"Batch progress: {done}/{total} processed in this call • Remaining: {len(leftover)}"
|
| 1096 |
+
return new_rows, gal, tbl, stamp, leftover, panel_vis, gr.update(value=msg), gr.update(value=prog)
|
| 1097 |
+
|
| 1098 |
+
|
| 1099 |
+
run_button.click(
|
| 1100 |
+
_run_click,
|
| 1101 |
+
inputs=[input_files, rows_state, instruction_preview, max_side, chunk_mode, chunk_size, gpu_budget, no_time_limit],
|
| 1102 |
+
outputs=[rows_state, gallery, table, autosave_md, remaining_state, step_panel, step_msg, progress_md]
|
| 1103 |
+
)
|
| 1104 |
+
|
| 1105 |
|
| 1106 |
def _step_next(remain, rows, instr, ms, csize, budget_s, no_limit):
|
| 1107 |
+
t, p, m = _tpms()
|
| 1108 |
+
remain = remain or []
|
| 1109 |
+
budget = None if no_limit else float(budget_s)
|
| 1110 |
+
|
| 1111 |
+
if not remain:
|
| 1112 |
+
return (
|
| 1113 |
+
rows,
|
| 1114 |
+
gr.update(value="No files remaining."),
|
| 1115 |
+
gr.update(visible=False),
|
| 1116 |
+
[],
|
| 1117 |
+
[],
|
| 1118 |
+
[],
|
| 1119 |
+
"Saved.",
|
| 1120 |
+
gr.update(value="")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1121 |
)
|
| 1122 |
+
|
| 1123 |
+
batch = remain[:int(csize)]
|
| 1124 |
+
leftover = remain[int(csize):]
|
| 1125 |
+
new_rows, gal, tbl, stamp, leftover_from_batch, done, total = run_batch(
|
| 1126 |
+
batch, rows or [], instr, t, p, m, int(ms), budget
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1127 |
)
|
| 1128 |
+
leftover = (leftover_from_batch or []) + leftover
|
| 1129 |
+
panel_vis = gr.update(visible=bool(leftover))
|
| 1130 |
+
msg = f"{len(leftover)} files remain. Process next chunk?" if leftover else "All done."
|
| 1131 |
+
prog = f"Batch progress: {done}/{total} processed in this step • Remaining overall: {len(leftover)}"
|
| 1132 |
+
return new_rows, msg, panel_vis, leftover, gal, tbl, stamp, gr.update(value=prog)
|
| 1133 |
+
|
| 1134 |
+
|
| 1135 |
+
step_next.click(
|
| 1136 |
+
_step_next,
|
| 1137 |
+
inputs=[remaining_state, rows_state, instruction_preview, max_side, chunk_size, gpu_budget, no_time_limit],
|
| 1138 |
+
outputs=[rows_state, step_msg, step_panel, remaining_state, gallery, table, autosave_md, progress_md]
|
| 1139 |
+
)
|
| 1140 |
+
|
| 1141 |
|
| 1142 |
def _step_finish():
|
| 1143 |
+
return gr.update(visible=False), gr.update(value=""), []
|
| 1144 |
|
|
|
|
| 1145 |
|
| 1146 |
+
step_finish.click(_step_finish, inputs=None, outputs=[step_panel, step_msg, remaining_state])
|
| 1147 |
+
|
| 1148 |
+
|
| 1149 |
+
# ---- Table edits → persist + refresh gallery
|
| 1150 |
def sync_table_to_session(table_value: Any, session_rows: List[dict]) -> Tuple[List[dict], list, str]:
|
| 1151 |
+
session_rows = _table_to_rows(table_value, session_rows or [])
|
| 1152 |
+
save_session(session_rows)
|
| 1153 |
+
gallery_pairs = [((r.get("thumb_path") or r.get("path")), r.get("caption", ""))
|
| 1154 |
+
for r in session_rows if (r.get("thumb_path") or r.get("path"))]
|
| 1155 |
+
return session_rows, gallery_pairs, f"Saved • {time.strftime('%H:%M:%S')}"
|
| 1156 |
|
|
|
|
| 1157 |
|
| 1158 |
+
table.change(sync_table_to_session, inputs=[table, rows_state], outputs=[rows_state, gallery, autosave_md])
|
| 1159 |
+
|
| 1160 |
+
|
| 1161 |
+
# ---- Exports
|
| 1162 |
+
export_csv_btn.click(
|
| 1163 |
+
lambda tbl, ds: (export_csv_from_table(tbl, ds), gr.update(visible=True)),
|
| 1164 |
+
inputs=[table, dataset_name], outputs=[csv_file, csv_file]
|
| 1165 |
+
)
|
| 1166 |
+
|
| 1167 |
+
export_xlsx_btn.click(
|
| 1168 |
+
lambda tbl, rows, px, ds: (export_excel_with_thumbs(tbl, rows or [], int(px), ds), gr.update(visible=True)),
|
| 1169 |
+
inputs=[table, rows_state, excel_thumb_px, dataset_name], outputs=[xlsx_file, xlsx_file]
|
| 1170 |
+
)
|
| 1171 |
+
|
| 1172 |
+
export_txt_btn.click(
|
| 1173 |
+
lambda tbl, ds: (export_txt_zip(tbl, ds), gr.update(visible=True)),
|
| 1174 |
+
inputs=[table, dataset_name], outputs=[txt_zip, txt_zip]
|
| 1175 |
+
)
|
| 1176 |
+
|
| 1177 |
|
| 1178 |
|
| 1179 |
# ------------------------------
|