AshenClock commited on
Commit
8b2fc25
·
verified ·
1 Parent(s): 28d2f0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -51
app.py CHANGED
@@ -10,6 +10,10 @@ from sentence_transformers import SentenceTransformer
10
  import faiss
11
  import json
12
  import numpy as np
 
 
 
 
13
 
14
  logging.basicConfig(
15
  level=logging.DEBUG,
@@ -23,7 +27,7 @@ if not API_KEY:
23
  logger.error("HF_API_KEY non impostata.")
24
  raise EnvironmentError("HF_API_KEY non impostata.")
25
 
26
- client = InferenceClient(api_key=API_KEY)
27
 
28
  RDF_FILE = "Ontologia.rdf"
29
  HF_MODEL = "Qwen/Qwen2.5-72B-Instruct"
@@ -137,7 +141,7 @@ async def call_hf_model(messages, temperature=0.5, max_tokens=1024)->str:
137
  max_tokens=max_tokens,
138
  top_p=0.9
139
  )
140
- raw=resp["choices"][0]["message"]["content"]
141
  # Forziamo la query su linea singola se multiline
142
  single_line = " ".join(raw.splitlines())
143
  logger.debug(f"Risposta HF single-line: {single_line}")
@@ -146,100 +150,101 @@ async def call_hf_model(messages, temperature=0.5, max_tokens=1024)->str:
146
  logger.error(f"HuggingFace error: {e}")
147
  raise HTTPException(status_code=500, detail=str(e))
148
 
149
- app=FastAPI()
150
 
151
  class QueryRequest(BaseModel):
152
- message:str
153
- max_tokens:int=1024
154
- temperature:float=0.5
155
 
156
  @app.post("/generate-response/")
157
- async def generate_response(req:QueryRequest):
158
- user_input=req.message
159
  logger.info(f"Utente dice: {user_input}")
160
 
161
  # Recupera documenti rilevanti usando RAG
162
  relevant_docs = retrieve_relevant_documents(user_input, top_k=3)
163
  retrieved_text = "\n".join([doc['text'] for doc in relevant_docs])
164
 
165
- sys_msg=create_system_message(knowledge_text, retrieved_text)
166
- msgs=[
167
- {"role":"system","content":sys_msg},
168
- {"role":"user","content":user_input}
169
  ]
 
170
  # Primo tentativo
171
- r1=await call_hf_model(msgs, req.temperature, req.max_tokens)
172
  logger.info(f"PRIMA RISPOSTA:\n{r1}")
173
 
174
  # Se non parte con "PREFIX base:"
175
  if not r1.startswith("PREFIX base:"):
176
- sc=f"Non hai risposto con query SPARQL su una sola riga. Riprova. Domanda: {user_input}"
177
- msgs2=[
178
- {"role":"system","content":sys_msg},
179
- {"role":"assistant","content":r1},
180
- {"role":"user","content":sc}
181
  ]
182
- r2=await call_hf_model(msgs2,req.temperature,req.max_tokens)
183
  logger.info(f"SECONDA RISPOSTA:\n{r2}")
184
  if r2.startswith("PREFIX base:"):
185
- sparql_query=r2
186
  else:
187
- return {"type":"NATURAL","response": r2}
188
  else:
189
- sparql_query=r1
190
 
191
  # Esegui la query con rdflib
192
- g=rdflib.Graph()
193
  try:
194
- g.parse(RDF_FILE,format="xml")
195
  except Exception as e:
196
  logger.error(f"Parsing RDF error: {e}")
197
- return {"type":"ERROR","response":f"Parsing RDF error: {e}"}
198
 
199
  try:
200
- results=g.query(sparql_query)
201
  except Exception as e:
202
- fallback=f"La query SPARQL ha fallito. Riprova. Domanda: {user_input}"
203
- msgs3=[
204
- {"role":"system","content":sys_msg},
205
- {"role":"assistant","content":sparql_query},
206
- {"role":"user","content":fallback}
207
  ]
208
- r3=await call_hf_model(msgs3,req.temperature,req.max_tokens)
209
  if r3.startswith("PREFIX base:"):
210
- sparql_query=r3
211
  try:
212
- results=g.query(sparql_query)
213
  except Exception as e2:
214
- return {"type":"ERROR","response":f"Query fallita di nuovo: {e2}"}
215
  else:
216
- return {"type":"NATURAL","response":r3}
217
 
218
- if len(results)==0:
219
- return {"type":"NATURAL","sparql_query":sparql_query,"response":"Nessun risultato."}
220
 
221
  # Confeziona risultati
222
- row_list=[]
223
  for row in results:
224
- row_str=", ".join([f"{k}:{v}" for k,v in row.asdict().items()])
225
  row_list.append(row_str)
226
- results_str="\n".join(row_list)
227
 
228
  # Spiegazione
229
- exp_prompt=create_explanation_prompt(results_str)
230
- msgs4=[
231
- {"role":"system","content":exp_prompt},
232
- {"role":"user","content":""}
233
  ]
234
- explanation=await call_hf_model(msgs4,req.temperature,req.max_tokens)
235
 
236
  return {
237
- "type":"NATURAL",
238
- "sparql_query":sparql_query,
239
- "sparql_results":row_list,
240
- "explanation":explanation
241
  }
242
 
243
  @app.get("/")
244
  def home():
