Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,65 +23,65 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 23 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 24 |
# return fixed_answer
|
| 25 |
|
| 26 |
-
class GeminiModel:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
# Define BasicAgent properly
|
| 60 |
-
class BasicAgent:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
def __call__(self, question: str) -> str:
|
| 75 |
-
"""Call the CodeAgent."""
|
| 76 |
-
print(f"Running agent for task: {question[:50]}...")
|
| 77 |
-
try:
|
| 78 |
-
result = self.agent.run(question)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# class BasicAgent(ReActAgent):
|
| 87 |
# def __init__(self):
|
|
@@ -151,6 +151,70 @@ class BasicAgent:
|
|
| 151 |
# return decoded.split("Answer:")[-1].strip()
|
| 152 |
# return decoded.strip()
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 155 |
"""
|
| 156 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 23 |
# print(f"Agent returning fixed answer: {fixed_answer}")
|
| 24 |
# return fixed_answer
|
| 25 |
|
| 26 |
+
# class GeminiModel:
|
| 27 |
+
# def __init__(self, model_name="gemini-2.0-flash-exp"):
|
| 28 |
+
# api_key = os.getenv("GEMINI_API_KEY")
|
| 29 |
+
# if not api_key:
|
| 30 |
+
# raise ValueError("GEMINI_API_KEY is missing.")
|
| 31 |
+
|
| 32 |
+
# os.environ["GOOGLE_API_KEY"] = api_key
|
| 33 |
+
# self.client = genai.Client()
|
| 34 |
+
# self.model_id = model_name
|
| 35 |
+
|
| 36 |
+
# self.generation_config = types.GenerateContentConfig(
|
| 37 |
+
# temperature=0.4,
|
| 38 |
+
# top_p=0.9,
|
| 39 |
+
# top_k=40,
|
| 40 |
+
# candidate_count=1,
|
| 41 |
+
# seed=42,
|
| 42 |
+
# presence_penalty=0.0,
|
| 43 |
+
# frequency_penalty=0.0,
|
| 44 |
+
# )
|
| 45 |
|
| 46 |
+
# def __call__(self, prompt: str, **kwargs) -> str:
|
| 47 |
+
# """Send prompt to Gemini."""
|
| 48 |
+
# try:
|
| 49 |
+
# response = self.client.generate_content(
|
| 50 |
+
# model=self.model_id,
|
| 51 |
+
# contents=[{"role": "user", "parts": [{"text": prompt}]}],
|
| 52 |
+
# generation_config=self.generation_config
|
| 53 |
+
# )
|
| 54 |
+
# # Return a dictionary that CodeAgent expects
|
| 55 |
+
# return {"content": response.candidates[0].content.parts[0].text.strip()}
|
| 56 |
+
# except Exception as e:
|
| 57 |
+
# return {"content": f"Error during Gemini call: {str(e)}"}
|
| 58 |
|
| 59 |
+
# # Define BasicAgent properly
|
| 60 |
+
# class BasicAgent:
|
| 61 |
+
# def __init__(self):
|
| 62 |
+
# print("Initializing CodeAgent with Gemini + tools.")
|
| 63 |
|
| 64 |
+
# # Load tools
|
| 65 |
+
# self.search_tool = DuckDuckGoSearchTool()
|
| 66 |
|
| 67 |
+
# # Build the agent
|
| 68 |
+
# self.agent = CodeAgent(
|
| 69 |
+
# tools=[self.search_tool],
|
| 70 |
+
# model=GeminiModel(), # Our simple Gemini wrapper
|
| 71 |
+
# planning_interval=3 # Activate planning
|
| 72 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# def __call__(self, question: str) -> str:
|
| 75 |
+
# """Call the CodeAgent."""
|
| 76 |
+
# print(f"Running agent for task: {question[:50]}...")
|
| 77 |
+
# try:
|
| 78 |
+
# result = self.agent.run(question)
|
| 79 |
+
|
| 80 |
+
# # Sleep to respect rate limits
|
| 81 |
+
# time.sleep(7)
|
| 82 |
+
# return result
|
| 83 |
+
# except Exception as e:
|
| 84 |
+
# return f"Error running agent: {str(e)}"
|
| 85 |
|
| 86 |
# class BasicAgent(ReActAgent):
|
| 87 |
# def __init__(self):
|
|
|
|
| 151 |
# return decoded.split("Answer:")[-1].strip()
|
| 152 |
# return decoded.strip()
|
| 153 |
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# Setup Gemini Client
|
| 157 |
+
api_key = os.getenv("GEMINI_API_KEY")
|
| 158 |
+
if not api_key:
|
| 159 |
+
raise ValueError("GEMINI_API_KEY is missing.")
|
| 160 |
+
os.environ["GOOGLE_API_KEY"] = api_key
|
| 161 |
+
client = genai.Client()
|
| 162 |
+
model_id = "gemini-2.0-flash-exp"
|
| 163 |
+
|
| 164 |
+
generation_config = types.GenerateContentConfig(
|
| 165 |
+
temperature=0.4,
|
| 166 |
+
top_p=0.9,
|
| 167 |
+
top_k=40,
|
| 168 |
+
candidate_count=1,
|
| 169 |
+
seed=42,
|
| 170 |
+
presence_penalty=0.0,
|
| 171 |
+
frequency_penalty=0.0,
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# Define the real agent
|
| 175 |
+
class BasicAgent:
|
| 176 |
+
def __init__(self):
|
| 177 |
+
print("Initializing BasicAgent with direct Gemini calls + DuckDuckGo tool.")
|
| 178 |
+
|
| 179 |
+
self.search_tool = DuckDuckGoSearchTool()
|
| 180 |
+
|
| 181 |
+
# Dummy model placeholder, not used
|
| 182 |
+
self.agent = CodeAgent(
|
| 183 |
+
tools=[self.search_tool],
|
| 184 |
+
model=self, # Pass self as the model
|
| 185 |
+
planning_interval=3
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
def __call__(self, prompt: str, **kwargs) -> object:
|
| 189 |
+
"""Directly call Gemini API for each prompt."""
|
| 190 |
+
try:
|
| 191 |
+
response = client.generate_content(
|
| 192 |
+
model=model_id,
|
| 193 |
+
contents=[{"role": "user", "parts": [{"text": prompt}]}],
|
| 194 |
+
generation_config=generation_config
|
| 195 |
+
)
|
| 196 |
+
answer = response.candidates[0].content.parts[0].text.strip()
|
| 197 |
+
# Wrap in a simple object with .content attribute
|
| 198 |
+
class Output:
|
| 199 |
+
def __init__(self, content):
|
| 200 |
+
self.content = content
|
| 201 |
+
return Output(content=answer)
|
| 202 |
+
except Exception as e:
|
| 203 |
+
class Output:
|
| 204 |
+
def __init__(self, content):
|
| 205 |
+
self.content = content
|
| 206 |
+
return Output(content=f"Error: {str(e)}")
|
| 207 |
+
|
| 208 |
+
def run_agent(self, question: str) -> str:
|
| 209 |
+
"""Run the agent on a question."""
|
| 210 |
+
print(f"Running agent for task: {question[:50]}...")
|
| 211 |
+
try:
|
| 212 |
+
result = self.agent.run(question)
|
| 213 |
+
time.sleep(7) # Respect rate limits
|
| 214 |
+
return result
|
| 215 |
+
except Exception as e:
|
| 216 |
+
return f"Error running agent: {str(e)}"
|
| 217 |
+
|
| 218 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 219 |
"""
|
| 220 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|