Update app.py
Browse files73 unmodified lines747576777879808182838485868788899091929394243 unmodified lines3383393403413423433442 unmodified lines34734834935035135235335435535635735835936036136236336436536636736836937037137237339 unmodified lines41341441541641741841942042142242342442542642742842943043143243343443543643743873 unmodified linesrows.append((_clean_int(no), _clean_text(mk), _clean_int(sks), _clean_grade(nl)))return rowsdef parse_transcript_pdf(pdf_bytes: bytes):import pdfplumber # lazy importrows = []with pdfplumber.open(BytesIO(pdf_bytes)) as pdf:for page in pdf.pages:part = _try_table_extract(page)if not part or len(part) < 5:part = _regex_lines(page)rows.extend(part)# uniqueseen=set(); uniq=[]for no,mk,sks,nl in rows:key=(no,mk)if key in seen: continueseen.add(key); uniq.append((no,mk,sks,nl))return uniq# ----------------- Normalisasi -----------------243 unmodified lines)sheet_name = gr.Textbox(label="Nama Sheet (kosongkan = aktif)", value="")header_row = gr.Number(label="Baris Header (default 1)", value=1, precision=0)value_row = gr.Number(label="Baris Nilai (horizontal) default 2", value=2, precision=0)gr.Markdown("### Hasil Ekstraksi")out_df = gr.Dataframe(wrap=True)2 unmodified linesgr.Markdown("### Unduh Excel Hasil (setelah isi template)")out_xlsx = gr.File(label="Excel Hasil")# --- Auto-reset ketika PDF/Template diganti (Satu PDF) ---def _reset_single(_):# kembalikan: df_state, out_df, status, out_xlsxreturn None, None, "", Nonedef _reset_output_on_tpl_single(_):# kosongkan hasil output ketika template digantireturn None, ""pdf.change(_reset_single,inputs=[pdf],outputs=[df_state, out_df, status, out_xlsx],)tpl.change(_reset_output_on_tpl_single,inputs=[tpl],outputs=[out_xlsx, status],)# wiring tahap 1def _store_df(df, msg):return df, df, msg39 unmodified linesgr.Markdown("### Unduh Excel Hasil (satu sheet)")out_file = gr.File(label="Excel Hasil (1 file)")# --- Auto-reset ketika kumpulan PDF/Template diganti (Batch) ---def _reset_batch(_):# kembalikan: batch_state, out_df_b, status_b, out_filereturn [], None, "", Nonedef _reset_output_on_tpl_batch(_):return None, ""pdfs.change(_reset_batch,inputs=[pdfs],outputs=[batch_state, out_df_b, status_b, out_file],)tpl_b.change(_reset_output_on_tpl_batch,inputs=[tpl_b],outputs=[out_file, status_b],)# wiring batchbtn_prev.click(batch_preview, inputs=[pdfs], outputs=[out_df_b, batch_state, status_b])btn_apply_b.click(73 unmodified lines7475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157243 unmodified lines4014024034044054064072 unmodified lines41041141241341441539 unmodified lines45545645745845946073 unmodified linesrows.append((_clean_int(no), _clean_text(mk), _clean_int(sks), _clean_grade(nl)))return rows# === NEW: parser fleksibel (fallback) ===def _flex_lines(page):"""Ekstraksi fleksibel berbasis teks:- ambil GRADE di ujung kanan,- ambil SKS di kiri-nya,- sisanya = nama MK,- buang No/Kode di awal bila ada."""rows = []text = page.extract_text() or ""for raw in text.split("\n"):s = _clean_text(raw)if not s:continue# skip baris header umumif re.search(r"\b(kode|mata|mk|sks|nilai|semester|jumlah|total)\b", s, re.I):continue# grade di ujung kananm_grade = re.search(r"([ABCD][+\-]?)\s*$", s, re.I)if not m_grade:continuegrade = _clean_grade(m_grade.group(1))left = s[:m_grade.start()].strip()# sks sebelum gradem_sks = re.search(r"(\d{1,2})\s*$", left)if not m_sks:continuesks = _clean_int(m_sks.group(1))name = left[:m_sks.start()].strip()# buang No/Kode di awalname = re.sub(r"^\d+\s+", "", name) # No: 1 ...name = re.sub(r"^[A-Z]{2,}\d{2,}\s+", "", name) # Kode: ABC123 ...name = re.sub(r"^[A-Z]{2,}\s+\d{2,}\s+", "", name)# Kode: ABC 123 ...name = _clean_text(name)if len(name) < 2:continuerows.append(("", name, sks, grade)) # No diisi belakanganreturn rowsdef parse_transcript_pdf(pdf_bytes: bytes):import pdfplumber # lazy importrows = []with pdfplumber.open(BytesIO(pdf_bytes)) as pdf:for page in pdf.pages:# 1) coba tabel garispart = _try_table_extract(page)# 2) kalau sedikit, coba regex lamaif not part or len(part) < 5:part2 = _regex_lines(page)if part2:part.extend(part2)# 3) kalau masih sedikit, pakai fallback fleksibelif not part or len(part) < 5:part3 = _flex_lines(page)if part3:part.extend(part3)rows.extend(part)# unique + isi nomor urut jika kosongseen, uniq = set(), []n = 1for no, mk, sks, nl in rows:key = (mk, sks, nl)if key in seen:continueseen.add(key)if not str(no).strip():no = nn += 1uniq.append((no, mk, sks, nl))return uniq# ----------------- Normalisasi -----------------243 unmodified lines)sheet_name = gr.Textbox(label="Nama Sheet (kosongkan = aktif)", value="")header_row = gr.Number(label="Baris Header (default 1)", value=1, precision=0)value_row = gr.Number(label="Baris Nilai (horizontal) default 2)", value=2, precision=0)gr.Markdown("### Hasil Ekstraksi")out_df = gr.Dataframe(wrap=True)2 unmodified linesgr.Markdown("### Unduh Excel Hasil (setelah isi template)")out_xlsx = gr.File(label="Excel Hasil")# wiring tahap 1def _store_df(df, msg):return df, df, msg39 unmodified linesgr.Markdown("### Unduh Excel Hasil (satu sheet)")out_file = gr.File(label="Excel Hasil (1 file)")# wiring batchbtn_prev.click(batch_preview, inputs=[pdfs], outputs=[out_df_b, batch_state, status_b])btn_apply_b.click(
app.py+71-49