Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,129 +1,12 @@
|
|
| 1 |
-
# app.py - À déployer sur Hugging Face Spaces
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import torch
|
| 6 |
-
import re
|
| 7 |
-
torch.set_grad_enabled(False)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
model.eval()
|
| 19 |
-
print(f"✅ Modèle chargé sur CPU(HF Spaces safe)")
|
| 20 |
-
|
| 21 |
-
def extract_invoice_items(image):
|
| 22 |
-
"""
|
| 23 |
-
Extrait les item descriptions d'une facture
|
| 24 |
-
"""
|
| 25 |
-
try:
|
| 26 |
-
# Préparer l'image
|
| 27 |
-
pixel_values = processor(
|
| 28 |
-
image,
|
| 29 |
-
return_tensors="pt"
|
| 30 |
-
).pixel_values
|
| 31 |
-
|
| 32 |
-
# Prompt pour extraction
|
| 33 |
-
task_prompt = "<s_header>"
|
| 34 |
-
decoder_input_ids = processor.tokenizer(
|
| 35 |
-
task_prompt,
|
| 36 |
-
add_special_tokens=False,
|
| 37 |
-
return_tensors="pt"
|
| 38 |
-
).input_ids
|
| 39 |
-
|
| 40 |
-
# Génération
|
| 41 |
-
print("⏳ Extraction en cours...")
|
| 42 |
-
with torch.no_grad():
|
| 43 |
-
outputs = model.generate(
|
| 44 |
-
pixel_values,
|
| 45 |
-
decoder_input_ids=decoder_input_ids,
|
| 46 |
-
max_length=model.decoder.config.max_position_embeddings,
|
| 47 |
-
pad_token_id=processor.tokenizer.pad_token_id,
|
| 48 |
-
eos_token_id=processor.tokenizer.eos_token_id,
|
| 49 |
-
use_cache=True,
|
| 50 |
-
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
| 51 |
-
return_dict_in_generate=True,
|
| 52 |
-
num_beams=1,
|
| 53 |
-
early_stopping=True,
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
# Décoder la séquence
|
| 57 |
-
sequence = processor.batch_decode(outputs.sequences)[0]
|
| 58 |
-
sequence = sequence.replace(processor.tokenizer.eos_token, "")
|
| 59 |
-
sequence = sequence.replace(processor.tokenizer.pad_token, "")
|
| 60 |
-
|
| 61 |
-
print("✅ Extraction terminée")
|
| 62 |
-
|
| 63 |
-
# Extraire tous les item_desc
|
| 64 |
-
item_descriptions = re.findall(
|
| 65 |
-
r'<s_item_desc>(.*?)</s_item_desc>',
|
| 66 |
-
sequence
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
# Créer l'output formaté
|
| 70 |
-
output = "=" * 60 + "\n"
|
| 71 |
-
output += "📋 ITEMS EXTRAITS (item_desc)\n"
|
| 72 |
-
output += "=" * 60 + "\n\n"
|
| 73 |
-
|
| 74 |
-
if item_descriptions:
|
| 75 |
-
output += f"✅ {len(item_descriptions)} item(s) trouvé(s) :\n\n"
|
| 76 |
-
for i, desc in enumerate(item_descriptions, 1):
|
| 77 |
-
output += f"{i}. {desc.strip()}\n"
|
| 78 |
-
else:
|
| 79 |
-
output += "⚠️ Aucun item trouvé. Voici la sortie brute :\n\n"
|
| 80 |
-
output += sequence[:1000]
|
| 81 |
-
|
| 82 |
-
# Ajouter les autres infos si disponibles
|
| 83 |
-
output += "\n" + "=" * 60 + "\n"
|
| 84 |
-
output += "📄 AUTRES INFORMATIONS EXTRAITES\n"
|
| 85 |
-
output += "=" * 60 + "\n\n"
|
| 86 |
-
|
| 87 |
-
# Extraire les autres champs
|
| 88 |
-
fields = {
|
| 89 |
-
"Numéro facture": r'<s_invoice_no>(.*?)</s_invoice_no>',
|
| 90 |
-
"Date": r'<s_invoice_date>(.*?)</s_invoice_date>',
|
| 91 |
-
"Vendeur": r'<s_seller>(.*?)</s_seller>',
|
| 92 |
-
"Client": r'<s_client>(.*?)</s_client>',
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
for label, pattern in fields.items():
|
| 96 |
-
match = re.search(pattern, sequence)
|
| 97 |
-
if match:
|
| 98 |
-
output += f"• {label}: {match.group(1).strip()}\n"
|
| 99 |
-
|
| 100 |
-
# Ajouter la sortie complète en bas
|
| 101 |
-
output += "\n" + "=" * 60 + "\n"
|
| 102 |
-
output += "🔍 SORTIE COMPLÈTE (Debug)\n"
|
| 103 |
-
output += "=" * 60 + "\n"
|
| 104 |
-
output += sequence[:2000] + ("..." if len(sequence) > 2000 else "")
|
| 105 |
-
|
| 106 |
-
return output
|
| 107 |
-
|
| 108 |
-
except Exception as e:
|
| 109 |
-
return f"❌ Erreur lors de l'extraction:\n\n{str(e)}\n\nAssurez-vous d'avoir uploadé une image de facture valide."
|
| 110 |
-
|
| 111 |
-
# Interface Gradio
|
| 112 |
-
demo = gr.Interface(
|
| 113 |
-
fn=extract_invoice_items,
|
| 114 |
-
inputs=gr.Image(type="pil", label="📤 Uploadez votre facture (JPG, PNG, PDF)"),
|
| 115 |
-
outputs=gr.Textbox(
|
| 116 |
-
label="✅ Items extraits (colonne item_desc)",
|
| 117 |
-
lines=25
|
| 118 |
-
),
|
| 119 |
-
title="🧾 Extracteur d'Items de Factures - Donut Model",
|
| 120 |
-
description="Extraction automatique de item_desc depuis des factures avec Donut"
|
| 121 |
-
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
if __name__ == "__main__":
|
| 125 |
-
print("🚀 Lancement de l'application...")
|
| 126 |
-
demo.launch()
|
| 127 |
-
|
| 128 |
|
| 129 |
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def show_sample(i):
|
| 4 |
+
return dataset[i]["image"]
|
| 5 |
|
| 6 |
+
gr.Interface(
|
| 7 |
+
fn=show_sample,
|
| 8 |
+
inputs=gr.Number(value=0, precision=0),
|
| 9 |
+
outputs=gr.Image(type="pil")
|
| 10 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
|