AshenClock commited on
Commit
cb51311
·
verified ·
1 Parent(s): d3f5358

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
  import logging
3
- from typing import Optional
4
  from pydantic import BaseModel
5
  from fastapi import FastAPI, HTTPException
6
  import rdflib
@@ -369,10 +369,30 @@ async def call_hf_model(prompt: str, temperature: float = 0.5, max_tokens: int =
369
  logger.error(f"Errore nella chiamata all'API Hugging Face tramite requests: {e}")
370
  raise HTTPException(status_code=500, detail=str(e))
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
  def is_ontology_related(query: str) -> bool:
373
  """Determina se la domanda è pertinente all'ontologia."""
 
374
  keywords = ["opera", "museo", "stanza", "tour", "visitatore", "biglietto", "guida", "evento", "agente"]
375
- return any(keyword.lower() in query.lower() for keyword in keywords)
 
 
 
 
376
 
377
  # Prepara i file necessari per RAG
378
  prepare_retrieval()
@@ -380,6 +400,9 @@ prepare_retrieval()
380
  # Carica il 'sunto' di classi, proprietà ed entità
381
  knowledge_text = extract_classes_and_properties(RDF_FILE)
382
 
 
 
 
383
  app = FastAPI()
384
 
385
  class QueryRequest(BaseModel):
 
1
  import os
2
  import logging
3
+ from typing import List
4
  from pydantic import BaseModel
5
  from fastapi import FastAPI, HTTPException
6
  import rdflib
 
369
  logger.error(f"Errore nella chiamata all'API Hugging Face tramite requests: {e}")
370
  raise HTTPException(status_code=500, detail=str(e))
371
 
372
+ # Variabile globale per le etichette delle entità
373
+ entity_labels: List[str] = []
374
+
375
+ def load_entity_labels(documents_file: str):
376
+ """Carica le etichette delle entità dal file documents.json."""
377
+ global entity_labels
378
+ try:
379
+ with open(documents_file, "r", encoding="utf-8") as f:
380
+ document = json.load(f)
381
+ entity_labels = [entity['label'].lower() for entity in document['entities']]
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}")
385
+ entity_labels = []
386
+
387
  def is_ontology_related(query: str) -> bool:
388
  """Determina se la domanda è pertinente all'ontologia."""
389
+ query_lower = query.lower()
390
  keywords = ["opera", "museo", "stanza", "tour", "visitatore", "biglietto", "guida", "evento", "agente"]
391
+ if any(keyword in query_lower for keyword in keywords):
392
+ return True
393
+ if any(entity in query_lower for entity in entity_labels):
394
+ return True
395
+ return False
396
 
397
  # Prepara i file necessari per RAG
398
  prepare_retrieval()
 
400
  # Carica il 'sunto' di classi, proprietà ed entità
401
  knowledge_text = extract_classes_and_properties(RDF_FILE)
402
 
403
+ # Carica le etichette delle entità
404
+ load_entity_labels(DOCUMENTS_FILE)
405
+
406
  app = FastAPI()
407
 
408
  class QueryRequest(BaseModel):