CoolFace
Apppublic

pikaduck/invoice-extraction

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
utils.py46 linesDownload Raw Back to root
1"""2    Utilities3    @author : Sakshi Tantak4"""5import streamlit as st6import base647 8def markdown_table_to_json(markdown):9    lines = markdown.strip().split("\n")10    11    # Extract headers12    headers = [h.strip() for h in lines[0].split("|") if h.strip()]13    14    # Extract rows15    rows = []16    for line in lines[2:]:  # Skip header and separator line17        values = [v.strip() for v in line.split("|") if v.strip()]18        row_dict = dict(zip(headers, values))19        rows.append(row_dict)20    21    return rows22 23def validate_pdf(pdf_bytes: bytes) -> bool:24    """25    Validates the uploaded PDF file.26    """27    if not pdf_bytes:28        return False29 30    # Check file signature for PDF (%PDF-)31    return pdf_bytes.startswith(b'%PDF-')32 33def displayPDF(file):34    # Opening file from file path35    if isinstance(file, str):36        file_bytes = open(file, 'rb').read()37    else:38        file_bytes = file39    # with open(file, "rb") as f:40    base64_pdf = base64.b64encode(file_bytes).decode('utf-8')41 42    # Embedding PDF in HTML43    pdf_display = F'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">'44 45    # Displaying File46    st.markdown(pdf_display, unsafe_allow_html=True)