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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from ragpipeline import (RAGPipeline,Retriever,ChatBot)
|
| 3 |
+
import tempfile
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from textutils import ParagraphDocumentProcessor, DocumentProcessor,WholeTextDocumentProcessor
|
| 6 |
+
from HFChatbot import HFBot
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
###########################################
|
| 10 |
+
# MAIN CON STREAMLIT
|
| 11 |
+
###########################################
|
| 12 |
+
def main():
|
| 13 |
+
UPLOAD_DIR = "/tmp/"
|
| 14 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 15 |
+
|
| 16 |
+
codice_tabella = f"<table><tr><td>💡AURA:</td><td> AI-Driven Unified Regulatory Audit</td></tr></table>"
|
| 17 |
+
st.markdown(codice_tabella, unsafe_allow_html=True)
|
| 18 |
+
|
| 19 |
+
if "faiss_builder" not in st.session_state:
|
| 20 |
+
ragpipeline = RAGPipeline(numero_frammenti=10)
|
| 21 |
+
st.session_state["faiss_builder"] = ragpipeline
|
| 22 |
+
else:
|
| 23 |
+
ragpipeline = st.session_state["faiss_builder"]
|
| 24 |
+
|
| 25 |
+
if "storico_domande" not in st.session_state:
|
| 26 |
+
st.session_state["storico_domande"] = []
|
| 27 |
+
|
| 28 |
+
if "indice_creato" not in st.session_state:
|
| 29 |
+
st.session_state["indice_creato"] = False
|
| 30 |
+
|
| 31 |
+
modelliLLM = [
|
| 32 |
+
'Almawave/Velvet-2B',
|
| 33 |
+
'Almawave/Velvet-14B',
|
| 34 |
+
'mistralai/Mistral-7B-Instruct-v0.1',
|
| 35 |
+
]
|
| 36 |
+
modelliOllama = [
|
| 37 |
+
|
| 38 |
+
'deepseek-r1:1.5b',
|
| 39 |
+
'qwen2.5:7b',
|
| 40 |
+
'deepseek-r1:7b',
|
| 41 |
+
'llama3.2:3b',
|
| 42 |
+
'Almawave/Velvet:2B',
|
| 43 |
+
'Almawave/Velvet:14b',
|
| 44 |
+
'mistral:latest',
|
| 45 |
+
"vaiton/minerva",
|
| 46 |
+
'qwen2.5:0.5b',
|
| 47 |
+
'qwen3:4b',
|
| 48 |
+
'minerva',
|
| 49 |
+
'nemo',
|
| 50 |
+
'deepseek-r1:14b',
|
| 51 |
+
'qwen3:14b',
|
| 52 |
+
'phi4-mini-reasoning',
|
| 53 |
+
'gemma3:12b',
|
| 54 |
+
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
modello_scelto = st.selectbox("Seleziona un modello:", modelliLLM)
|
| 58 |
+
st.write(f"Hai selezionato: {modello_scelto}")
|
| 59 |
+
|
| 60 |
+
st.title("Suddivisione in paragrafi")
|
| 61 |
+
docprocessor_options = {
|
| 62 |
+
"ParagraphDocumentProcessor": ParagraphDocumentProcessor(),
|
| 63 |
+
"WholeText": WholeTextDocumentProcessor()
|
| 64 |
+
}
|
| 65 |
+
selected_docprocessor = st.selectbox("Divisione in paragrafi", docprocessor_options.keys())
|
| 66 |
+
docprocessor = docprocessor_options[selected_docprocessor]
|
| 67 |
+
st.write(f"Hai selezionato: **{selected_docprocessor}**")
|
| 68 |
+
|
| 69 |
+
ragpipeline.docprocessor = docprocessor
|
| 70 |
+
if not st.session_state["indice_creato"]:
|
| 71 |
+
st.subheader("Carica l'atto principale (Determinazione)")
|
| 72 |
+
|
| 73 |
+
main_pdf = st.file_uploader("Carica 1 file PDF - Determinazione", type=["pdf"], key="main_pdf")
|
| 74 |
+
if main_pdf:
|
| 75 |
+
st.session_state["main_pdf_nome"] = main_pdf.name
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
st.subheader("Carica eventuali Allegati PDF multipli")
|
| 79 |
+
other_pdfs = st.file_uploader("Carica allegati (puoi caricare più PDF)",
|
| 80 |
+
type=["pdf"],
|
| 81 |
+
accept_multiple_files=True,
|
| 82 |
+
key="allegati_pdf")
|
| 83 |
+
if st.button("Crea indice FAISS"):
|
| 84 |
+
if main_pdf is not None:
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
try:
|
| 89 |
+
save_path = os.path.join(UPLOAD_DIR, main_pdf.name)
|
| 90 |
+
with open(save_path, "wb") as f:
|
| 91 |
+
f.write(main_pdf.read())
|
| 92 |
+
st.success(f"✅ Determinazione caricata con successo! File salvato in: `{save_path}`")
|
| 93 |
+
ragpipeline.aggiungi_file_pdf(save_path)
|
| 94 |
+
except Exception as e:
|
| 95 |
+
st.error(f"❌ Errore nel salvataggio del file: {e}")
|
| 96 |
+
|
| 97 |
+
else:
|
| 98 |
+
st.warning("Nessun PDF di Determinazione caricato.")
|
| 99 |
+
|
| 100 |
+
if other_pdfs:
|
| 101 |
+
for uploaded_file in other_pdfs:
|
| 102 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf", dir=UPLOAD_DIR) as tmp_file:
|
| 103 |
+
tmp_file.write(uploaded_file.read())
|
| 104 |
+
tmp_path = tmp_file.name
|
| 105 |
+
ragpipeline.aggiungi_file_pdf(tmp_path)
|
| 106 |
+
st.subheader(f"Caricato file {tmp_file.name}")
|
| 107 |
+
st.success("Allegati caricati con successo!")
|
| 108 |
+
else:
|
| 109 |
+
st.info("Nessun allegato caricato.")
|
| 110 |
+
|
| 111 |
+
ragpipeline.crea_indice()
|
| 112 |
+
st.success("Indice FAISS generato e caricato.")
|
| 113 |
+
st.session_state["indice_creato"] = True
|
| 114 |
+
frammenti_recuperati =ragpipeline.attributi_frammenti
|
| 115 |
+
for frammento_recuperato in frammenti_recuperati:
|
| 116 |
+
RAGPipeline.dump_excel(dizionario=frammento_recuperato, filename="frammentiChatbot.xlsx")
|
| 117 |
+
|
| 118 |
+
if st.session_state["indice_creato"]:
|
| 119 |
+
with st.form(key="domanda_form"):
|
| 120 |
+
domanda = st.text_area("Inserisci la domanda", key="domanda_input")
|
| 121 |
+
istruzione = st.text_area("Inserisci le istruzioni", key="istruzione_input")
|
| 122 |
+
submit_button = st.form_submit_button("Analizza atto")
|
| 123 |
+
|
| 124 |
+
if submit_button:
|
| 125 |
+
if domanda.strip().upper() == "FINE":
|
| 126 |
+
st.stop()
|
| 127 |
+
|
| 128 |
+
#cb = ChatBot(model_name="flaollama", model_orig=modello_scelto)
|
| 129 |
+
|
| 130 |
+
cb = HFBot(model_name=modello_scelto)
|
| 131 |
+
|
| 132 |
+
ret = Retriever(
|
| 133 |
+
indice=ragpipeline.indice,
|
| 134 |
+
sentence_transformer_model=ragpipeline.sentence_transformer_model,
|
| 135 |
+
query=domanda + istruzione,
|
| 136 |
+
documenti=ragpipeline.documenti,
|
| 137 |
+
frammenti_indicizzati=ragpipeline.frammenti_indicizzati,
|
| 138 |
+
attributi_frammenti=ragpipeline.attributi_frammenti
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
ret.esegui_query(top_k=3)
|
| 142 |
+
|
| 143 |
+
risposta = cb.generate(
|
| 144 |
+
query=domanda,
|
| 145 |
+
relevant_docs=ret.passaggi_rilevanti,
|
| 146 |
+
attributi_frammenti_rilevanti=ret.attributi_rilevanti,
|
| 147 |
+
istruzioni=istruzione
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
st.session_state.storico_domande.append((modello_scelto, domanda, istruzione, risposta))
|
| 151 |
+
|
| 152 |
+
st.markdown(
|
| 153 |
+
f"<p><strong>Domanda:</strong> {domanda} <br/>"
|
| 154 |
+
f"<strong>Istruzioni:</strong> <em>{istruzione}</em><br/><br/>"
|
| 155 |
+
f"<strong>Risposta:</strong><em> {risposta}</em></p>",
|
| 156 |
+
unsafe_allow_html=True
|
| 157 |
+
)
|
| 158 |
+
id_frammenti_recuperati = ":".join(sorted(set(elemento['id'] for elemento in ret.attributi_rilevanti)))
|
| 159 |
+
dump = {
|
| 160 |
+
'timestamp': ragpipeline.timestamp,
|
| 161 |
+
"modello": cb.model_orig,
|
| 162 |
+
"documenti": st.session_state.get("main_pdf_nome", "non disponibile"),
|
| 163 |
+
"file_recuperati": "",
|
| 164 |
+
"file_gold": "",
|
| 165 |
+
"frammenti_recuperati":id_frammenti_recuperati,
|
| 166 |
+
"frammenti_gold": "",
|
| 167 |
+
"domanda":domanda,
|
| 168 |
+
"istruzioni":istruzione,
|
| 169 |
+
"risposta_gold": " ",
|
| 170 |
+
"risposta":cb.pulisci_risposta(risposta)}
|
| 171 |
+
|
| 172 |
+
RAGPipeline.dump_excel(dizionario=dump,filename="dumpChatbot.xlsx")
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
if st.session_state.storico_domande:
|
| 176 |
+
st.markdown("---")
|
| 177 |
+
st.subheader("Storico delle domande analizzate")
|
| 178 |
+
for idx, (mymod, q, inst, resp) in enumerate(st.session_state.storico_domande, 1):
|
| 179 |
+
st.markdown(
|
| 180 |
+
f"""
|
| 181 |
+
**{idx}. Domanda:** {q}<br/>
|
| 182 |
+
<em>Modello: </em>{mymod}<br/>
|
| 183 |
+
<em>Istruzioni:</em> {inst}<br/><br/>
|
| 184 |
+
<strong>Risposta:</strong> {resp}<br/>
|
| 185 |
+
""",
|
| 186 |
+
unsafe_allow_html=True
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
if __name__ == "__main__":
|
| 190 |
+
main()
|