GilbertoEwaldFilho commited on
Commit
2293d28
·
verified ·
1 Parent(s): 353a546

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -31
app.py CHANGED
@@ -5,7 +5,7 @@ import requests
5
  import pandas as pd
6
 
7
  from huggingface_hub import InferenceClient
8
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
9
 
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -70,53 +70,59 @@ SYSTEM_PROMPT = (
70
  # =========================================================
71
 
72
  class BasicAgent:
73
- def __init__(self):
74
- print("Initializing GAIA Agent...")
75
-
76
- HF_TOKEN = os.getenv("HF_TOKEN")
77
- print("HF_TOKEN exists?", HF_TOKEN is not None)
78
 
79
- if not HF_TOKEN:
80
- raise ValueError(" HF_TOKEN not found create a Secret in your Space!")
81
 
 
82
  self.search_tool = DuckDuckGoSearchTool()
83
 
84
- self.client = InferenceClient(
85
- model="mistralai/Mixtral-8x7B-Instruct-v0.1",
86
- token=HF_TOKEN,
87
- )
88
-
89
- self.model = InferenceClientModel(
90
- client=self.client,
91
- system_prompt="Answer ONLY with the final answer. No steps, no explanation."
92
  )
93
 
 
94
  self.agent = CodeAgent(
95
  model=self.model,
96
  tools=[self.search_tool],
97
- max_steps=5,
98
  )
99
 
100
  def __call__(self, question: str) -> str:
101
- print(f"\n=== NEW QUESTION ===\n{question}\n")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  try:
104
- raw = self.agent.run(
105
- f"Answer this question exactly. Only answer:\n{question}"
106
- )
107
- print("RAW MODEL RESPONSE:", raw)
108
-
109
- if not raw or raw.strip() == "":
110
- print("⚠️ MODEL RETURNED EMPTY STRING ⚠️")
111
- return ""
112
-
113
- return clean_answer(raw)
114
-
115
  except Exception as e:
116
- print("ERROR:", e)
117
  return ""
118
 
119
-
120
  # =========================================================
121
  # Runner + submit (mantido do template, usando BasicAgent novo)
122
  # =========================================================
 
5
  import pandas as pd
6
 
7
  from huggingface_hub import InferenceClient
8
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
9
 
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
70
  # =========================================================
71
 
72
  class BasicAgent:
73
+ """
74
+ Agente usando smolagents + HfApiModel + DuckDuckGoSearchTool,
75
+ ajustado para o cenário do GAIA (EXACT MATCH).
76
+ """
 
77
 
78
+ def __init__(self):
79
+ print("Initializing GAIA agent with HfApiModel + DuckDuckGoSearchTool...")
80
 
81
+ # ferramenta de busca da própria smolagents (usa ddgs por baixo)
82
  self.search_tool = DuckDuckGoSearchTool()
83
 
84
+ # Modelo chamado via API do Hugging Face
85
+ # Você pode trocar o model_id se o curso recomendar outro
86
+ self.model = HfApiModel(
87
+ model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
88
+ max_new_tokens=256,
89
+ temperature=0.2,
 
 
90
  )
91
 
92
+ # CodeAgent: agente que consegue chamar tools e escrever código
93
  self.agent = CodeAgent(
94
  model=self.model,
95
  tools=[self.search_tool],
96
+ add_base_tools=False, # deixamos só a search tool pra ficar simples
97
  )
98
 
99
  def __call__(self, question: str) -> str:
100
+ print(f"Processing question: {question[:80]}...")
101
+
102
+ # Instruções de EXACT MATCH embutidas no prompt
103
+ prompt = (
104
+ "You are solving GAIA Level 1 questions.\n"
105
+ "You have access to a web search tool and MUST use it whenever needed "
106
+ "to obtain accurate, up-to-date information.\n"
107
+ "RULES:\n"
108
+ " - Return ONLY the final answer as a short string.\n"
109
+ " - NO explanations.\n"
110
+ " - NO 'Final answer', 'Answer:', etc.\n"
111
+ " - If the answer is a number, output just the number.\n"
112
+ "Question:\n"
113
+ f"{question}\n"
114
+ )
115
 
116
  try:
117
+ raw = self.agent.run(prompt)
118
+ final = clean_answer(raw)
119
+ print(f"Raw answer: {raw!r}")
120
+ print(f"Final cleaned answer: {final!r}")
121
+ return final
 
 
 
 
 
 
122
  except Exception as e:
123
+ print(f"Error while running agent: {e}")
124
  return ""
125
 
 
126
  # =========================================================
127
  # Runner + submit (mantido do template, usando BasicAgent novo)
128
  # =========================================================