Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,36 @@ import gradio as gr
|
|
| 9 |
SUPPORTED_EXT = {".xlsx", ".xls", ".csv"}
|
| 10 |
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# -----------------------------
|
| 13 |
# Header auto-detection
|
| 14 |
# -----------------------------
|
|
@@ -145,7 +175,6 @@ def _collect_inputs(files, zip_batch):
|
|
| 145 |
# Main combine
|
| 146 |
# -----------------------------
|
| 147 |
def combine_excel(uploaded_files, uploaded_zip, progress=gr.Progress()):
|
| 148 |
-
|
| 149 |
if not uploaded_files and not uploaded_zip:
|
| 150 |
return "Please upload files or ZIP.", None, None, "No files uploaded."
|
| 151 |
|
|
@@ -163,7 +192,6 @@ def combine_excel(uploaded_files, uploaded_zip, progress=gr.Progress()):
|
|
| 163 |
progress(0, desc="Starting...")
|
| 164 |
|
| 165 |
for i, path in enumerate(input_paths, start=1):
|
| 166 |
-
|
| 167 |
progress((i - 1) / total, desc=f"Reading {path.name}")
|
| 168 |
|
| 169 |
df, header_row = _read_with_auto_header(path)
|
|
@@ -186,7 +214,6 @@ def combine_excel(uploaded_files, uploaded_zip, progress=gr.Progress()):
|
|
| 186 |
final_column_order.append(col)
|
| 187 |
|
| 188 |
all_dfs.append(df)
|
| 189 |
-
|
| 190 |
progress(i / total, desc=f"Processed {path.name}")
|
| 191 |
|
| 192 |
progress(1, desc="Combining...")
|
|
@@ -214,39 +241,61 @@ def combine_excel(uploaded_files, uploaded_zip, progress=gr.Progress()):
|
|
| 214 |
- Total columns: **{len(combined.columns)}**
|
| 215 |
- Error rows detected: **{error_count}**
|
| 216 |
"""
|
| 217 |
-
|
| 218 |
return "Done ✅", str(output_path), (str(error_path) if error_path else None), summary
|
| 219 |
|
| 220 |
|
| 221 |
# -----------------------------
|
| 222 |
-
# UI
|
| 223 |
# -----------------------------
|
| 224 |
with gr.Blocks(title="Excel Batch Combiner") as demo:
|
| 225 |
-
gr.
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
)
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
summary = gr.Markdown()
|
| 245 |
-
|
| 246 |
-
run_btn.click(
|
| 247 |
-
combine_excel,
|
| 248 |
-
inputs=[uploaded_files, uploaded_zip],
|
| 249 |
-
outputs=[status, output_file, error_file, summary]
|
| 250 |
)
|
| 251 |
|
| 252 |
-
|
|
|
|
|
|
|
|
|
| 9 |
SUPPORTED_EXT = {".xlsx", ".xls", ".csv"}
|
| 10 |
|
| 11 |
|
| 12 |
+
# ========================
|
| 13 |
+
# CUSTOM LOGIN (Option 2)
|
| 14 |
+
# ========================
|
| 15 |
+
EXPECTED_USER = os.environ.get("USERNAME", "")
|
| 16 |
+
EXPECTED_PASS = os.environ.get("PASSWORD", "")
|
| 17 |
+
|
| 18 |
+
if not EXPECTED_USER or not EXPECTED_PASS:
|
| 19 |
+
raise RuntimeError(
|
| 20 |
+
"Missing USERNAME/PASSWORD secrets. "
|
| 21 |
+
"Set them in Hugging Face Space > Settings > Secrets."
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def do_login(username: str, password: str):
|
| 26 |
+
"""Toggle visibility based on login success."""
|
| 27 |
+
if username == EXPECTED_USER and password == EXPECTED_PASS:
|
| 28 |
+
return (
|
| 29 |
+
True,
|
| 30 |
+
"✅ Logged in successfully.",
|
| 31 |
+
gr.update(visible=False), # hide login
|
| 32 |
+
gr.update(visible=True), # show app
|
| 33 |
+
)
|
| 34 |
+
return (
|
| 35 |
+
False,
|
| 36 |
+
"❌ Wrong username or password.",
|
| 37 |
+
gr.update(visible=True),
|
| 38 |
+
gr.update(visible=False),
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
# -----------------------------
|
| 43 |
# Header auto-detection
|
| 44 |
# -----------------------------
|
|
|
|
| 175 |
# Main combine
|
| 176 |
# -----------------------------
|
| 177 |
def combine_excel(uploaded_files, uploaded_zip, progress=gr.Progress()):
|
|
|
|
| 178 |
if not uploaded_files and not uploaded_zip:
|
| 179 |
return "Please upload files or ZIP.", None, None, "No files uploaded."
|
| 180 |
|
|
|
|
| 192 |
progress(0, desc="Starting...")
|
| 193 |
|
| 194 |
for i, path in enumerate(input_paths, start=1):
|
|
|
|
| 195 |
progress((i - 1) / total, desc=f"Reading {path.name}")
|
| 196 |
|
| 197 |
df, header_row = _read_with_auto_header(path)
|
|
|
|
| 214 |
final_column_order.append(col)
|
| 215 |
|
| 216 |
all_dfs.append(df)
|
|
|
|
| 217 |
progress(i / total, desc=f"Processed {path.name}")
|
| 218 |
|
| 219 |
progress(1, desc="Combining...")
|
|
|
|
| 241 |
- Total columns: **{len(combined.columns)}**
|
| 242 |
- Error rows detected: **{error_count}**
|
| 243 |
"""
|
|
|
|
| 244 |
return "Done ✅", str(output_path), (str(error_path) if error_path else None), summary
|
| 245 |
|
| 246 |
|
| 247 |
# -----------------------------
|
| 248 |
+
# UI (with login gate)
|
| 249 |
# -----------------------------
|
| 250 |
with gr.Blocks(title="Excel Batch Combiner") as demo:
|
| 251 |
+
logged_in = gr.State(False)
|
| 252 |
+
|
| 253 |
+
# --- Login page ---
|
| 254 |
+
login_box = gr.Column(visible=True)
|
| 255 |
+
with login_box:
|
| 256 |
+
gr.Markdown("# Log in page")
|
| 257 |
+
login_user = gr.Textbox(label="Username", placeholder="Enter username")
|
| 258 |
+
login_pass = gr.Textbox(label="Password", type="password", placeholder="Enter password")
|
| 259 |
+
login_btn = gr.Button("Log In", variant="primary")
|
| 260 |
+
login_msg = gr.Markdown("")
|
| 261 |
+
|
| 262 |
+
# --- App page (hidden until login) ---
|
| 263 |
+
app_box = gr.Column(visible=False)
|
| 264 |
+
with app_box:
|
| 265 |
+
gr.Markdown("# Excel / ZIP Batch Combiner (Preserve Header Order)")
|
| 266 |
+
|
| 267 |
+
with gr.Row():
|
| 268 |
+
uploaded_files = gr.File(
|
| 269 |
+
label="Upload Multiple Excel/CSV Files",
|
| 270 |
+
file_types=[".xlsx", ".xls", ".csv"],
|
| 271 |
+
file_count="multiple"
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
uploaded_zip = gr.File(
|
| 275 |
+
label="Or Upload ZIP Batch",
|
| 276 |
+
file_types=[".zip"]
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
run_btn = gr.Button("Combine")
|
| 280 |
+
|
| 281 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 282 |
+
output_file = gr.File(label="Download Combined File")
|
| 283 |
+
error_file = gr.File(label="Download Error Report")
|
| 284 |
+
summary = gr.Markdown()
|
| 285 |
+
|
| 286 |
+
run_btn.click(
|
| 287 |
+
combine_excel,
|
| 288 |
+
inputs=[uploaded_files, uploaded_zip],
|
| 289 |
+
outputs=[status, output_file, error_file, summary]
|
| 290 |
)
|
| 291 |
|
| 292 |
+
# Login action
|
| 293 |
+
login_btn.click(
|
| 294 |
+
fn=do_login,
|
| 295 |
+
inputs=[login_user, login_pass],
|
| 296 |
+
outputs=[logged_in, login_msg, login_box, app_box],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
)
|
| 298 |
|
| 299 |
+
if __name__ == "__main__":
|
| 300 |
+
demo.queue().launch()
|
| 301 |
+
|