Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
from parse_lpo import read_msg, parse_lpo_html, init_or_load_workbook, append_row, get_excel_path
|
| 7 |
+
|
| 8 |
+
def process_msg_files(files):
|
| 9 |
+
if not files:
|
| 10 |
+
return None, "No files uploaded."
|
| 11 |
+
|
| 12 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 13 |
+
tmpdir = Path(tmpdir)
|
| 14 |
+
|
| 15 |
+
# Save uploaded files to temp folder
|
| 16 |
+
for f in files:
|
| 17 |
+
shutil.copy(f.name, tmpdir / Path(f.name).name)
|
| 18 |
+
|
| 19 |
+
# Run parser
|
| 20 |
+
excel_path = get_excel_path(tmpdir)
|
| 21 |
+
wb, ws = init_or_load_workbook(excel_path)
|
| 22 |
+
next_row = ws.max_row + 1
|
| 23 |
+
|
| 24 |
+
log_lines = []
|
| 25 |
+
for msg_path in sorted(tmpdir.glob('*.msg')):
|
| 26 |
+
try:
|
| 27 |
+
subject, html_bytes = read_msg(msg_path)
|
| 28 |
+
if not html_bytes:
|
| 29 |
+
log_lines.append(f"⚠️ {msg_path.name} — no HTML body")
|
| 30 |
+
continue
|
| 31 |
+
fields = parse_lpo_html(html_bytes)
|
| 32 |
+
row_data = {'subject': subject, 'filename': msg_path.name, **fields}
|
| 33 |
+
missing = [k for k, v in fields.items() if v is None]
|
| 34 |
+
status = 'PARTIAL' if missing else 'OK'
|
| 35 |
+
append_row(ws, row_data, next_row, status=status)
|
| 36 |
+
next_row += 1
|
| 37 |
+
log_lines.append(f"✅ {msg_path.name} — LPO:{fields['lpo_number']} | Inv:{fields['inv_number']} | Amount:{fields['inv_amount']}")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
log_lines.append(f"❌ {msg_path.name} — ERROR: {e}")
|
| 40 |
+
|
| 41 |
+
wb.save(excel_path)
|
| 42 |
+
wb.close()
|
| 43 |
+
|
| 44 |
+
# Copy excel to a stable output path Gradio can serve
|
| 45 |
+
out_path = Path(tempfile.mktemp(suffix='.xlsx'))
|
| 46 |
+
shutil.copy(excel_path, out_path)
|
| 47 |
+
|
| 48 |
+
return str(out_path), "\n".join(log_lines)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
demo = gr.Interface(
|
| 52 |
+
fn=process_msg_files,
|
| 53 |
+
inputs=gr.File(
|
| 54 |
+
file_count="multiple",
|
| 55 |
+
file_types=[".msg"],
|
| 56 |
+
label="Upload .msg files"
|
| 57 |
+
),
|
| 58 |
+
outputs=[
|
| 59 |
+
gr.File(label="Download Excel"),
|
| 60 |
+
gr.Textbox(label="Processing Log", lines=15)
|
| 61 |
+
],
|
| 62 |
+
title="LPO Invoice Parser",
|
| 63 |
+
description="Upload one or more Outlook .msg LPO emails. Get back a formatted Excel with LPO #, Ref No, Inv #, and Amount."
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
demo.launch()
|