Shivangsinha commited on
Commit
8dcd1fd
·
verified ·
1 Parent(s): d3f2b55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -16
app.py CHANGED
@@ -24,17 +24,15 @@ class BasicAgent:
24
  def __init__(self):
25
  print("BasicAgent initialized.")
26
 
27
- # 1. Fetch the OpenRouter API Key
28
- openrouter_api_key = os.getenv("OPENROUTER_API_KEY")
29
- if not openrouter_api_key:
30
- raise ValueError("OPENROUTER_API_KEY environment variable not set in Space Secrets.")
31
 
32
- # The double "openrouter/" is not a typo!
33
- # The first tells LiteLLM to connect to OpenRouter.
34
- # The second tells OpenRouter to use its dynamic, load-balanced free pool.
35
  self.model = LiteLLMModel(
36
- model_id="openrouter/openrouter/free",
37
- api_key=openrouter_api_key,
38
  )
39
 
40
  self.tools = [
@@ -47,10 +45,10 @@ class BasicAgent:
47
  self.agent = CodeAgent(
48
  tools=self.tools,
49
  model=self.model,
50
- max_steps=6, # Reduced from 8 to save tokens and prevent quota crashes
51
  additional_authorized_imports=["datetime", "re", "json", "math", "collections"],
52
  )
53
- print("BasicAgent ready with OpenRouter (Llama-3.3-70b Free).")
54
 
55
  def __call__(self, question: str) -> str:
56
  print(f"\nAgent received question: {question[:80]}...")
@@ -58,14 +56,14 @@ class BasicAgent:
58
 
59
  for attempt in range(max_retries):
60
  try:
61
- # A tiny safety buffer per step
62
  time.sleep(2)
63
  answer = self.agent.run(question)
64
  print(f"Agent answer: {str(answer)[:200]}")
65
  return str(answer)
66
  except Exception as e:
67
  err_msg = str(e).lower()
68
- # If we hit a rate limit, pause and retry
69
  if "429" in err_msg or "rate limit" in err_msg or "too many requests" in err_msg:
70
  wait_time = 20 * (attempt + 1)
71
  print(f"Rate limit hit! Pausing for {wait_time} seconds before retrying (Attempt {attempt+1}/{max_retries})...")
@@ -125,7 +123,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
125
  print(f"Error on task {task_id}: {e}")
126
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
127
 
128
- # CRITICAL FIX: Give the API token bucket time to cool down between questions
129
  print("Cooling down for 15 seconds to prevent token exhaustion...")
130
  time.sleep(15)
131
 
@@ -154,11 +152,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
154
 
155
  # --- Build Gradio UI ---
156
  with gr.Blocks() as demo:
157
- gr.Markdown("# Basic Agent Evaluation Runner")
158
  gr.Markdown(
159
  """
160
  **Instructions:**
161
- 1. Set `OPENROUTER_API_KEY` in your Space Secrets.
162
  2. Log in with your Hugging Face account below.
163
  3. Click 'Run Evaluation & Submit' to start.
164
  """
 
24
  def __init__(self):
25
  print("BasicAgent initialized.")
26
 
27
+ # 1. Fetch the Groq API Key
28
+ groq_api_key = os.getenv("GROQ_API_KEY")
29
+ if not groq_api_key:
30
+ raise ValueError("GROQ_API_KEY environment variable not set in Space Secrets.")
31
 
32
+ # 2. Use LiteLLM to connect to Groq's Llama 3.3 70B (Extremely smart, fast, and free)
 
 
33
  self.model = LiteLLMModel(
34
+ model_id="groq/llama-3.3-70b-versatile",
35
+ api_key=groq_api_key,
36
  )
37
 
38
  self.tools = [
 
45
  self.agent = CodeAgent(
46
  tools=self.tools,
47
  model=self.model,
48
+ max_steps=7, # Capped at 7 to give it enough thought cycles without looping infinitely
49
  additional_authorized_imports=["datetime", "re", "json", "math", "collections"],
50
  )
51
+ print("BasicAgent ready with Groq (Llama-3.3-70b-versatile).")
52
 
53
  def __call__(self, question: str) -> str:
54
  print(f"\nAgent received question: {question[:80]}...")
 
56
 
57
  for attempt in range(max_retries):
58
  try:
59
+ # A small 2-second buffer per thought step
60
  time.sleep(2)
61
  answer = self.agent.run(question)
62
  print(f"Agent answer: {str(answer)[:200]}")
63
  return str(answer)
64
  except Exception as e:
65
  err_msg = str(e).lower()
66
+ # If we hit a Groq rate limit, pause and retry
67
  if "429" in err_msg or "rate limit" in err_msg or "too many requests" in err_msg:
68
  wait_time = 20 * (attempt + 1)
69
  print(f"Rate limit hit! Pausing for {wait_time} seconds before retrying (Attempt {attempt+1}/{max_retries})...")
 
123
  print(f"Error on task {task_id}: {e}")
124
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
125
 
126
+ # CRITICAL FIX: Give the API token bucket 15 seconds to cool down between questions
127
  print("Cooling down for 15 seconds to prevent token exhaustion...")
128
  time.sleep(15)
129
 
 
152
 
153
  # --- Build Gradio UI ---
154
  with gr.Blocks() as demo:
155
+ gr.Markdown("# Basic Agent Evaluation Runner (Groq Edition)")
156
  gr.Markdown(
157
  """
158
  **Instructions:**
159
+ 1. Set `GROQ_API_KEY` in your Space Secrets.
160
  2. Log in with your Hugging Face account below.
161
  3. Click 'Run Evaluation & Submit' to start.
162
  """