Mattimax commited on
Commit
8398ebb
·
verified ·
1 Parent(s): 19dd1f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -23
app.py CHANGED
@@ -33,31 +33,42 @@ def load_boolq_dataset(num_samples=DEFAULT_NUM_SAMPLES):
33
 
34
  def build_boolq_prompt(passage, question):
35
  """
36
- Costruisce un prompt generico per LLM per BoolQ.
37
- Il modello deve rispondere solo 'yes' o 'no'.
38
  """
39
  prompt = (
40
- "You are a question answering system. "
41
- "Answer strictly with 'yes' or 'no'.\n\n"
42
- f"Passage: {passage}\n"
43
- f"Question: {question}\n"
44
- "Answer:"
45
  )
46
  return prompt
47
 
48
 
49
  def parse_yes_no(output_text):
50
  """
51
- Estrae 'yes' o 'no' dall'output del modello.
52
- Se non è chiaro, restituisce None.
 
53
  """
54
  text = output_text.strip().lower()
55
- # prendi solo la prima parola
56
- first = text.split()[0] if text else ""
 
 
 
 
 
 
 
 
 
 
57
  if first.startswith("yes"):
58
  return True
59
  if first.startswith("no"):
60
  return False
 
61
  return None
62
 
63
 
@@ -105,14 +116,19 @@ def evaluate_model_on_boolq(model_name, num_samples=DEFAULT_NUM_SAMPLES, max_new
105
  temperature=0.0,
106
  )
107
  t1 = time.time()
