Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -2,34 +2,41 @@ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
|
2 |
from PIL import Image
|
3 |
import pdf2image
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-stage1")
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
11 |
def process_invoice(pdf_file):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
interface = gr.Interface(
|
27 |
fn=process_invoice,
|
28 |
-
inputs=gr.File(type="
|
29 |
outputs="text",
|
30 |
title="AI Invoice Processor",
|
31 |
description="Wgraj fakturę PDF, a AI wyodrębni dane tekstowe.",
|
32 |
)
|
33 |
|
34 |
-
# Uruchomienie aplikacji
|
35 |
interface.launch()
|
|
|
2 |
from PIL import Image
|
3 |
import pdf2image
|
4 |
import gradio as gr
|
5 |
+
import torch
|
6 |
|
7 |
+
# ✅ Używamy mniejszej wersji modelu dla szybszego działania
|
8 |
+
MODEL_NAME = "microsoft/trocr-base-stage1"
|
|
|
9 |
|
10 |
+
# ✅ Ładujemy model i procesor
|
11 |
+
processor = TrOCRProcessor.from_pretrained(MODEL_NAME)
|
12 |
+
model = VisionEncoderDecoderModel.from_pretrained(MODEL_NAME)
|
13 |
+
|
14 |
+
# ✅ Funkcja do przetwarzania faktur PDF
|
15 |
def process_invoice(pdf_file):
|
16 |
+
try:
|
17 |
+
# ✅ Konwersja PDF do obrazu
|
18 |
+
images = pdf2image.convert_from_bytes(pdf_file.read())
|
19 |
+
|
20 |
+
# ✅ Przetwarzanie pierwszej strony faktury
|
21 |
+
image = images[0].convert("RGB")
|
22 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
23 |
|
24 |
+
# ✅ Uruchomienie modelu AI
|
25 |
+
generated_ids = model.generate(pixel_values)
|
26 |
+
extracted_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
27 |
|
28 |
+
return extracted_text
|
29 |
+
except Exception as e:
|
30 |
+
return f"Błąd przetwarzania: {str(e)}"
|
31 |
|
32 |
+
# ✅ Poprawne użycie `gr.File(type="binary")`
|
33 |
interface = gr.Interface(
|
34 |
fn=process_invoice,
|
35 |
+
inputs=gr.File(type="binary"),
|
36 |
outputs="text",
|
37 |
title="AI Invoice Processor",
|
38 |
description="Wgraj fakturę PDF, a AI wyodrębni dane tekstowe.",
|
39 |
)
|
40 |
|
41 |
+
# ✅ Uruchomienie aplikacji
|
42 |
interface.launch()
|