Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForQuestionAnswering,
|
| 3 |
|
| 4 |
# Modelli
|
| 5 |
MODEL_DEBERTA = "osiria/deberta-italian-question-answering"
|
|
@@ -10,10 +10,10 @@ tok_deb = AutoTokenizer.from_pretrained(MODEL_DEBERTA)
|
|
| 10 |
mdl_deb = AutoModelForQuestionAnswering.from_pretrained(MODEL_DEBERTA)
|
| 11 |
qa_deb = pipeline("question-answering", model=mdl_deb, tokenizer=tok_deb, device=-1)
|
| 12 |
|
| 13 |
-
# Pipeline UmBERTo (
|
| 14 |
tok_umb = AutoTokenizer.from_pretrained(MODEL_UMBERTO)
|
| 15 |
-
mdl_umb =
|
| 16 |
-
qa_umb = pipeline("
|
| 17 |
|
| 18 |
def ensemble_invoice_qa(md_text: str, question: str):
|
| 19 |
results = {}
|
|
@@ -28,22 +28,22 @@ def ensemble_invoice_qa(md_text: str, question: str):
|
|
| 28 |
except Exception as e:
|
| 29 |
results["DeBERTa (estrattivo)"] = {"errore": str(e)}
|
| 30 |
|
| 31 |
-
#
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
}
|
| 38 |
except Exception as e:
|
| 39 |
-
results["UmBERTo (
|
| 40 |
|
| 41 |
return results
|
| 42 |
|
| 43 |
# ================== UI Gradio ==================
|
| 44 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 45 |
gr.Markdown("# 🧾 Invoice QA: Ensemble DeBERTa + UmBERTo")
|
| 46 |
-
gr.Markdown("Confronto tra risposte estrattive
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column(scale=1):
|
|
@@ -61,7 +61,7 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 61 |
btn = gr.Button("🔍 Analizza Documento", variant="primary")
|
| 62 |
|
| 63 |
with gr.Column(scale=1):
|
| 64 |
-
out_json = gr.JSON(label="Risultati Ensemble (
|
| 65 |
|
| 66 |
btn.click(
|
| 67 |
fn=ensemble_invoice_qa,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
|
| 3 |
|
| 4 |
# Modelli
|
| 5 |
MODEL_DEBERTA = "osiria/deberta-italian-question-answering"
|
|
|
|
| 10 |
mdl_deb = AutoModelForQuestionAnswering.from_pretrained(MODEL_DEBERTA)
|
| 11 |
qa_deb = pipeline("question-answering", model=mdl_deb, tokenizer=tok_deb, device=-1)
|
| 12 |
|
| 13 |
+
# Pipeline UmBERTo (estrattivo)
|
| 14 |
tok_umb = AutoTokenizer.from_pretrained(MODEL_UMBERTO)
|
| 15 |
+
mdl_umb = AutoModelForQuestionAnswering.from_pretrained(MODEL_UMBERTO)
|
| 16 |
+
qa_umb = pipeline("question-answering", model=mdl_umb, tokenizer=tok_umb, device=-1)
|
| 17 |
|
| 18 |
def ensemble_invoice_qa(md_text: str, question: str):
|
| 19 |
results = {}
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
results["DeBERTa (estrattivo)"] = {"errore": str(e)}
|
| 30 |
|
| 31 |
+
# Estrattivo (UmBERTo)
|
| 32 |
try:
|
| 33 |
+
res_umb = qa_umb(question=question, context=md_text)
|
| 34 |
+
results["UmBERTo (estrattivo)"] = {
|
| 35 |
+
"risposta": res_umb["answer"].strip(),
|
| 36 |
+
"confidenza": round(res_umb["score"], 3)
|
| 37 |
}
|
| 38 |
except Exception as e:
|
| 39 |
+
results["UmBERTo (estrattivo)"] = {"errore": str(e)}
|
| 40 |
|
| 41 |
return results
|
| 42 |
|
| 43 |
# ================== UI Gradio ==================
|
| 44 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 45 |
gr.Markdown("# 🧾 Invoice QA: Ensemble DeBERTa + UmBERTo")
|
| 46 |
+
gr.Markdown("Confronto tra risposte estrattive di due modelli italiani.")
|
| 47 |
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column(scale=1):
|
|
|
|
| 61 |
btn = gr.Button("🔍 Analizza Documento", variant="primary")
|
| 62 |
|
| 63 |
with gr.Column(scale=1):
|
| 64 |
+
out_json = gr.JSON(label="Risultati Ensemble (DeBERTa vs UmBERTo)")
|
| 65 |
|
| 66 |
btn.click(
|
| 67 |
fn=ensemble_invoice_qa,
|