Shivangsinha commited on
Commit
3098349
·
verified ·
1 Parent(s): 24811b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -24,13 +24,10 @@ class BasicAgent:
24
  def __init__(self):
25
  print("BasicAgent initialized.")
26
 
27
- # 1. Fetch the NEW Hugging Face token from secrets
28
- new_hf_token = os.getenv("HF_TOKEN")
29
  if not new_hf_token:
30
- raise ValueError("HF_TOKEN environment variable not set in Space Secrets.")
31
 
32
- # 2. Connect to Hugging Face's free Serverless Inference API
33
- # Qwen2.5-Coder-32B-Instruct is the recommended model for this course
34
  self.model = InferenceClientModel(
35
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
36
  token=new_hf_token,
@@ -46,8 +43,9 @@ class BasicAgent:
46
  self.agent = CodeAgent(
47
  tools=self.tools,
48
  model=self.model,
49
- max_steps=7,
50
- additional_authorized_imports=["datetime", "re", "json", "math", "collections"],
 
51
  )
52
  print("BasicAgent ready with Hugging Face (Qwen2.5-Coder-32B).")
53
 
@@ -65,7 +63,7 @@ class BasicAgent:
65
  err_msg = str(e).lower()
66
  if "429" in err_msg or "rate limit" in err_msg or "too many requests" in err_msg:
67
  wait_time = 20 * (attempt + 1)
68
- print(f"Rate limit hit! Pausing for {wait_time} seconds before retrying (Attempt {attempt+1}/{max_retries})...")
69
  time.sleep(wait_time)
70
  else:
71
  print(f"Agent error processing question: {e}")
@@ -111,11 +109,31 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
111
  for i, item in enumerate(questions_data):
112
  task_id = item.get("task_id")
113
  question_text = item.get("question")
 
 
 
 
114
  if not task_id or not question_text:
115
  continue
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  try:
118
- submitted_answer = agent(question_text)
 
119
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
120
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
121
  except Exception as e:
@@ -150,12 +168,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
150
 
151
  # --- Build Gradio UI ---
152
  with gr.Blocks() as demo:
153
- gr.Markdown("# Basic Agent Evaluation Runner (Hugging Face Edition)")
154
  gr.Markdown(
155
  """
156
  **Instructions:**
157
- 1. Set `NEW_HF_TOKEN` in your Space Secrets using a secondary Hugging Face account.
158
- 2. Log in with your primary Hugging Face account below.
159
  3. Click 'Run Evaluation & Submit' to start.
160
  """
161
  )
 
24
  def __init__(self):
25
  print("BasicAgent initialized.")
26
 
27
+ new_hf_token = os.getenv("NEW_HF_TOKEN")
 
28
  if not new_hf_token:
29
+ raise ValueError("NEW_HF_TOKEN environment variable not set in Space Secrets.")
30
 
 
 
31
  self.model = InferenceClientModel(
32
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
33
  token=new_hf_token,
 
43
  self.agent = CodeAgent(
44
  tools=self.tools,
45
  model=self.model,
46
+ max_steps=10,
47
+ # CRITICAL FIX: Added pandas and requests so the agent can download and read Excel/CSV files!
48
+ additional_authorized_imports=["datetime", "re", "json", "math", "collections", "pandas", "requests"],
49
  )
50
  print("BasicAgent ready with Hugging Face (Qwen2.5-Coder-32B).")
51
 
 
63
  err_msg = str(e).lower()
64
  if "429" in err_msg or "rate limit" in err_msg or "too many requests" in err_msg:
65
  wait_time = 20 * (attempt + 1)
66
+ print(f"Rate limit hit! Pausing for {wait_time} seconds before retrying...")
67
  time.sleep(wait_time)
68
  else:
69
  print(f"Agent error processing question: {e}")
 
109
  for i, item in enumerate(questions_data):
110
  task_id = item.get("task_id")
111
  question_text = item.get("question")
112
+
113
+ # CRITICAL FIX 1: Grab the hidden file URL if the server provides one
114
+ file_url = item.get("file_url")
115
+
116
  if not task_id or not question_text:
117
  continue
118
 
119
+ # CRITICAL FIX 2: Inject the file URL into the agent's prompt so it can download it
120
+ if file_url:
121
+ question_text += f"\n\n[IMPORTANT: This task requires analyzing an attached file. You MUST download or read it directly from this URL: {file_url} using your Python tool.]"
122
+
123
+ # CRITICAL FIX 3: Threaten the agent to act like a strict robot to pass the automated grader
124
+ strict_prompt = (
125
+ f"{question_text}\n\n"
126
+ "CRITICAL SUBMISSION INSTRUCTIONS:\n"
127
+ "The system evaluating your answer is a strict automated parser.\n"
128
+ "1. You MUST output ONLY the final requested answer.\n"
129
+ "2. DO NOT include any conversational text, explanations, or reasoning in your final output.\n"
130
+ "3. If the answer is a name, number, or short string, output ONLY that exact string.\n"
131
+ "4. For numbers, do not include symbols unless explicitly requested."
132
+ )
133
+
134
  try:
135
+ # We pass the strict prompt instead of the raw question
136
+ submitted_answer = agent(strict_prompt)
137
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
138
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
139
  except Exception as e:
 
168
 
169
  # --- Build Gradio UI ---
170
  with gr.Blocks() as demo:
171
+ gr.Markdown("# Basic Agent Evaluation Runner")
172
  gr.Markdown(
173
  """
174
  **Instructions:**
175
+ 1. Ensure your `NEW_HF_TOKEN` is set.
176
+ 2. Log in below.
177
  3. Click 'Run Evaluation & Submit' to start.
178
  """
179
  )