Spaces:
Sleeping
Sleeping
File size: 1,987 Bytes
07d0354 814c19e 061d5cb 10213d3 99ddfcc 814c19e 99ddfcc 814c19e 159c760 07d0354 99ddfcc 10213d3 99ddfcc db576bd 99ddfcc 9f38b98 99ddfcc 9f38b98 99ddfcc 9f38b98 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import gradio as gr
import pdfplumber
import re
from transformers import pipeline
# Model do rozpoznawania nazw organizacji i wartości numerycznych
extractor = pipeline("ner", model="dbmdz/bert-large-cased-finetuned-conll03-english", aggregation_strategy="simple")
# Reguły do identyfikacji wartości liczbowych (NIP, kwoty, daty)
nip_pattern = re.compile(r'\b\d{10}\b') # Polski NIP: 10 cyfr
kwota_pattern = re.compile(r'\b\d+[\.,]?\d*\b') # Kwoty: np. 123.45 lub 123
data_pattern = re.compile(r'\b\d{2}\.\d{2}\.\d{4}\b') # Daty: np. 21.10.2024
def extract_invoice_data(pdf_file):
with pdfplumber.open(pdf_file) as pdf:
full_text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
# Szukamy danych w tekście
entities = extractor(full_text)
seller_name = []
seller_nip = None
items = []
total_amount = None
invoice_date = None
for entity in entities:
if "ORG" in entity["entity_group"]:
seller_name.append(entity["word"]) # Zbieramy nazwę sprzedawcy
# Znajdujemy wartości numeryczne dla NIP, kwot, dat
seller_nip = nip_pattern.search(full_text)
total_amount = max(kwota_pattern.findall(full_text), key=float, default=None)
invoice_date = data_pattern.search(full_text)
return {
"Sprzedawca": " ".join(seller_name) if seller_name else "Nie znaleziono",
"NIP": seller_nip.group() if seller_nip else "Nie znaleziono",
"Data faktury": invoice_date.group() if invoice_date else "Nie znaleziono",
"Kwota całkowita": total_amount if total_amount else "Nie znaleziono"
}
# Interfejs użytkownika w Hugging Face Spaces
iface = gr.Interface(
fn=extract_invoice_data,
inputs=gr.File(label="Wybierz plik PDF"),
outputs="json",
title="Ekstrakcja danych z faktury",
description="Prześlij plik PDF, a model zwróci dane sprzedawcy, NIP, kwotę i datę faktury."
)
if __name__ == "__main__":
iface.launch()
|