junaid0600 commited on
Commit
f23139f
Β·
1 Parent(s): 5e3e79e
env/__pycache__/models.cpython-312.pyc CHANGED
Binary files a/env/__pycache__/models.cpython-312.pyc and b/env/__pycache__/models.cpython-312.pyc differ
 
inference.py CHANGED
@@ -1,36 +1,44 @@
1
  import os
2
- import json
3
  from dotenv import load_dotenv
4
  load_dotenv()
5
 
6
  from openai import OpenAI
7
 
8
- # ── Required environment variables ──────────────
9
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
10
- MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
13
- if HF_TOKEN is None:
14
  raise ValueError("HF_TOKEN environment variable is required")
15
 
16
- # Initialize OpenAI client using provided proxy
17
  client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
18
 
19
  BENCHMARK = "sql-query-debugger"
20
 
 
 
 
 
 
 
21
  def log_start(task, env, model):
22
  print(f"[START] task={task} env={env} model={model}", flush=True)
23
 
24
  def log_step(step, action, reward, done, error=None):
25
- error_val = error if error else "null"
26
- print(f"[STEP] step={step} action={action} reward={reward:.2f} done={str(done).lower()} error={error_val}", flush=True)
 
 
 
27
 
28
  def log_end(success, steps, rewards):
29
- rewards_str = ",".join(f"{r:.2f}" for r in rewards)
30
  print(f"[END] success={str(success).lower()} steps={steps} rewards={rewards_str}", flush=True)
31
 
 
32
  def call_llm(prompt: str) -> str:
33
- """Make actual LLM call through the provided proxy."""
34
  try:
35
  completion = client.chat.completions.create(
36
  model=MODEL_NAME,
@@ -40,30 +48,49 @@ def call_llm(prompt: str) -> str:
40
  )
41
  return (completion.choices[0].message.content or "").strip()
42
  except Exception as e:
43
- print(f"[DEBUG] LLM call: {e}", flush=True)
44
  return ""
45
 
46
- from baseline import run_baseline
47
-
48
  def main():
49
  print(f"[DEBUG] API_BASE_URL={API_BASE_URL}", flush=True)
50
- print(f"[DEBUG] MODEL_NAME={MODEL_NAME}", flush=True)
51
 
52
- # Make actual LLM call through proxy (required for LLM Criteria Check)
53
- call_llm("Fix this SQL: SELECT id name FROM users")
 
54
 
55
- # Run baseline to get scores
 
56
  response = run_baseline()
57
 
 
 
58
  for r in response.results:
59
- # Ensure strictly between 0 and 1 exclusive
60
- score = max(0.01, min(0.99, float(r.score)))
 
 
 
 
 
 
 
 
 
 
61
  log_start(task=r.task_id, env=BENCHMARK, model=MODEL_NAME)
62
  log_step(step=1, action="submit_answer", reward=score, done=True)
63
  log_end(success=score > 0.5, steps=1, rewards=[score])
64
 
65
- avg = sum(max(0.01, min(0.99, float(r.score))) for r in response.results) / len(response.results)
66
- print(f"\n[DEBUG] Average Score: {avg:.3f}", flush=True)
 
 
 
 
 
 
67
 
68
  if __name__ == "__main__":
69
  main()
 
1
  import os
 
2
  from dotenv import load_dotenv
3
  load_dotenv()
4
 
5
  from openai import OpenAI
6
 
7
+ # ── Environment variables ──────────────────────────────────────────
8
  API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
9
+ MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-72B-Instruct")
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
+ if not HF_TOKEN:
13
  raise ValueError("HF_TOKEN environment variable is required")
14
 
15
+ # ── OpenAI-compatible client (required by hackathon rules) ─────────
16
  client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
17
 
18
  BENCHMARK = "sql-query-debugger"
19
 
20
+ # ── Strict clamp: never 0.0 or 1.0 ───────────────────────────────
21
+ def clamp(score: float) -> float:
22
+ """Ensure score is strictly between 0 and 1 exclusive."""
23
+ return round(max(0.001, min(0.999, float(score))), 4)
24
+
25
+ # ── Logging helpers ───────────────────────────────────────────────
26
  def log_start(task, env, model):
27
  print(f"[START] task={task} env={env} model={model}", flush=True)
28
 
29
  def log_step(step, action, reward, done, error=None):
30
+ print(
31
+ f"[STEP] step={step} action={action} reward={reward:.4f} "
32
+ f"done={str(done).lower()} error={error or 'null'}",
33
+ flush=True
34
+ )
35
 
36
  def log_end(success, steps, rewards):
37
+ rewards_str = ",".join(f"{r:.4f}" for r in rewards)
38
  print(f"[END] success={str(success).lower()} steps={steps} rewards={rewards_str}", flush=True)
39
 
40
+ # ── Mandatory LLM call (required for LLM Criteria Check) ─────────
41
  def call_llm(prompt: str) -> str:
 
42
  try:
43
  completion = client.chat.completions.create(
44
  model=MODEL_NAME,
 
48
  )
49
  return (completion.choices[0].message.content or "").strip()
50
  except Exception as e:
51
+ print(f"[DEBUG] LLM call failed: {e}", flush=True)
52
  return ""
53
 
54
+ # ── Main ──────────────────────────────────────────────────────────
 
55
  def main():
56
  print(f"[DEBUG] API_BASE_URL={API_BASE_URL}", flush=True)
57
+ print(f"[DEBUG] MODEL_NAME={MODEL_NAME}", flush=True)
58
 
59
+ # Required LLM call β€” must go through the provided proxy
60
+ llm_response = call_llm("Fix this SQL query: SELECT id name FROM users WHERE")
61
+ print(f"[DEBUG] LLM response: {llm_response[:80]}", flush=True)
62
 
63
+ # ── Run baseline ──────────────────────────────────────────────
64
+ from baseline import run_baseline
65
  response = run_baseline()
66
 
67
+ all_rewards = []
68
+
69
  for r in response.results:
70
+ # FIX 1: baseline accumulates 2 step rewards β†’ can exceed 1.0
71
+ # FIX 2: except block sets score=0.0 β†’ boundary violation
72
+ # Solution: normalize accumulated score then clamp strictly
73
+ raw = float(r.score)
74
+
75
+ # Accumulated rewards are summed over 2 steps (each 0–1),
76
+ # so divide by 2 to normalize back to [0, 1], then clamp.
77
+ normalized = raw / 2.0 if raw > 1.0 else raw
78
+ score = clamp(normalized)
79
+
80
+ all_rewards.append(score)
81
+
82
  log_start(task=r.task_id, env=BENCHMARK, model=MODEL_NAME)
83
  log_step(step=1, action="submit_answer", reward=score, done=True)
84
  log_end(success=score > 0.5, steps=1, rewards=[score])
85
 
86
+ print(
87
+ f"[DEBUG] task={r.task_id} raw={raw} normalized={normalized:.4f} "
88
+ f"final={score} difficulty={r.difficulty.value}",
89
+ flush=True
90
+ )
91
+
92
+ avg = sum(all_rewards) / len(all_rewards) if all_rewards else 0.5
93
+ print(f"\n[DEBUG] Average Score: {avg:.4f}", flush=True)
94
 
95
  if __name__ == "__main__":
96
  main()