Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
|
|
6 |
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
@@ -10,14 +12,32 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
10 |
|
11 |
# --- Basic Agent Definition ---
|
12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
13 |
-
class
|
14 |
def __init__(self):
|
15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def __call__(self, question: str) -> str:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
23 |
"""
|
@@ -40,7 +60,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
40 |
|
41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
42 |
try:
|
43 |
-
agent =
|
44 |
except Exception as e:
|
45 |
print(f"Error instantiating agent: {e}")
|
46 |
return f"Error initializing agent: {e}", None
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from smolagents import CodeAgent, OpenAIServerModel, DuckDuckGoSearchTool
|
7 |
+
from functools import lru_cache
|
8 |
|
9 |
# (Keep Constants as is)
|
10 |
# --- Constants ---
|
|
|
12 |
|
13 |
# --- Basic Agent Definition ---
|
14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
15 |
+
class SmartAgent:
|
16 |
def __init__(self):
|
17 |
print("BasicAgent initialized.")
|
18 |
+
self.agent = CodeAgent(
|
19 |
+
llm=OpenAIServerModel(model_id="gpt-4o"),
|
20 |
+
tools=[DuckDuckGoSearchTool()]
|
21 |
+
# def __call__(self, question: str) -> str:
|
22 |
+
# print(f"Agent received question (first 50 chars): {question[:50]}...")
|
23 |
+
# fixed_answer = "This is a default answer."
|
24 |
+
# print(f"Agent returning fixed answer: {fixed_answer}")
|
25 |
+
# return fixed_answer
|
26 |
+
|
27 |
+
@lru_cache(maxsize=100)
|
28 |
def __call__(self, question: str) -> str:
|
29 |
+
# Step 1: Use web search for factual questions
|
30 |
+
if self._needs_search(question):
|
31 |
+
return DuckDuckGoSearchTool().run(question)
|
32 |
+
# Step 2: Use GPT-4o for complex questions
|
33 |
+
return self.agent.run(question)
|
34 |
+
|
35 |
+
def _needs_search(self, question: str) -> bool:
|
36 |
+
prompt = f"""
|
37 |
+
Should this question use web search? Answer "yes" or "no":
|
38 |
+
Question: "{question}"
|
39 |
+
"""
|
40 |
+
return "yes" in self.agent.run(prompt).lower()
|
41 |
|
42 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
43 |
"""
|
|
|
60 |
|
61 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
62 |
try:
|
63 |
+
agent = SmartAgent()
|
64 |
except Exception as e:
|
65 |
print(f"Error instantiating agent: {e}")
|
66 |
return f"Error initializing agent: {e}", None
|