mianf4884 commited on
Commit
2dd087e
·
verified ·
1 Parent(s): 21ea6c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, VisitWebpageTool
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -10,32 +10,34 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
  # --- Agent Definition ---
11
  class AgentArchitect:
12
  def __init__(self):
13
- hf_token = os.getenv("HF_TOKEN")
 
14
 
15
- # We use the 7B Coder model. It is almost certainly on the FREE Serverless tier.
16
- # This avoids hitting the 'nscale' or other paid Inference Providers.
17
- self.model = InferenceClientModel(
18
- model_id="Qwen/Qwen2.5-Coder-7B-Instruct",
19
- token=hf_token
 
 
 
20
  )
21
 
22
- # Essential tools for GAIA: Search + Deep Scrapping
23
  self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
24
 
25
- # CodeAgent is the best scaffold for technical tasks
26
  self.agent = CodeAgent(
27
  tools=self.tools,
28
  model=self.model,
29
- add_base_tools=True # Gives it Python for math/data processing
30
  )
31
 
32
  def __call__(self, question: str) -> str:
33
  try:
34
- # We add a strict formatting instruction to help with Exact Match scoring
35
  prompt = (
36
  f"{question}\n\n"
37
- "Final Answer Requirement: Use your tools to find the information. "
38
- "Provide ONLY the final answer concisely (e.g., just the name, number, or list)."
39
  )
40
  result = self.agent.run(prompt)
41
  return str(result)
@@ -55,6 +57,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
55
 
56
  try:
57
  agent_instance = AgentArchitect()
 
58
  response = requests.get(questions_url, timeout=15)
59
  response.raise_for_status()
60
  questions_data = response.json()
@@ -62,17 +65,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
62
  results_log = []
63
  answers_payload = []
64
 
65
- # The agent will now process the questions using the free tier model
66
  for item in questions_data:
67
  task_id = item.get("task_id")
68
  question_text = item.get("question")
 
 
69
  submitted_answer = agent_instance(question_text)
 
70
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
71
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
72
 
73
- # Submission Logic
74
  agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
75
- submission_data = {"username": username.strip(), "agent_code": agent_code_link, "answers": answers_payload}
 
 
 
 
76
 
77
  submit_response = requests.post(submit_url, json=submission_data, timeout=60)
78
  submit_response.raise_for_status()
@@ -81,20 +89,20 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
81
  final_status = (
82
  f"Submission Successful!\n"
83
  f"User: {result_data.get('username')}\n"
84
- f"Score: {result_data.get('score')}% \n"
85
  f"Message: {result_data.get('message')}"
86
  )
87
  return final_status, pd.DataFrame(results_log)
88
 
89
  except Exception as e:
90
- return f"Error during run: {e}", None
91
 
92
  # --- Gradio UI ---
93
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
94
- gr.Markdown("# 🚀 Professional Agent Evaluator (Free Tier Optimized)")
95
  gr.LoginButton()
96
  run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
97
- status_output = gr.Textbox(label="Submission Result", lines=5)
98
  results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
99
 
100
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, VisitWebpageTool
6
 
7
  # --- Constants ---
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
10
  # --- Agent Definition ---
11
  class AgentArchitect:
12
  def __init__(self):
13
+ # SECURE: Fetches the OpenAI API key from your Space Secrets
14
+ openai_api_key = os.getenv("OPENAI_API_KEY")
15
 
16
+ if not openai_api_key:
17
+ print("CRITICAL: OPENAI_API_KEY is missing. Please add it to your Space Secrets!")
18
+
19
+ # Bypassing Hugging Face billing completely.
20
+ # We use gpt-4o-mini because it is highly capable at coding and very cost-effective.
21
+ self.model = LiteLLMModel(
22
+ model_id="gpt-4o-mini",
23
+ api_key=openai_api_key
24
  )
25
 
 
26
  self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()]
27
 
 
28
  self.agent = CodeAgent(
29
  tools=self.tools,
30
  model=self.model,
31
+ add_base_tools=True
32
  )
33
 
34
  def __call__(self, question: str) -> str:
35
  try:
36
+ # Enforce Exact Match scoring formatting
37
  prompt = (
38
  f"{question}\n\n"
39
+ f"Instructions: Think step-by-step. Solve the problem using your tools. "
40
+ f"Provide ONLY the final, concise answer."
41
  )
42
  result = self.agent.run(prompt)
43
  return str(result)
 
57
 
58
  try:
59
  agent_instance = AgentArchitect()
60
+
61
  response = requests.get(questions_url, timeout=15)
62
  response.raise_for_status()
63
  questions_data = response.json()
 
65
  results_log = []
66
  answers_payload = []
67
 
 
68
  for item in questions_data:
69
  task_id = item.get("task_id")
70
  question_text = item.get("question")
71
+
72
+ # Agent logic with OpenAI's brain
73
  submitted_answer = agent_instance(question_text)
74
+
75
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
76
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
77
 
 
78
  agent_code_link = f"https://huggingface.co/spaces/{space_id}/tree/main"
79
+ submission_data = {
80
+ "username": username.strip(),
81
+ "agent_code": agent_code_link,
82
+ "answers": answers_payload
83
+ }
84
 
85
  submit_response = requests.post(submit_url, json=submission_data, timeout=60)
86
  submit_response.raise_for_status()
 
89
  final_status = (
90
  f"Submission Successful!\n"
91
  f"User: {result_data.get('username')}\n"
92
+ f"Final Score: {result_data.get('score')}% \n"
93
  f"Message: {result_data.get('message')}"
94
  )
95
  return final_status, pd.DataFrame(results_log)
96
 
97
  except Exception as e:
98
+ return f"Submission Failed: {e}", None
99
 
100
  # --- Gradio UI ---
101
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
102
+ gr.Markdown("# 🚀 Professional Agent Evaluator (OpenAI Edition)")
103
  gr.LoginButton()
104
  run_button = gr.Button("Run Evaluation & Submit All Answers", variant="primary")
105
+ status_output = gr.Textbox(label="Leaderboard Status", lines=4)
106
  results_table = gr.DataFrame(label="Agent Reasoning Trace", wrap=True)
107
 
108
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])