245
- return {"message":"Prompt lascia libertà su come chiamare la proprietà del materiale, ma suggerisce un possibile 'materialeOpera'."}
 
10
  import faiss
11
  import json
12
  import numpy as np
13
+ from dotenv import load_dotenv
14
+
15
+ # Carica le variabili d'ambiente
16
+ load_dotenv()
17
 
18
  logging.basicConfig(
19
  level=logging.DEBUG,
 
27
  logger.error("HF_API_KEY non impostata.")
28
  raise EnvironmentError("HF_API_KEY non impostata.")
29
 
30
+ client = InferenceClient(token=API_KEY)
31
 
32
  RDF_FILE = "Ontologia.rdf"
33
  HF_MODEL = "Qwen/Qwen2.5-72B-Instruct"
 
141
  max_tokens=max_tokens,
142
  top_p=0.9
143
  )
144
+ raw = resp["choices"][0]["message"]["content"]
145
  # Forziamo la query su linea singola se multiline
146
  single_line = " ".join(raw.splitlines())
147
  logger.debug(f"Risposta HF single-line: {single_line}")
 
150
  logger.error(f"HuggingFace error: {e}")
151
  raise HTTPException(status_code=500, detail=str(e))
152
 
153
+ app = FastAPI()
154
 
155
  class QueryRequest(BaseModel):
156
+ message: str
157
+ max_tokens: int = 1024
158
+ temperature: float = 0.5
159
 
160
  @app.post("/generate-response/")
161
+ async def generate_response(req: QueryRequest):
162
+ user_input = req.message
163
  logger.info(f"Utente dice: {user_input}")
164
 
165
  # Recupera documenti rilevanti usando RAG
166
  relevant_docs = retrieve_relevant_documents(user_input, top_k=3)
167
  retrieved_text = "\n".join([doc['text'] for doc in relevant_docs])
168
 
169
+ sys_msg = create_system_message(knowledge_text, retrieved_text)
170
+ msgs = [
171
+ {"role": "system", "content": sys_msg},
172
+ {"role": "user", "content": user_input}
173
  ]
174
+
175
  # Primo tentativo
176
+ r1 = await call_hf_model(msgs, req.temperature, req.max_tokens)
177
  logger.info(f"PRIMA RISPOSTA:\n{r1}")
178
 
179
  # Se non parte con "PREFIX base:"
180
  if not r1.startswith("PREFIX base:"):
181
+ sc = f"Non hai risposto con query SPARQL su una sola riga. Riprova. Domanda: {user_input}"
182
+ msgs2 = [
183
+ {"role": "system", "content": sys_msg},
184
+ {"role": "assistant", "content": r1},
185
+ {"role": "user", "content": sc}
186
  ]
187
+ r2 = await call_hf_model(msgs2, req.temperature, req.max_tokens)
188
  logger.info(f"SECONDA RISPOSTA:\n{r2}")
189
  if r2.startswith("PREFIX base:"):
190
+ sparql_query = r2
191
  else:
192
+ return {"type": "NATURAL", "response": r2}
193
  else:
194
+ sparql_query = r1
195
 
196
  # Esegui la query con rdflib
197
+ g = rdflib.Graph()
198
  try:
199
+ g.parse(RDF_FILE, format="xml")
200
  except Exception as e:
201
  logger.error(f"Parsing RDF error: {e}")
202
+ return {"type": "ERROR", "response": f"Parsing RDF error: {e}"}
203
 
204
  try:
205
+ results = g.query(sparql_query)
206
  except Exception as e:
207
+ fallback = f"La query SPARQL ha fallito. Riprova. Domanda: {user_input}"
208
+ msgs3 = [
209
+ {"role": "system", "content": sys_msg},
210
+ {"role": "assistant", "content": sparql_query},
211
+ {"role": "user", "content": fallback}
212
  ]
213
+ r3 = await call_hf_model(msgs3, req.temperature, req.max_tokens)
214
  if r3.startswith("PREFIX base:"):
215
+ sparql_query = r3
216
  try:
217
+ results = g.query(sparql_query)
218
  except Exception as e2:
219
+ return {"type": "ERROR", "response": f"Query fallita di nuovo: {e2}"}
220
  else:
221
+ return {"type": "NATURAL", "response": r3}
222
 
223
+ if len(results) == 0:
224
+ return {"type": "NATURAL", "sparql_query": sparql_query, "response": "Nessun risultato."}
225
 
226
  # Confeziona risultati
227
+ row_list = []
228
  for row in results:
229
+ row_str = ", ".join([f"{k}:{v}" for k, v in row.asdict().items()])
230
  row_list.append(row_str)
231
+ results_str = "\n".join(row_list)
232
 
233
  # Spiegazione
234
+ exp_prompt = create_explanation_prompt(results_str)
235
+ msgs4 = [
236
+ {"role": "system", "content": exp_prompt},
237
+ {"role": "user", "content": ""}
238
  ]
239
+ explanation = await call_hf_model(msgs4, req.temperature, req.max_tokens)
240
 
241
  return {
242
+ "type": "NATURAL",
243
+ "sparql_query": sparql_query,
244
+ "sparql_results": row_list,
245
+ "explanation": explanation
246
  }
247
 
248
  @app.get("/")
249
  def home():
250
+ return {"message": "Prompt lascia libertà su come chiamare la proprietà del materiale, ma suggerisce un possibile 'materialeOpera'."}