Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
|
| 3 |
|
| 4 |
# Modelli
|
| 5 |
MODEL_DEBERTA = "osiria/deberta-italian-question-answering"
|
| 6 |
-
|
| 7 |
|
| 8 |
# Pipeline DeBERTa (estrattivo)
|
| 9 |
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
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 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["
|
| 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 +
|
| 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, AutoModelForCausalLM, pipeline
|
| 3 |
|
| 4 |
# Modelli
|
| 5 |
MODEL_DEBERTA = "osiria/deberta-italian-question-answering"
|
| 6 |
+
MODEL_GEPPETTO = "LorenzoDeMattei/GePpeTto"
|
| 7 |
|
| 8 |
# Pipeline DeBERTa (estrattivo)
|
| 9 |
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 GePpeTto (generativo)
|
| 14 |
+
tok_gepp = AutoTokenizer.from_pretrained(MODEL_GEPPETTO)
|
| 15 |
+
mdl_gepp = AutoModelForCausalLM.from_pretrained(MODEL_GEPPETTO)
|
| 16 |
+
qa_gepp = pipeline("text-generation", model=mdl_gepp, tokenizer=tok_gepp, 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 |
+
# Generativo (GePpeTto)
|
| 32 |
try:
|
| 33 |
+
prompt = f"Domanda: {question}\nContesto: {md_text}\nRisposta:"
|
| 34 |
+
res_gepp = qa_gepp(prompt, max_new_tokens=64, do_sample=False)
|
| 35 |
+
results["GePpeTto (generativo)"] = {
|
| 36 |
+
"risposta": res_gepp[0]["generated_text"].replace(prompt, "").strip()
|
| 37 |
}
|
| 38 |
except Exception as e:
|
| 39 |
+
results["GePpeTto (generativo)"] = {"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 + GePpeTto")
|
| 46 |
+
gr.Markdown("Confronto tra risposte estrattive (DeBERTa) e generative (GePpeTto).")
|
| 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 (Estrattivo vs Generativo)")
|
| 65 |
|
| 66 |
btn.click(
|
| 67 |
fn=ensemble_invoice_qa,
|