Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ from typing import List
|
|
4 |
from pydantic import BaseModel
|
5 |
from fastapi import FastAPI, HTTPException
|
6 |
import rdflib
|
7 |
-
from rdflib import RDF, RDFS, OWL
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
import faiss
|
10 |
import json
|
@@ -93,6 +93,11 @@ def extract_ontology(rdf_file: str, output_file: str):
|
|
93 |
for entity in g.subjects(RDF.type, OWL.NamedIndividual):
|
94 |
label = g.value(entity, RDFS.label, default=str(entity))
|
95 |
description = g.value(entity, RDFS.comment, default="No description.")
|
|
|
|
|
|
|
|
|
|
|
96 |
# Estrai le proprietà dell'entità
|
97 |
entity_properties = {}
|
98 |
for predicate, obj in g.predicate_objects(entity):
|
@@ -300,7 +305,7 @@ Ecco alcune informazioni rilevanti recuperate dalla base di conoscenza:
|
|
300 |
|
301 |
### Regole Stringenti ###
|
302 |
1) Se l'utente chiede informazioni su questa ontologia, genera SEMPRE una query SPARQL in UNA SOLA RIGA, con prefix:
|
303 |
-
PREFIX base: <http://www.semanticweb.org/lucreziamosca/ontologies/
|
304 |
2) La query SPARQL deve essere precisa e cercare esattamente le entità specificate dall'utente. Ad esempio, se l'utente chiede "Chi ha creato l'opera 'Amore e Psiche'?", la query dovrebbe cercare l'opera esattamente con quel nome.
|
305 |
3) Se la query produce 0 risultati o fallisce, ritenta con un secondo tentativo.
|
306 |
4) Se la domanda è generica (tipo 'Ciao, come stai?'), rispondi breve.
|
@@ -309,7 +314,7 @@ Ecco alcune informazioni rilevanti recuperate dalla base di conoscenza:
|
|
309 |
7) Non multiline. Esempio: PREFIX base: <...> SELECT ?x WHERE {{ ... }}.
|
310 |
Esempio:
|
311 |
Utente: Chi ha creato l'opera 'Amore e Psiche'?
|
312 |
-
Risposta: PREFIX base: <http://www.semanticweb.org/lucreziamosca/ontologies/
|
313 |
FINE REGOLE
|
314 |
|
315 |
### Conversazione ###
|
@@ -378,7 +383,16 @@ def load_entity_labels(documents_file: str):
|
|
378 |
try:
|
379 |
with open(documents_file, "r", encoding="utf-8") as f:
|
380 |
document = json.load(f)
|
381 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
382 |
logger.info(f"Elenco delle etichette delle entità caricato: {entity_labels}")
|
383 |
except Exception as e:
|
384 |
logger.error(f"Errore nel caricamento delle etichette delle entità: {e}")
|
|
|
4 |
from pydantic import BaseModel
|
5 |
from fastapi import FastAPI, HTTPException
|
6 |
import rdflib
|
7 |
+
from rdflib import RDF, RDFS, OWL, URIRef
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
import faiss
|
10 |
import json
|
|
|
93 |
for entity in g.subjects(RDF.type, OWL.NamedIndividual):
|
94 |
label = g.value(entity, RDFS.label, default=str(entity))
|
95 |
description = g.value(entity, RDFS.comment, default="No description.")
|
96 |
+
# Se l'etichetta è un URI, estrai il fragment
|
97 |
+
if isinstance(label, URIRef):
|
98 |
+
label = label.split('#')[-1].replace('_', ' ')
|
99 |
+
else:
|
100 |
+
label = str(label)
|
101 |
# Estrai le proprietà dell'entità
|
102 |
entity_properties = {}
|
103 |
for predicate, obj in g.predicate_objects(entity):
|
|
|
305 |
|
306 |
### Regole Stringenti ###
|
307 |
1) Se l'utente chiede informazioni su questa ontologia, genera SEMPRE una query SPARQL in UNA SOLA RIGA, con prefix:
|
308 |
+
PREFIX base: <http://www.semanticweb.org/lucreziamosca/ontologies/progettomuseo#>
|
309 |
2) La query SPARQL deve essere precisa e cercare esattamente le entità specificate dall'utente. Ad esempio, se l'utente chiede "Chi ha creato l'opera 'Amore e Psiche'?", la query dovrebbe cercare l'opera esattamente con quel nome.
|
310 |
3) Se la query produce 0 risultati o fallisce, ritenta con un secondo tentativo.
|
311 |
4) Se la domanda è generica (tipo 'Ciao, come stai?'), rispondi breve.
|
|
|
314 |
7) Non multiline. Esempio: PREFIX base: <...> SELECT ?x WHERE {{ ... }}.
|
315 |
Esempio:
|
316 |
Utente: Chi ha creato l'opera 'Amore e Psiche'?
|
317 |
+
Risposta: PREFIX base: <http://www.semanticweb.org/lucreziamosca/ontologies/progettomuseo#> SELECT ?creatore WHERE {{ ?opera base:hasName "Amore e Psiche" . ?opera base:creatoDa ?creatore . }}
|
318 |
FINE REGOLE
|
319 |
|
320 |
### Conversazione ###
|
|
|
383 |
try:
|
384 |
with open(documents_file, "r", encoding="utf-8") as f:
|
385 |
document = json.load(f)
|
386 |
+
# Estrai etichette vere e proprie
|
387 |
+
entity_labels = []
|
388 |
+
for entity in document.get('entities', []):
|
389 |
+
label = entity.get('label', '')
|
390 |
+
if label.startswith("http://") or label.startswith("https://"):
|
391 |
+
# Estrai il fragment dell'URI
|
392 |
+
label = label.split('#')[-1].replace('_', ' ')
|
393 |
+
else:
|
394 |
+
label = label.replace('_', ' ')
|
395 |
+
entity_labels.append(label.lower())
|
396 |
logger.info(f"Elenco delle etichette delle entità caricato: {entity_labels}")
|
397 |
except Exception as e:
|
398 |
logger.error(f"Errore nel caricamento delle etichette delle entità: {e}")
|