| import os |
| import sys |
| import torch |
| import dotenv |
| import logging |
| import streamlit as st |
| sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| from modules import semantic_filter |
|
|
| torch.classes.__path__ = [] |
|
|
|
|
| CONFIDENCE = 0.8 |
| CHUNK_SIZE = 1000 |
| METADATA_PATH = 'modules/fixtures/Scheda metadatazione_Progetti_Lozalizzazioni_PNRR_Italiadomani_V2.xlsx' |
|
|
|
|
| def set_page_config() -> None: |
| """Configure Streamlit page settings for semantic filter. |
| |
| Returns: |
| None |
| """ |
| st.set_page_config( |
| page_title="Semantic Filter", |
| page_icon=":desert_island:", |
| ) |
|
|
|
|
| def main() -> None: |
| """Main function for semantic filter user interface. |
| Handles file upload, parameter configuration, and search execution. |
| |
| Returns: |
| None |
| """ |
| st.title("🔍 Filtro Semantico Progetti PNRR") |
| st.markdown(""" |
| Questa sezione permette di filtrare i progetti PNRR utilizzando ricerca semantica avanzata. |
| Inserisci una query testuale e il sistema identificherà automaticamente i progetti più rilevanti. |
| """) |
|
|
| st.header("📁 Carica il File Excel") |
| uploaded_file = st.file_uploader( |
| "Seleziona il file Excel contenente i progetti PNRR", type=["xlsx"]) |
|
|
| st.header("⚙️ Parametri di Ricerca") |
| col1, col2 = st.columns(2) |
|
|
| with col1: |
| confidence = st.slider( |
| "Soglia di confidenza", |
| min_value=0.0, |
| max_value=1.0, |
| value=CONFIDENCE, |
| step=0.01, |
| help="Valore minimo di similarità per considerare un progetto rilevante" |
| ) |
|
|
| with col2: |
| output_option = st.selectbox( |
| "Opzione di output", |
| [("Aggiungi una colonna al file", "add_column"), |
| ("Crea un nuovo file", "new_file")], |
| format_func=lambda x: x[0], |
| index=0 |
| ) |
|
|
| st.header("💬 Query di Ricerca") |
| user_query = st.text_area( |
| "Inserisci la query di ricerca semantica:", |
| height=150, |
| max_chars=None, |
| help="Descrivi il tipo di progetti che stai cercando in linguaggio naturale", |
| placeholder="Esempio: progetti di digitalizzazione nelle scuole, infrastrutture sostenibili, riqualificazione urbana..." |
| ) |
|
|
| if st.button("🚀 Avvia Ricerca Semantica", type="primary"): |
| if uploaded_file is not None and user_query: |
| with st.spinner("Ricerca in corso... Questo potrebbe richiedere alcuni minuti."): |
| try: |
| semantic_filter.apply( |
| data_frame_path=uploaded_file, |
| metadata_path=METADATA_PATH, |
| user_query=user_query, |
| threshold=confidence, |
| chunk_size=CHUNK_SIZE, |
| output_option=output_option[1] |
| ) |
|
|
| with open(semantic_filter.SAVE_PATH, 'rb') as f: |
| file_bytes = f.read() |
|
|
| st.success("✅ Ricerca completata con successo!") |
|
|
| st.download_button( |
| label="📥 Scarica Risultati", |
| data=file_bytes, |
| file_name=semantic_filter.SAVE_PATH.split('/')[-1], |
| mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" |
| ) |
| except Exception as e: |
| st.error(f"❌ Errore durante la ricerca: {str(e)}") |
| else: |
| st.error("⚠️ Carica un file Excel e inserisci una query di ricerca.") |
|
|
|
|
| if __name__ == "__main__": |
| logging.basicConfig(level=logging.INFO) |
| dotenv.load_dotenv() |
|
|
| set_page_config() |
| main() |
|
|