sofials commited on
Commit
1f49309
·
verified ·
1 Parent(s): 6f4dbdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -37
app.py CHANGED
@@ -2,48 +2,40 @@ import os
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
5
- import spaces
6
  from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, InferenceClientModel
7
 
8
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
9
 
10
- class GaiaAgent:
11
- def __init__(self):
12
- print("Initializing GaiaAgent...")
13
- self.model = InferenceClientModel(
14
- model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
15
- token=os.getenv("HF_TOKEN"),
16
- )
17
- self.agent = CodeAgent(
18
- tools=[
19
- DuckDuckGoSearchTool(),
20
- VisitWebpageTool(),
21
- ],
22
- model=self.model,
23
- max_steps=10,
24
- additional_authorized_imports=["math", "datetime", "re", "json"],
25
- )
26
- print("GaiaAgent ready!")
27
 
28
- def __call__(self, question: str) -> str:
29
- prompt = f"""Answer the following question.
30
  Your answer must be as concise as possible — just the final answer, no explanation.
31
  If the answer is a number, return just the number.
32
  If the answer is a name, return just the name.
33
  If the answer is a list, return comma-separated values.
34
 
35
  Question: {question}"""
36
- try:
37
- response = self.agent.run(prompt)
38
- return str(response).strip()
39
- except Exception as e:
40
- print(f"Error: {e}")
41
- return "I don't know"
42
-
43
- # Funzione con decoratore GPU — obbligatorio per ZeroGPU
44
- @spaces.GPU
45
- def run_agent_on_question(agent, question):
46
- return agent(question)
47
 
48
  def run_and_submit_all(profile: gr.OAuthProfile | None):
49
  space_id = os.getenv("SPACE_ID")
@@ -58,7 +50,8 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
58
  submit_url = f"{api_url}/submit"
59
 
60
  try:
61
- agent = GaiaAgent()
 
62
  except Exception as e:
63
  return f"Error initializing agent: {e}", None
64
 
@@ -81,22 +74,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
81
  if not task_id or question_text is None:
82
  continue
83
  try:
84
- submitted_answer = run_agent_on_question(agent, question_text)
85
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
86
  results_log.append({
87
  "Task ID": task_id,
88
  "Question": question_text[:100],
89
  "Submitted Answer": submitted_answer
90
  })
 
91
  except Exception as e:
 
92
  results_log.append({
93
  "Task ID": task_id,
94
  "Question": question_text[:100],
95
- "Submitted Answer": f"ERROR: {e}"
96
  })
97
-
98
- if not answers_payload:
99
- return "Agent did not produce any answers.", pd.DataFrame(results_log)
100
 
101
  submission_data = {
102
  "username": username.strip(),
 
2
  import gradio as gr
3
  import requests
4
  import pandas as pd
 
5
  from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, InferenceClientModel
6
 
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
9
+ def build_agent():
10
+ model = InferenceClientModel(
11
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
12
+ token=os.getenv("HF_TOKEN"),
13
+ )
14
+ agent = CodeAgent(
15
+ tools=[
16
+ DuckDuckGoSearchTool(),
17
+ VisitWebpageTool(),
18
+ ],
19
+ model=model,
20
+ max_steps=10,
21
+ additional_authorized_imports=["math", "datetime", "re", "json"],
22
+ )
23
+ return agent
 
 
24
 
25
+ def run_agent(agent, question: str) -> str:
26
+ prompt = f"""Answer the following question.
27
  Your answer must be as concise as possible — just the final answer, no explanation.
28
  If the answer is a number, return just the number.
29
  If the answer is a name, return just the name.
30
  If the answer is a list, return comma-separated values.
31
 
32
  Question: {question}"""
33
+ try:
34
+ response = agent.run(prompt)
35
+ return str(response).strip()
36
+ except Exception as e:
37
+ print(f"Error: {e}")
38
+ return "I don't know"
 
 
 
 
 
39
 
40
  def run_and_submit_all(profile: gr.OAuthProfile | None):
41
  space_id = os.getenv("SPACE_ID")
 
50
  submit_url = f"{api_url}/submit"
51
 
52
  try:
53
+ agent = build_agent()
54
+ print("Agent initialized!")
55
  except Exception as e:
56
  return f"Error initializing agent: {e}", None
57
 
 
74
  if not task_id or question_text is None:
75
  continue
76
  try:
77
+ submitted_answer = run_agent(agent, question_text)
78
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
79
  results_log.append({
80
  "Task ID": task_id,
81
  "Question": question_text[:100],
82
  "Submitted Answer": submitted_answer
83
  })
84
+ print(f"✅ {task_id}: {submitted_answer[:50]}")
85
  except Exception as e:
86
+ print(f"❌ {task_id}: {e}")
87
  results_log.append({
88
  "Task ID": task_id,
89
  "Question": question_text[:100],
90
+ "Submitted Answer": "I don't know"
91
  })
92
+ answers_payload.append({"task_id": task_id, "submitted_answer": "I don't know"})
 
 
93
 
94
  submission_data = {
95
  "username": username.strip(),