Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,100 +5,46 @@ import inspect
|
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
# Additional packages
|
| 8 |
-
from
|
| 9 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 10 |
|
| 11 |
# (Keep Constants as is)
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
# Added constants
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
prompt = hub.pull("hwchase17/react")
|
| 47 |
-
|
| 48 |
-
agent = create_react_agent(llm=self.llm, tools=self.tools, prompt=prompt)
|
| 49 |
-
self.executor = AgentExecutor(agent=agent, tools=self.tools, verbose=True, handle_parsing_errors=True, max_iterations=12)
|
| 50 |
-
print("FinalAgent initialized successfully.")
|
| 51 |
-
|
| 52 |
-
def _setup_tools(self):
|
| 53 |
-
"""Defines the tools available to the agent."""
|
| 54 |
-
search = GoogleSerperAPIWrapper(serper_api_key=SERPER_API_KEY)
|
| 55 |
-
|
| 56 |
-
def file_downloader_tool(task_id: str) -> str:
|
| 57 |
-
try:
|
| 58 |
-
all_questions_response = requests.get(f"{self.api_url}/questions", timeout=10)
|
| 59 |
-
all_questions_response.raise_for_status()
|
| 60 |
-
all_questions = all_questions_response.json()
|
| 61 |
-
file_name = next((q.get('file') for q in all_questions if q.get('task_id') == task_id), None)
|
| 62 |
-
if not file_name: return f"Error: No file associated with task_id '{task_id}'."
|
| 63 |
-
local_path = f"/tmp/{file_name}"
|
| 64 |
-
if not os.path.exists(local_path):
|
| 65 |
-
response = requests.get(f"{self.api_url}/files/{task_id}", timeout=10)
|
| 66 |
-
response.raise_for_status()
|
| 67 |
-
with open(local_path, "wb") as f: f.write(response.content)
|
| 68 |
-
return f"File '{file_name}' is available at local path: {local_path}"
|
| 69 |
-
except Exception as e: return f"Error downloading file for task {task_id}: {str(e)}"
|
| 70 |
-
|
| 71 |
-
def python_interpreter_tool(code: str) -> str:
|
| 72 |
-
try:
|
| 73 |
-
exec_globals = {"pd": pd, "io": io}
|
| 74 |
-
local_vars = {}
|
| 75 |
-
exec(code, exec_globals, local_vars)
|
| 76 |
-
return str(local_vars.get('output', 'Code executed successfully with no output.'))
|
| 77 |
-
except Exception as e: return f"Error executing Python code: {str(e)}"
|
| 78 |
-
|
| 79 |
-
return [
|
| 80 |
-
Tool(name"Search", func=search.run, description="A useful tool for searching the internet to answer questions about current events, facts, and general knowledge."),
|
| 81 |
-
Tool(name="FileDownloader", func=file_downloader_tool, description="Use to download a file associated with a task. The input MUST be the task_id. Returns the local path where the file is stored."),
|
| 82 |
-
Tool(name="PythonInterpreter", func=python_interpreter_tool, description="Use to execute Python code for calculations or data analysis. Use this to analyze files AFTER donwloading them. You have the pandas library available as 'pd'. Example: df = pd.read_excel('/tmp/filename.xlsx')"),
|
| 83 |
-
]
|
| 84 |
-
|
| 85 |
-
def __call__(self, item: dict) -> str:
|
| 86 |
-
"""Runs the agent on a single question item."""
|
| 87 |
-
task_id = item.get("task_id")
|
| 88 |
-
question = item.get("question")
|
| 89 |
|
| 90 |
-
prompt_input = f"Question: {question}"
|
| 91 |
-
if item.get("file"):
|
| 92 |
-
prompt_input += f"\n(This question has an associated file names '{item.get('file')}'. To use it, you must first call the FileDownloader tool with the task_id: ‘{task_id}‘)"
|
| 93 |
|
| 94 |
-
input_data = {"input": prompt_input}
|
| 95 |
|
| 96 |
-
print(f"Question: {question}")
|
| 97 |
-
try:
|
| 98 |
-
response = self.executor.invoke(input_data)
|
| 99 |
-
return str(response.get("output", "Agent failed to produce an answer.")).strip()
|
| 100 |
-
except Exception as e:
|
| 101 |
-
return f"AGENT_ERROR: {str(e)}"
|
| 102 |
|
| 103 |
|
| 104 |
# Submission logic Unchanged
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
# Additional packages
|
| 8 |
+
from smolagents import CodeAgent, InferenceClientModel, OpenAIModel
|
|
|
|
| 9 |
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
# Added constants
|
| 15 |
+
GAIA_SYSTEM_PROMPT = """You are solving GAIA level 1 questions with extreme precision.
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
CRITICAL RULES:
|
| 19 |
+
1. Return ONLY the final answer - no explanations, no context, no preamble
|
| 20 |
+
2. For numbers: just the number (no units unless explicitly requested)
|
| 21 |
+
3. For strings: just the answer (no articles like "the" or "a")
|
| 22 |
+
4. For lists: format as "item1, item2, item3 (no quotes, no brackets)
|
| 23 |
+
|
| 24 |
+
STRATEGY:
|
| 25 |
+
- Use web search liberally - search multiple times with different keywords
|
| 26 |
+
- Visit actual webpages to get complete information
|
| 27 |
+
- Cross-reference multiple sources
|
| 28 |
+
- Think step-by-step but output only the final answer
|
| 29 |
+
- If you find relevant info but not the complete answer, search again with more specifc terms
|
| 30 |
+
|
| 31 |
+
NEVER output:
|
| 32 |
+
- "FINAL ANSWER:"
|
| 33 |
+
- "The answer is:"
|
| 34 |
+
- Explanations or reasoning
|
| 35 |
+
- "No information found" (keep searching!)
|
| 36 |
+
|
| 37 |
+
Examples of correct outputs:
|
| 38 |
+
Question: "How many studio albums?" -> Answer: "7"
|
| 39 |
+
Question: "What is the capital?" -> Answer: "Paris"
|
| 40 |
+
Question: "List the winners" → Answer: "John, Mary, Bob"
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
# Submission logic Unchanged
|