Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
-
from fastapi import FastAPI, HTTPException
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from rdflib import Graph
|
| 5 |
from pydantic import BaseModel
|
| 6 |
|
| 7 |
# Configurazione API Hugging Face
|
| 8 |
API_KEY = os.getenv("HF_API_KEY")
|
| 9 |
-
if not API_KEY:
|
| 10 |
-
raise ValueError("API Key non trovata. Assicurati che sia definita come variabile d'ambiente 'HF_API_KEY'.")
|
| 11 |
-
|
| 12 |
client = InferenceClient(api_key=API_KEY)
|
| 13 |
|
| 14 |
# File RDF
|
|
@@ -30,7 +27,7 @@ def validate_sparql_query(query, rdf_data):
|
|
| 30 |
g.parse(data=rdf_data, format="xml")
|
| 31 |
g.query(query)
|
| 32 |
return True
|
| 33 |
-
except Exception
|
| 34 |
return False
|
| 35 |
|
| 36 |
# FastAPI app
|
|
@@ -64,15 +61,18 @@ def generate_response(message, max_tokens, temperature):
|
|
| 64 |
|
| 65 |
try:
|
| 66 |
# Usa il metodo corretto per invocare il modello Hugging Face
|
| 67 |
-
response = client.
|
| 68 |
model="Qwen/Qwen2.5-72B-Instruct",
|
| 69 |
inputs=full_prompt,
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
-
return response.strip()
|
| 74 |
except Exception as e:
|
| 75 |
raise HTTPException(status_code=500, detail=f"Errore nell'elaborazione: {str(e)}")
|
|
|
|
| 76 |
# Endpoint per generare query SPARQL
|
| 77 |
@app.post("/generate-query/")
|
| 78 |
async def generate_query(request: QueryRequest):
|
|
@@ -90,4 +90,4 @@ async def generate_query(request: QueryRequest):
|
|
| 90 |
# Endpoint per verificare se il server è attivo
|
| 91 |
@app.get("/")
|
| 92 |
async def root():
|
| 93 |
-
return {"message": "Il server è attivo e pronto a generare query SPARQL!"}
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from rdflib import Graph
|
| 5 |
from pydantic import BaseModel
|
| 6 |
|
| 7 |
# Configurazione API Hugging Face
|
| 8 |
API_KEY = os.getenv("HF_API_KEY")
|
|
|
|
|
|
|
|
|
|
| 9 |
client = InferenceClient(api_key=API_KEY)
|
| 10 |
|
| 11 |
# File RDF
|
|
|
|
| 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
|
|
|
|
| 61 |
|
| 62 |
try:
|
| 63 |
# Usa il metodo corretto per invocare il modello Hugging Face
|
| 64 |
+
response = client.post(
|
| 65 |
model="Qwen/Qwen2.5-72B-Instruct",
|
| 66 |
inputs=full_prompt,
|
| 67 |
+
parameters={
|
| 68 |
+
"max_new_tokens": max_tokens,
|
| 69 |
+
"temperature": temperature
|
| 70 |
+
}
|
| 71 |
)
|
| 72 |
+
return response.get("generated_text", "").strip()
|
| 73 |
except Exception as e:
|
| 74 |
raise HTTPException(status_code=500, detail=f"Errore nell'elaborazione: {str(e)}")
|
| 75 |
+
|
| 76 |
# Endpoint per generare query SPARQL
|
| 77 |
@app.post("/generate-query/")
|
| 78 |
async def generate_query(request: QueryRequest):
|
|
|
|
| 90 |
# Endpoint per verificare se il server è attivo
|
| 91 |
@app.get("/")
|
| 92 |
async def root():
|
| 93 |
+
return {"message": "Il server è attivo e pronto a generare query SPARQL!"}
|