Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,48 +3,30 @@ import pdfplumber
|
|
3 |
import re
|
4 |
from transformers import pipeline
|
5 |
|
6 |
-
# Model
|
7 |
-
extractor = pipeline("
|
8 |
|
9 |
-
def
|
10 |
with pdfplumber.open(pdf_file) as pdf:
|
11 |
-
# Pobranie tekstu z PDF
|
12 |
full_text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
|
13 |
|
14 |
-
#
|
15 |
-
|
|
|
16 |
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
-
entities = extractor(chunk)
|
21 |
-
|
22 |
-
for entity in entities:
|
23 |
-
if "ORG" in entity["entity_group"]: # Szukamy nazw organizacji
|
24 |
-
word = entity["word"]
|
25 |
-
|
26 |
-
# Usuwamy błędne tokeny (np. "Faktura", "Z", itp.)
|
27 |
-
if not re.match(r"^(Faktura|Z|##|I|KRZYSZTOF|ŻARNOWIECKA)$", word, re.IGNORECASE):
|
28 |
-
seller_tokens.append(word)
|
29 |
-
|
30 |
-
if seller_tokens: # Jeśli znaleziono organizację, przerywamy pętlę
|
31 |
-
break
|
32 |
-
|
33 |
-
# Łączymy tokeny w pełną nazwę organizacji
|
34 |
-
seller_name = " ".join(seller_tokens)
|
35 |
-
|
36 |
-
# Usuwamy ewentualne powtórzone litery lub spacje między literami np. "S A" → "S.A."
|
37 |
-
seller_name = re.sub(r"\bS A\b", "S.A.", seller_name)
|
38 |
-
|
39 |
-
return {"Sprzedawca": seller_name if seller_name else "Nie znaleziono"}
|
40 |
|
41 |
# Interfejs użytkownika w Hugging Face Spaces
|
42 |
iface = gr.Interface(
|
43 |
-
fn=
|
44 |
inputs=gr.File(label="Wybierz plik PDF"),
|
45 |
outputs="json",
|
46 |
-
title="Ekstrakcja
|
47 |
-
description="Prześlij plik PDF,
|
48 |
)
|
49 |
|
50 |
if __name__ == "__main__":
|
|
|
3 |
import re
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# Model do analizy układu faktur
|
7 |
+
extractor = pipeline("document-question-answering", model="impira/layoutlm-document-qa")
|
8 |
|
9 |
+
def extract_seller_data(pdf_file):
|
10 |
with pdfplumber.open(pdf_file) as pdf:
|
11 |
+
# Pobranie całego tekstu z PDF
|
12 |
full_text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
|
13 |
|
14 |
+
# Zadawanie pytania modelowi → pytamy o cały blok danych sprzedawcy
|
15 |
+
question = "What is the seller's information?"
|
16 |
+
response = extractor(question=question, context=full_text)
|
17 |
|
18 |
+
# Model zwraca tekst, który uznał za odpowiedź na pytanie
|
19 |
+
seller_info = response[0]["answer"] if response else "Nie znaleziono"
|
20 |
|
21 |
+
return {"Sprzedawca": seller_info}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Interfejs użytkownika w Hugging Face Spaces
|
24 |
iface = gr.Interface(
|
25 |
+
fn=extract_seller_data,
|
26 |
inputs=gr.File(label="Wybierz plik PDF"),
|
27 |
outputs="json",
|
28 |
+
title="Ekstrakcja danych sprzedawcy z faktury",
|
29 |
+
description="Prześlij plik PDF, a model zwróci kompletny zestaw danych o sprzedawcy."
|
30 |
)
|
31 |
|
32 |
if __name__ == "__main__":
|