Update app.py
Browse files
app.py
CHANGED
|
@@ -1,371 +1,377 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
-
import traceback
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
| 7 |
import pandas as pd
|
| 8 |
from openai import OpenAI
|
| 9 |
|
|
|
|
| 10 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
| 11 |
from smolagents.models import OpenAIServerModel
|
| 12 |
|
| 13 |
-
# --- Logging ---
|
| 14 |
-
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
-
# ---
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 22 |
if not GITHUB_TOKEN:
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
GITHUB_ENDPOINT = "https://models.github.ai/inference"
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# ---
|
| 29 |
-
|
|
|
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
base_url=GITHUB_ENDPOINT,
|
| 33 |
-
api_key=GITHUB_TOKEN,
|
| 34 |
-
)
|
| 35 |
except Exception as e:
|
| 36 |
-
logger.error(f"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
|
| 44 |
@tool
|
| 45 |
-
def
|
| 46 |
"""
|
| 47 |
-
Performs a
|
| 48 |
-
Use this for general
|
| 49 |
Args:
|
| 50 |
-
query (str): The search query.
|
| 51 |
Returns:
|
| 52 |
-
str: The search results, or an error message.
|
| 53 |
"""
|
| 54 |
-
logger.info(f"Executing
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
-
# Call the instantiated search tool
|
| 57 |
result = search_tool_instance(query=query)
|
| 58 |
-
logger.info(f"
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
return result
|
| 65 |
except Exception as e:
|
| 66 |
-
logger.exception(f"
|
| 67 |
return f"Search Error: {e}"
|
| 68 |
|
| 69 |
@tool
|
| 70 |
-
def
|
| 71 |
-
"""
|
| 72 |
-
Reframes an unclear search query to improve relevance. Often useful before calling duckduckgo_search if the initial query is vague.
|
| 73 |
-
Args:
|
| 74 |
-
query (str): The original search query.
|
| 75 |
-
Returns:
|
| 76 |
-
str: A concise, improved version prepended with 'Summarize and reframe:'.
|
| 77 |
-
"""
|
| 78 |
-
logger.info(f"Executing summarize_query with query: {query}")
|
| 79 |
-
# This still doesn't use an LLM, it's just a placeholder/reframing instruction
|
| 80 |
-
return f"Summarize and reframe: {query}"
|
| 81 |
-
|
| 82 |
-
@tool
|
| 83 |
-
def wikipedia_search(page: str) -> str:
|
| 84 |
"""
|
| 85 |
-
Fetches the summary
|
|
|
|
| 86 |
Args:
|
| 87 |
-
|
| 88 |
Returns:
|
| 89 |
-
str: The
|
| 90 |
"""
|
| 91 |
-
page_safe =
|
| 92 |
-
logger.info(f"Executing
|
| 93 |
try:
|
| 94 |
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{page_safe}"
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
headers =
|
| 98 |
-
r
|
| 99 |
-
r.raise_for_status() # Raises HTTPError for 4xx/5xx
|
| 100 |
data = r.json()
|
| 101 |
extract = data.get("extract", "")
|
| 102 |
-
if
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
except requests.exceptions.HTTPError as e:
|
| 117 |
if e.response.status_code == 404:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
else:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
except requests.exceptions.RequestException as e:
|
| 124 |
logger.exception(f"Wikipedia network request failed for page: {page_safe}")
|
| 125 |
-
return f"Wikipedia
|
| 126 |
except Exception as e:
|
| 127 |
-
logger.exception(f"Unexpected
|
| 128 |
-
return f"
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
#
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
"""
|
| 153 |
|
| 154 |
-
# ---
|
|
|
|
| 155 |
try:
|
| 156 |
-
model
|
|
|
|
| 157 |
model_id=MODEL_ID,
|
| 158 |
api_key=GITHUB_TOKEN,
|
| 159 |
base_url=GITHUB_ENDPOINT,
|
| 160 |
-
# Add timeout
|
| 161 |
-
# Add model_kwargs if needed, e.g. model_kwargs={'temperature': 0.5}
|
| 162 |
)
|
| 163 |
-
|
|
|
|
|
|
|
| 164 |
except Exception as e:
|
| 165 |
-
logger.exception("Failed to configure OpenAIServerModel")
|
| 166 |
raise RuntimeError(f"Could not configure SmolAgents model for GitHub endpoint: {e}") from e
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
#
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
if not
|
| 219 |
logger.warning("Submission attempt failed: User not logged in.")
|
| 220 |
-
return "Please log in to Hugging Face to submit.", None
|
| 221 |
|
| 222 |
-
username
|
| 223 |
-
|
| 224 |
-
if not space_id:
|
| 225 |
-
logger.warning("SPACE_ID environment variable not set. Agent code URL will be incomplete.")
|
| 226 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Agent code URL unavailable (SPACE_ID not set)"
|
| 227 |
-
logger.info(f"Starting evaluation run for user '{username}'")
|
| 228 |
-
agent = BasicAgent()
|
| 229 |
|
| 230 |
-
# Fetch
|
|
|
|
| 231 |
try:
|
| 232 |
-
logger.info(f"Fetching questions from {
|
| 233 |
-
resp = requests.get(f"{
|
| 234 |
resp.raise_for_status()
|
| 235 |
questions_data = resp.json()
|
| 236 |
-
if
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
| 241 |
except Exception as e:
|
| 242 |
logger.exception("Failed to fetch questions")
|
| 243 |
-
return f"Error fetching questions: {e}", None
|
| 244 |
|
| 245 |
if not questions:
|
| 246 |
logger.warning("No questions fetched or questions list is empty.")
|
| 247 |
-
return "No questions were fetched from the server.", None
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
| 251 |
for i, item in enumerate(questions):
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
if not tid or not q:
|
| 258 |
-
logger.warning(f"Skipping question with missing task_id or question: {item}")
|
| 259 |
continue
|
| 260 |
|
| 261 |
-
logger.info(f"Processing question {i+1}/{
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
# Extract
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
if
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
logger.info(f"Task ID: {tid}, Question: '{q}', Submitted Answer: '{submitted_ans}'")
|
| 281 |
-
# Store more info for the Gradio table, including the raw output for debugging
|
| 282 |
-
logs.append({
|
| 283 |
-
"Task ID": tid,
|
| 284 |
-
"Question": q,
|
| 285 |
-
"Submitted Answer": submitted_ans,
|
| 286 |
-
"Agent Raw Output": ans_raw # Show the full thought process in the table
|
| 287 |
})
|
| 288 |
-
payload
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
-
logger.info(f"Submitting {len(payload)} answers for user '{username}'...")
|
| 300 |
-
# Submit answers
|
| 301 |
try:
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
)
|
| 309 |
-
|
| 310 |
-
result = post.json()
|
| 311 |
-
logger.info(f"Submission successful. Result: {result}")
|
| 312 |
-
|
| 313 |
-
score_percent = result.get('score', 'N/A')
|
| 314 |
-
try: # Format score nicely
|
| 315 |
-
score_percent = f"{float(score_percent):.2f}" if isinstance(score_percent, (int, float)) else score_percent
|
| 316 |
-
except (ValueError, TypeError): pass
|
| 317 |
-
|
| 318 |
-
status = (
|
| 319 |
-
f"Submission Successful!\n"
|
| 320 |
-
f"User: {result.get('username', 'N/A')}\n"
|
| 321 |
-
f"Score: {score_percent}%\n"
|
| 322 |
-
f"Correct: {result.get('correct_count','?')} / Attempted: {result.get('total_attempted','?')}\n"
|
| 323 |
-
f"Message: {result.get('message','(No message)')}"
|
| 324 |
-
)
|
| 325 |
-
# Update logs DataFrame with final status if needed, though usually not necessary
|
| 326 |
-
return status, pd.DataFrame(logs) # Return status and the detailed logs
|
| 327 |
|
| 328 |
except requests.exceptions.RequestException as e:
|
| 329 |
logger.exception("Submission request failed")
|
| 330 |
error_details = str(e)
|
| 331 |
if e.response is not None:
|
| 332 |
-
|
| 333 |
-
return f"Submission Failed: {error_details}",
|
| 334 |
except Exception as e:
|
| 335 |
-
logger.exception("
|
| 336 |
-
return f"Submission Failed with unexpected error: {e}",
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
gr.
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
wrap=True,
|
| 357 |
-
#
|
| 358 |
-
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
)
|
| 361 |
-
btn.click(run_and_submit_all, outputs=[out_status, out_table], api_name="run_submit") # Add api_name
|
| 362 |
|
|
|
|
|
|
|
|
|
|
| 363 |
if __name__ == "__main__":
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
#
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
demo.launch(debug=True, share=True)
|
| 371 |
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import traceback
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
| 7 |
import pandas as pd
|
| 8 |
from openai import OpenAI
|
| 9 |
|
| 10 |
+
# Assuming these imports from smolagents are correct
|
| 11 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
| 12 |
from smolagents.models import OpenAIServerModel
|
| 13 |
|
| 14 |
+
# --- Basic Logging Setup ---
|
| 15 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
+
# --- Configuration ---
|
| 19 |
+
# URL for fetching questions and submitting answers
|
| 20 |
+
SUBMISSION_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 21 |
|
| 22 |
+
# GitHub Models Configuration
|
| 23 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 24 |
if not GITHUB_TOKEN:
|
| 25 |
+
# Critical error if token is missing
|
| 26 |
+
raise ValueError("GITHUB_TOKEN environment variable not set. Please set it in Space secrets.")
|
| 27 |
|
| 28 |
GITHUB_ENDPOINT = "https://models.github.ai/inference"
|
| 29 |
+
# Use a known model ID compatible with the endpoint
|
| 30 |
+
# Let's stick to gpt-4o-mini based on previous logs, ensure it's available.
|
| 31 |
+
MODEL_ID = os.getenv("MODEL_ID", "openai/gpt-4o-mini")
|
| 32 |
|
| 33 |
+
# --- Tool Definitions ---
|
| 34 |
+
|
| 35 |
+
# Instantiate the search tool ONCE to reuse its state/connection if any
|
| 36 |
try:
|
| 37 |
+
search_tool_instance = DuckDuckGoSearchTool()
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
+
logger.error(f"Failed to instantiate DuckDuckGoSearchTool: {e}")
|
| 40 |
+
# Depending on the app's requirements, you might want to raise an error here
|
| 41 |
+
# or allow the app to start but log the failure.
|
| 42 |
+
search_tool_instance = None # Indicate failure
|
| 43 |
|
| 44 |
+
# IMPORTANT: Define wrapper functions that the LLM will be instructed to call.
|
| 45 |
+
# Use the @tool decorator so CodeAgent recognizes them.
|
| 46 |
|
| 47 |
@tool
|
| 48 |
+
def web_search(query: str) -> str:
|
| 49 |
"""
|
| 50 |
+
Performs a web search using DuckDuckGo for the given query.
|
| 51 |
+
Use this for general questions, finding current information, or when Wikipedia fails.
|
| 52 |
Args:
|
| 53 |
+
query (str): The search query string.
|
| 54 |
Returns:
|
| 55 |
+
str: The search results obtained from DuckDuckGo, or an error message.
|
| 56 |
"""
|
| 57 |
+
logger.info(f"Executing web_search with query: '{query[:100]}...'") # Log snippet
|
| 58 |
+
if search_tool_instance is None:
|
| 59 |
+
logger.error("web_search cannot execute because DuckDuckGoSearchTool failed to initialize.")
|
| 60 |
+
return "Search Error: Tool not initialized."
|
| 61 |
try:
|
|
|
|
| 62 |
result = search_tool_instance(query=query)
|
| 63 |
+
logger.info(f"web_search returned {len(result)} characters.")
|
| 64 |
+
# Limit result length to prevent excessively large observations
|
| 65 |
+
max_len = 3000
|
| 66 |
+
if len(result) > max_len:
|
| 67 |
+
logger.warning(f"Truncating web_search result from {len(result)} to {max_len} chars.")
|
| 68 |
+
return result[:max_len] + "... (truncated)"
|
| 69 |
return result
|
| 70 |
except Exception as e:
|
| 71 |
+
logger.exception(f"web_search failed for query: {query}")
|
| 72 |
return f"Search Error: {e}"
|
| 73 |
|
| 74 |
@tool
|
| 75 |
+
def wikipedia_lookup(page_title: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
"""
|
| 77 |
+
Fetches the summary introduction text of an English Wikipedia page.
|
| 78 |
+
Use this for factual information about specific topics, people, or entities.
|
| 79 |
Args:
|
| 80 |
+
page_title (str): The exact title of the Wikipedia page (e.g., 'Albert Einstein', 'List_of_programming_languages'). Spaces will be converted to underscores.
|
| 81 |
Returns:
|
| 82 |
+
str: The summary text of the page, or an error message if not found or failed.
|
| 83 |
"""
|
| 84 |
+
page_safe = page_title.replace(" ", "_")
|
| 85 |
+
logger.info(f"Executing wikipedia_lookup for page: '{page_title}' (URL: {page_safe})")
|
| 86 |
try:
|
| 87 |
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{page_safe}"
|
| 88 |
+
space_id = os.getenv("SPACE_ID", "unknown-huggingface-space")
|
| 89 |
+
headers = {'User-Agent': f'GAIAgent/1.0 ({space_id})'}
|
| 90 |
+
r = requests.get(url, headers=headers, timeout=15) # Increased timeout
|
| 91 |
+
r.raise_for_status() # Check for HTTP 4xx/5xx errors
|
|
|
|
| 92 |
data = r.json()
|
| 93 |
extract = data.get("extract", "")
|
| 94 |
+
if extract:
|
| 95 |
+
logger.info(f"wikipedia_lookup found summary ({len(extract)} chars) for '{page_title}'.")
|
| 96 |
+
return extract
|
| 97 |
+
else:
|
| 98 |
+
# Handle pages found but without extracts (e.g., disambiguation)
|
| 99 |
+
page_type = data.get("type", "standard")
|
| 100 |
+
title = data.get("title", page_title)
|
| 101 |
+
if page_type == "disambiguation":
|
| 102 |
+
description = data.get("description", "multiple meanings")
|
| 103 |
+
logger.warning(f"wikipedia_lookup found a disambiguation page for '{title}': {description}")
|
| 104 |
+
return f"Wikipedia Error: '{title}' refers to {description}. Please provide a more specific page title."
|
| 105 |
+
else:
|
| 106 |
+
logger.warning(f"wikipedia_lookup found page '{title}' but it has no summary text.")
|
| 107 |
+
return f"Wikipedia Error: Page '{title}' found but has no summary."
|
| 108 |
except requests.exceptions.HTTPError as e:
|
| 109 |
if e.response.status_code == 404:
|
| 110 |
+
logger.warning(f"Wikipedia page not found: {page_safe}")
|
| 111 |
+
return f"Wikipedia Error: Page '{page_safe}' not found."
|
| 112 |
else:
|
| 113 |
+
logger.error(f"Wikipedia HTTP error {e.response.status_code} for page: {page_safe}")
|
| 114 |
+
return f"Wikipedia Error: HTTP {e.response.status_code} for page '{page_safe}'."
|
| 115 |
except requests.exceptions.RequestException as e:
|
| 116 |
logger.exception(f"Wikipedia network request failed for page: {page_safe}")
|
| 117 |
+
return f"Wikipedia Error: Network error for page '{page_safe}': {e}"
|
| 118 |
except Exception as e:
|
| 119 |
+
logger.exception(f"Unexpected error during wikipedia_lookup for page: {page_safe}")
|
| 120 |
+
return f"Wikipedia Error: Unexpected error: {e}"
|
| 121 |
+
|
| 122 |
+
# Removed summarize_query tool for simplicity, as it wasn't adding much value in logs
|
| 123 |
+
|
| 124 |
+
# --- The ReACT Prompt ---
|
| 125 |
+
# Define the *exact* instructions for the LLM, listing the *actual* tool function names.
|
| 126 |
+
# Keep it clear and concise.
|
| 127 |
+
REACT_INSTRUCTION_PROMPT = """You are a helpful assistant that answers questions using the provided tools.
|
| 128 |
+
|
| 129 |
+
Available Tools:
|
| 130 |
+
- web_search(query: str): Use this for searching the web for general information, current events, or when you don't know a specific Wikipedia page title.
|
| 131 |
+
- wikipedia_lookup(page_title: str): Use this to get information from a specific English Wikipedia page. Use exact page titles (e.g., 'Berlin', 'Python_(programming_language)').
|
| 132 |
+
|
| 133 |
+
Follow these steps for each question:
|
| 134 |
+
1. **Thought:** Briefly explain your plan and which tool you will use and why.
|
| 135 |
+
2. **Action:** Call ONE tool using the correct function name and arguments. Example: web_search(query="latest news") or wikipedia_lookup(page_title="Artificial_intelligence").
|
| 136 |
+
3. **Observation:** Record the result provided by the tool.
|
| 137 |
+
4. **Thought:** Analyze the observation. Does it answer the question? If yes, prepare the final answer. If not, plan the next step (e.g., try a different tool, refine the search query, use a different Wikipedia title).
|
| 138 |
+
5. Repeat Action/Observation/Thought until you have the answer or determine it cannot be found.
|
| 139 |
+
6. **Thought:** Summarize the findings and prepare the final answer based ONLY on the observations.
|
| 140 |
+
7. **Final Answer:** Provide the final answer in the required format (number, short string, or comma-separated list) on a new line starting exactly with "FINAL ANSWER: ".
|
| 141 |
+
|
| 142 |
+
Formatting Rules for FINAL ANSWER:
|
| 143 |
+
- Numbers: Output only the number (e.g., `42`, `1000`). No commas, units ($).
|
| 144 |
+
- Strings: Use minimal words, no articles (a, an, the). Write digits as words (e.g., `seven`) unless numerical output is implied.
|
| 145 |
+
- Lists: Comma-separated, apply number/string rules to each item (e.g., `paris,london,three`).
|
| 146 |
+
|
| 147 |
+
Let's begin!
|
| 148 |
"""
|
| 149 |
|
| 150 |
+
# --- SmolAgent Setup ---
|
| 151 |
+
logger.info(f"Initializing LLM connection to {MODEL_ID} via {GITHUB_ENDPOINT}")
|
| 152 |
try:
|
| 153 |
+
# Configure the model connection to use GitHub's endpoint
|
| 154 |
+
llm_model = OpenAIServerModel(
|
| 155 |
model_id=MODEL_ID,
|
| 156 |
api_key=GITHUB_TOKEN,
|
| 157 |
base_url=GITHUB_ENDPOINT,
|
| 158 |
+
request_timeout=60 # Add a timeout for model requests
|
|
|
|
| 159 |
)
|
| 160 |
+
# Verify connection (optional, depends on OpenAIServerModel implementation)
|
| 161 |
+
# You might add a simple test call here if the library supports it easily
|
| 162 |
+
logger.info("LLM connection configured successfully.")
|
| 163 |
except Exception as e:
|
| 164 |
+
logger.exception("CRITICAL: Failed to configure OpenAIServerModel")
|
| 165 |
raise RuntimeError(f"Could not configure SmolAgents model for GitHub endpoint: {e}") from e
|
| 166 |
|
| 167 |
+
logger.info("Initializing CodeAgent...")
|
| 168 |
+
try:
|
| 169 |
+
# Create the agent instance, passing the *list of actual functions* decorated with @tool
|
| 170 |
+
agent = CodeAgent(
|
| 171 |
+
tools=[web_search, wikipedia_lookup], # Only include the defined tool functions
|
| 172 |
+
model=llm_model
|
| 173 |
+
)
|
| 174 |
+
# Log the names of the tools the agent actually recognized (if possible/safe)
|
| 175 |
+
# This depends on how CodeAgent stores tools. Avoid the previous error.
|
| 176 |
+
# logger.info(f"CodeAgent initialized. Tools detected by agent (if available): {agent.tools}") # Be cautious with this line
|
| 177 |
+
logger.info("CodeAgent initialized successfully.")
|
| 178 |
+
except Exception as e:
|
| 179 |
+
logger.exception("CRITICAL: Failed to initialize CodeAgent")
|
| 180 |
+
raise RuntimeError(f"Could not initialize CodeAgent: {e}") from e
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# --- Gradio Interface ---
|
| 184 |
+
|
| 185 |
+
def run_agent_on_question(question: str) -> str:
|
| 186 |
+
"""
|
| 187 |
+
Takes a question, runs the SmolAgent, and returns the raw output.
|
| 188 |
+
Handles basic validation and error catching.
|
| 189 |
+
"""
|
| 190 |
+
question = question.strip()
|
| 191 |
+
if not question:
|
| 192 |
+
logger.error("Agent called with empty question.")
|
| 193 |
+
return "AGENT_ERROR: Question cannot be empty."
|
| 194 |
+
|
| 195 |
+
# Construct the full prompt for the agent run
|
| 196 |
+
full_prompt = REACT_INSTRUCTION_PROMPT.strip() + "\n\nQUESTION: " + question
|
| 197 |
+
logger.info(f"--- Running Agent for Question: '{question}' ---")
|
| 198 |
+
# Log first few lines of prompt for verification (optional)
|
| 199 |
+
# logger.debug(f"Prompt start:\n{full_prompt[:300]}...")
|
| 200 |
+
|
| 201 |
+
try:
|
| 202 |
+
# Execute the agent run
|
| 203 |
+
raw_result = agent.run(full_prompt)
|
| 204 |
+
logger.info(f"Agent run completed for question: '{question}'. Output length: {len(raw_result)}")
|
| 205 |
+
# Log first/last parts of the raw result for debugging (optional)
|
| 206 |
+
# logger.debug(f"Raw agent result snippet:\n{raw_result[:500]}...\n...{raw_result[-500:]}")
|
| 207 |
+
return raw_result
|
| 208 |
+
except Exception as e:
|
| 209 |
+
logger.exception(f"Agent run failed for question '{question}'")
|
| 210 |
+
tb_str = traceback.format_exc() # Get detailed traceback
|
| 211 |
+
return f"AGENT_ERROR: An exception occurred during agent execution: {e}\nTraceback:\n{tb_str}"
|
| 212 |
+
|
| 213 |
+
def evaluate_and_submit(hf_profile: gr.OAuthProfile | None):
|
| 214 |
+
"""
|
| 215 |
+
Gradio action: Fetches questions, runs agent on each, submits results.
|
| 216 |
+
"""
|
| 217 |
+
if not hf_profile:
|
| 218 |
logger.warning("Submission attempt failed: User not logged in.")
|
| 219 |
+
return "β οΈ Please log in to Hugging Face via the button above to submit.", None # Status message, empty DataFrame
|
| 220 |
|
| 221 |
+
username = hf_profile.username
|
| 222 |
+
logger.info(f"π Starting evaluation run for user '{username}'...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
# 1. Fetch Questions
|
| 225 |
+
questions = []
|
| 226 |
try:
|
| 227 |
+
logger.info(f"Fetching questions from {SUBMISSION_URL}/questions")
|
| 228 |
+
resp = requests.get(f"{SUBMISSION_URL}/questions", timeout=20)
|
| 229 |
resp.raise_for_status()
|
| 230 |
questions_data = resp.json()
|
| 231 |
+
if isinstance(questions_data, list):
|
| 232 |
+
questions = questions_data
|
| 233 |
+
logger.info(f"β
Fetched {len(questions)} questions.")
|
| 234 |
+
else:
|
| 235 |
+
logger.error(f"Fetched questions data is not a list: {type(questions_data)}")
|
| 236 |
+
return "β Error: Fetched questions format is incorrect.", None
|
| 237 |
except Exception as e:
|
| 238 |
logger.exception("Failed to fetch questions")
|
| 239 |
+
return f"β Error fetching questions: {e}", None
|
| 240 |
|
| 241 |
if not questions:
|
| 242 |
logger.warning("No questions fetched or questions list is empty.")
|
| 243 |
+
return "βΉοΈ No questions were fetched from the server.", None
|
| 244 |
|
| 245 |
+
# 2. Run Agent on Questions
|
| 246 |
+
results_log = []
|
| 247 |
+
answers_payload = []
|
| 248 |
+
total_questions = len(questions)
|
| 249 |
for i, item in enumerate(questions):
|
| 250 |
+
task_id = item.get("task_id")
|
| 251 |
+
question_text = item.get("question")
|
| 252 |
+
|
| 253 |
+
if not task_id or not question_text:
|
| 254 |
+
logger.warning(f"Skipping invalid question item {i+1}/{total_questions}: Missing task_id or question. Data: {item}")
|
|
|
|
|
|
|
| 255 |
continue
|
| 256 |
|
| 257 |
+
logger.info(f"Processing question {i+1}/{total_questions} (Task ID: {task_id})...")
|
| 258 |
+
raw_agent_output = run_agent_on_question(question_text) # Run the agent
|
| 259 |
+
|
| 260 |
+
# Extract final answer for submission
|
| 261 |
+
final_answer = "AGENT_ERROR: No 'FINAL ANSWER:' marker found in output." # Default if parsing fails
|
| 262 |
+
marker = "FINAL ANSWER:"
|
| 263 |
+
if marker in raw_agent_output:
|
| 264 |
+
final_answer = raw_agent_output.split(marker, 1)[1].strip()
|
| 265 |
+
elif "AGENT_ERROR:" in raw_agent_output: # If agent returned an error explicitly
|
| 266 |
+
final_answer = raw_agent_output # Submit the error message
|
| 267 |
+
|
| 268 |
+
logger.info(f"Task ID: {task_id} -> Submitted Answer: '{final_answer}'")
|
| 269 |
+
|
| 270 |
+
# Log results for Gradio table
|
| 271 |
+
results_log.append({
|
| 272 |
+
"Task ID": task_id,
|
| 273 |
+
"Question": question_text,
|
| 274 |
+
"Submitted Answer": final_answer,
|
| 275 |
+
"Full Agent Output": raw_agent_output # Show full trace in UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
})
|
| 277 |
+
# Prepare payload for submission API
|
| 278 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": final_answer})
|
| 279 |
+
|
| 280 |
+
results_df = pd.DataFrame(results_log)
|
| 281 |
+
if not answers_payload:
|
| 282 |
+
logger.warning("Agent did not produce any answers to submit.")
|
| 283 |
+
return "β οΈ Agent ran but produced no answers in the expected format.", results_df
|
| 284 |
+
|
| 285 |
+
# 3. Submit Answers
|
| 286 |
+
logger.info(f"Submitting {len(answers_payload)} answers for user '{username}'...")
|
| 287 |
+
space_id = os.getenv("SPACE_ID", "SPACE_ID_NOT_SET")
|
| 288 |
+
agent_code_url = f"https://huggingface.co/spaces/{space_id}/tree/main" if "NOT_SET" not in space_id else "Agent code URL unavailable"
|
| 289 |
+
submit_data = {
|
| 290 |
+
"username": username,
|
| 291 |
+
"agent_code": agent_code_url,
|
| 292 |
+
"answers": answers_payload
|
| 293 |
+
}
|
| 294 |
|
|
|
|
|
|
|
| 295 |
try:
|
| 296 |
+
response = requests.post(f"{SUBMISSION_URL}/submit", json=submit_data, timeout=90)
|
| 297 |
+
response.raise_for_status() # Check for HTTP errors
|
| 298 |
+
submission_result = response.json()
|
| 299 |
+
logger.info(f"β
Submission successful! API Response: {submission_result}")
|
| 300 |
+
|
| 301 |
+
score = submission_result.get('score', 'N/A')
|
| 302 |
+
score_str = f"{float(score):.2f}%" if isinstance(score, (int, float)) else str(score)
|
| 303 |
+
correct = submission_result.get('correct_count', '?')
|
| 304 |
+
attempted = submission_result.get('total_attempted', '?')
|
| 305 |
+
message = submission_result.get('message', '(No message from server)')
|
| 306 |
+
|
| 307 |
+
status_message = (
|
| 308 |
+
f"β
Submission Successful!\n"
|
| 309 |
+
f"User: {username}\n"
|
| 310 |
+
f"Score: {score_str}\n"
|
| 311 |
+
f"Details: {correct} / {attempted} correct\n"
|
| 312 |
+
f"Server Message: {message}"
|
| 313 |
)
|
| 314 |
+
return status_message, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
except requests.exceptions.RequestException as e:
|
| 317 |
logger.exception("Submission request failed")
|
| 318 |
error_details = str(e)
|
| 319 |
if e.response is not None:
|
| 320 |
+
error_details += f" | Status: {e.response.status_code} | Response: {e.response.text[:300]}" # Log snippet
|
| 321 |
+
return f"β Submission Failed: {error_details}", results_df
|
| 322 |
except Exception as e:
|
| 323 |
+
logger.exception("Unexpected error during submission")
|
| 324 |
+
return f"β Submission Failed with unexpected error: {e}", results_df
|
| 325 |
+
|
| 326 |
+
# --- Build Gradio App ---
|
| 327 |
+
logger.info("Setting up Gradio interface...")
|
| 328 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 329 |
+
gr.Markdown(
|
| 330 |
+
"""
|
| 331 |
+
# π Agent Evaluation Runner π
|
| 332 |
+
|
| 333 |
+
Connect your Hugging Face account, then click the button below to fetch tasks, run the agent, and submit the answers.
|
| 334 |
+
Ensure the `GITHUB_TOKEN` secret is correctly set in your Space settings.
|
| 335 |
+
"""
|
| 336 |
+
)
|
| 337 |
+
|
| 338 |
+
with gr.Row():
|
| 339 |
+
hf_login_button = gr.LoginButton() # Use the login button component
|
| 340 |
+
|
| 341 |
+
run_button = gr.Button("βΆοΈ Run Evaluation & Submit All Answers", variant="primary")
|
| 342 |
+
|
| 343 |
+
submission_status_textbox = gr.Textbox(
|
| 344 |
+
label="π Submission Status",
|
| 345 |
+
lines=5,
|
| 346 |
+
interactive=False,
|
| 347 |
+
placeholder="Submission status will appear here..."
|
| 348 |
+
)
|
| 349 |
+
|
| 350 |
+
results_dataframe = gr.DataFrame(
|
| 351 |
+
label="π Detailed Log (Questions & Agent Output)",
|
| 352 |
+
headers=["Task ID", "Question", "Submitted Answer", "Full Agent Output"],
|
| 353 |
wrap=True,
|
| 354 |
+
# Removed height, let Gradio manage it or control via CSS if needed
|
| 355 |
+
column_widths=["10%", "25%", "20%", "45%"]
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
# Connect button click to the evaluation function
|
| 359 |
+
# Pass the login button's profile info to the function
|
| 360 |
+
run_button.click(
|
| 361 |
+
fn=evaluate_and_submit,
|
| 362 |
+
inputs=[hf_login_button], # Pass the profile info from the login button
|
| 363 |
+
outputs=[submission_status_textbox, results_dataframe],
|
| 364 |
+
api_name="evaluate_submit" # For API usage if needed
|
| 365 |
)
|
|
|
|
| 366 |
|
| 367 |
+
logger.info("Gradio interface setup complete.")
|
| 368 |
+
|
| 369 |
+
# --- Launch the App ---
|
| 370 |
if __name__ == "__main__":
|
| 371 |
+
logger.info("Launching Gradio application...")
|
| 372 |
+
demo.launch(
|
| 373 |
+
debug=True, # Provides more detailed logs for Gradio itself
|
| 374 |
+
share=True # Necessary for public access on Hugging Face Spaces
|
| 375 |
+
)
|
| 376 |
+
logger.info("Gradio application has been launched.")
|
|
|
|
| 377 |
|