Update app.py
Browse files
app.py
CHANGED
@@ -25,9 +25,11 @@ def validate_sparql_query(query, rdf_data):
|
|
25 |
try:
|
26 |
g = Graph()
|
27 |
g.parse(data=rdf_data, format="xml")
|
|
|
28 |
g.query(query)
|
29 |
return True
|
30 |
-
except Exception:
|
|
|
31 |
return False
|
32 |
|
33 |
# FastAPI app
|
@@ -92,7 +94,13 @@ async def generate_query(request: QueryRequest):
|
|
92 |
if response.startswith("SELECT") or response.startswith("ASK"):
|
93 |
is_valid = validate_sparql_query(response, rdf_context)
|
94 |
if not is_valid:
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
explanation = "La query generata è: " + response.replace("\n", " ").strip() + ". "
|
98 |
explanation += "Questa query è stata progettata per estrarre informazioni specifiche dai dati RDF, consentendo di rispondere alla tua domanda. Risultati ottenuti da questa query possono includere dettagli come entità, relazioni o attributi collegati al contesto fornito."
|
|
|
25 |
try:
|
26 |
g = Graph()
|
27 |
g.parse(data=rdf_data, format="xml")
|
28 |
+
# Prova a eseguire la query
|
29 |
g.query(query)
|
30 |
return True
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Errore durante la validazione della query SPARQL: {e}")
|
33 |
return False
|
34 |
|
35 |
# FastAPI app
|
|
|
94 |
if response.startswith("SELECT") or response.startswith("ASK"):
|
95 |
is_valid = validate_sparql_query(response, rdf_context)
|
96 |
if not is_valid:
|
97 |
+
# Log e messaggio per il client
|
98 |
+
print("Query non valida rispetto al file RDF:")
|
99 |
+
print(response)
|
100 |
+
return {
|
101 |
+
"query": response.replace("\n", " ").strip(),
|
102 |
+
"explanation": "La query generata non è valida rispetto al file RDF fornito. Potrebbe essere necessario correggerla manualmente."
|
103 |
+
}
|
104 |
|
105 |
explanation = "La query generata è: " + response.replace("\n", " ").strip() + ". "
|
106 |
explanation += "Questa query è stata progettata per estrarre informazioni specifiche dai dati RDF, consentendo di rispondere alla tua domanda. Risultati ottenuti da questa query possono includere dettagli come entità, relazioni o attributi collegati al contesto fornito."
|