108
- gen_text = tokenizer.decode(output_ids[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
 
 
 
109
 
110
  pred = parse_yes_no(gen_text)
111
- if pred is not None:
112
- if pred == label:
113
- correct += 1
114
- total += 1
115
- times.append(t1 - t0)
 
 
116
 
117
  if total == 0:
118
  accuracy = 0.0
@@ -197,11 +213,18 @@ def run_benchmark_ui(
197
  try:
198
  res = evaluate_model_on_boolq(name, num_samples=num_samples)
199
  results.append(res)
 
 
 
 
 
 
 
200
  logs.append(
201
  f" - Esempi valutati: {res['num_samples']}\n"
202
  f" - Accuracy: {res['accuracy']:.3f}\n"
203
- f" - Tempo medio per esempio (s): "
204
- f"{res['avg_time_per_sample_sec']:.3f}" if res['avg_time_per_sample_sec'] is not None else "N/A"
205
  )
206
  except Exception as e:
207
  logs.append(f" ERRORE: {e}")
@@ -221,10 +244,10 @@ def run_benchmark_ui(
221
  # Costruzione interfaccia Gradio
222
  # =========================
223
 
224
- with gr.Blocks(title="LLM Benchmark Space - BoolQ") as demo:
225
  gr.Markdown(
226
  """
227
- # 🔍 LLM Benchmark Space (BoolQ)
228
 
229
  Inserisci i nomi dei modelli Hugging Face (es. `meta-llama/Meta-Llama-3-8B-Instruct`)
230
  e confrontali su un subset del dataset **BoolQ** (domande sì/no).
@@ -232,6 +255,8 @@ with gr.Blocks(title="LLM Benchmark Space - BoolQ") as demo:
232
  - Minimo **2 modelli**
233
  - Puoi aggiungere fino a **5 modelli** con il pulsante **"+ Aggiungi modello"**
234
  - Output: tabella con **accuracy**, numero di esempi e tempi
 
 
235
  """
236
  )
237
 
@@ -241,13 +266,13 @@ with gr.Blocks(title="LLM Benchmark Space - BoolQ") as demo:
241
 
242
  model_1 = gr.Textbox(
243
  label="Modello 1",
244
- placeholder="es. meta-llama/Meta-Llama-3-8B-Instruct",
245
  value="",
246
  visible=True,
247
  )
248
  model_2 = gr.Textbox(
249
  label="Modello 2",
250
- placeholder="es. mistralai/Mistral-7B-Instruct-v0.3",
251
  value="",
252
  visible=True,
253
  )
 
33
 
34
  def build_boolq_prompt(passage, question):
35
  """
36
+ Prompt in italiano: il modello deve rispondere solo 'sì' o 'no'.
 
37
  """
38
  prompt = (
39
+ "Sei un sistema di question answering. "
40
+ "Rispondi strettamente solo con '' o 'no'.\n\n"
41
+ f"Testo: {passage}\n"
42
+ f"Domanda: {question}\n"
43
+ "Risposta:"
44
  )
45
  return prompt
46
 
47
 
48
  def parse_yes_no(output_text):
49
  """
50
+ Estrae 'sì/si' o 'no' dall'output del modello.
51
+ Supporta anche 'yes'/'no' per modelli inglesi.
52
+ Ritorna True per sì/yes, False per no, None se non riconosciuto.
53
  """
54
  text = output_text.strip().lower()
55
+ if not text:
56
+ return None
57
+
58
+ first = text.split()[0]
59
+
60
+ # italiano
61
+ if first.startswith("sì") or first.startswith("si"):
62
+ return True
63
+ if first.startswith("no"):
64
+ return False
65
+
66
+ # inglese
67
  if first.startswith("yes"):
68
  return True
69
  if first.startswith("no"):
70
  return False
71
+
72
  return None
73
 
74
 
 
116
  temperature=0.0,
117
  )
118
  t1 = time.time()
119
+ gen_text = tokenizer.decode(
120
+ output_ids[0][inputs["input_ids"].shape[-1]:],
121
+ skip_special_tokens=True,
122
+ )
123
 
124
  pred = parse_yes_no(gen_text)
125
+
126
+ # Contiamo sempre l'esempio, anche se il modello non risponde in modo valido
127
+ total += 1
128
+ times.append(t1 - t0)
129
+
130
+ if pred is not None and pred == label:
131
+ correct += 1
132
 
133
  if total == 0:
134
  accuracy = 0.0
 
213
  try:
214
  res = evaluate_model_on_boolq(name, num_samples=num_samples)
215
  results.append(res)
216
+
217
+ avg_time_str = (
218
+ f"{res['avg_time_per_sample_sec']:.3f}"
219
+ if res['avg_time_per_sample_sec'] is not None
220
+ else "N/A"
221
+ )
222
+
223
  logs.append(
224
  f" - Esempi valutati: {res['num_samples']}\n"
225
  f" - Accuracy: {res['accuracy']:.3f}\n"
226
+ f" - Tempo medio per esempio (s): {avg_time_str}\n"
227
+ f" - Tempo totale (s): {res['total_time_sec']:.3f}"
228
  )
229
  except Exception as e:
230
  logs.append(f" ERRORE: {e}")
 
244
  # Costruzione interfaccia Gradio
245
  # =========================
246
 
247
+ with gr.Blocks(title="LLM Benchmark Space - BoolQ (IT)") as demo:
248
  gr.Markdown(
249
  """
250
+ # 🔍 LLM Benchmark Space (BoolQ, IT)
251
 
252
  Inserisci i nomi dei modelli Hugging Face (es. `meta-llama/Meta-Llama-3-8B-Instruct`)
253
  e confrontali su un subset del dataset **BoolQ** (domande sì/no).
 
255
  - Minimo **2 modelli**
256
  - Puoi aggiungere fino a **5 modelli** con il pulsante **"+ Aggiungi modello"**
257
  - Output: tabella con **accuracy**, numero di esempi e tempi
258
+
259
+ I prompt sono in **italiano** e il modello deve rispondere solo con **"sì"** o **"no"**.
260
  """
261
  )
262
 
 
266
 
267
  model_1 = gr.Textbox(
268
  label="Modello 1",
269
+ placeholder="es. Mattimax/DACMini-IT",
270
  value="",
271
  visible=True,
272
  )
273
  model_2 = gr.Textbox(
274
  label="Modello 2",
275
+ placeholder="es. Mattimax/DAC60M",
276
  value="",
277
  visible=True,
278
  )