Commit
·
163af2c
1
Parent(s):
81917a3
....
Browse files- app.py +346 -143
- requirements.txt +6 -1
app.py
CHANGED
|
@@ -1,196 +1,399 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
-
and
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# 2. Fetch Questions
|
| 52 |
-
print(f"Fetching questions from: {questions_url}")
|
| 53 |
try:
|
| 54 |
-
response = requests.get(questions_url, timeout=15)
|
| 55 |
-
response.raise_for_status()
|
| 56 |
questions_data = response.json()
|
| 57 |
-
if not questions_data:
|
| 58 |
-
print("Fetched questions list is empty."
|
| 59 |
-
return "Fetched questions list is empty or invalid format.", None
|
| 60 |
print(f"Fetched {len(questions_data)} questions.")
|
| 61 |
-
except
|
| 62 |
-
print(f"Error fetching questions: {e}")
|
| 63 |
-
return f"Error fetching questions: {e}", None
|
| 64 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 65 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 66 |
-
print(f"Response text: {response.text[:500]}")
|
| 67 |
-
return f"Error decoding server response for questions: {e}", None
|
| 68 |
-
except Exception as e:
|
| 69 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
| 70 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 71 |
|
| 72 |
-
|
| 73 |
-
results_log = []
|
| 74 |
-
answers_payload = []
|
| 75 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 76 |
for item in questions_data:
|
| 77 |
-
task_id = item.get("task_id")
|
| 78 |
-
question_text = item.get("question")
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
-
print(f"Skipping item with missing
|
|
|
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
submitted_answer = agent(question_text)
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Error running agent on task {task_id}: {e}")
|
|
|
|
| 88 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 89 |
|
| 90 |
if not answers_payload:
|
| 91 |
-
|
| 92 |
-
return "Agent did not produce any answers
|
| 93 |
|
| 94 |
-
# 4. Prepare Submission
|
| 95 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 96 |
-
|
| 97 |
-
print(status_update)
|
| 98 |
-
|
| 99 |
-
# 5. Submit
|
| 100 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 101 |
try:
|
| 102 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 103 |
-
response.raise_for_status()
|
| 104 |
result_data = response.json()
|
| 105 |
-
final_status = (
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 109 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 110 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
| 111 |
-
)
|
| 112 |
print("Submission successful.")
|
| 113 |
-
|
| 114 |
-
return final_status, results_df
|
| 115 |
except requests.exceptions.HTTPError as e:
|
| 116 |
-
error_detail = f"Server responded
|
| 117 |
-
try:
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 122 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 123 |
-
print(status_message)
|
| 124 |
-
results_df = pd.DataFrame(results_log)
|
| 125 |
-
return status_message, results_df
|
| 126 |
-
except requests.exceptions.Timeout:
|
| 127 |
-
status_message = "Submission Failed: The request timed out."
|
| 128 |
-
print(status_message)
|
| 129 |
-
results_df = pd.DataFrame(results_log)
|
| 130 |
-
return status_message, results_df
|
| 131 |
-
except requests.exceptions.RequestException as e:
|
| 132 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 133 |
-
print(status_message)
|
| 134 |
-
results_df = pd.DataFrame(results_log)
|
| 135 |
-
return status_message, results_df
|
| 136 |
except Exception as e:
|
| 137 |
-
|
| 138 |
-
print(status_message)
|
| 139 |
-
results_df = pd.DataFrame(results_log)
|
| 140 |
-
return status_message, results_df
|
| 141 |
-
|
| 142 |
|
| 143 |
-
# ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
-
gr.Markdown("#
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 153 |
-
|
| 154 |
---
|
| 155 |
-
**
|
| 156 |
-
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
| 157 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
|
| 158 |
"""
|
| 159 |
)
|
| 160 |
-
|
| 161 |
gr.LoginButton()
|
| 162 |
-
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
-
|
| 165 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 166 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 168 |
-
|
| 169 |
-
run_button.click(
|
| 170 |
-
fn=run_and_submit_all,
|
| 171 |
-
outputs=[status_output, results_table]
|
| 172 |
-
)
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 176 |
-
#
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 183 |
-
else:
|
| 184 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 185 |
-
|
| 186 |
-
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 187 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 188 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 189 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 190 |
-
else:
|
| 191 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 192 |
-
|
| 193 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 194 |
-
|
| 195 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 196 |
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
import logging
|
| 6 |
+
import time
|
| 7 |
+
import json
|
| 8 |
+
from typing import Optional, Dict, Any
|
| 9 |
+
from functools import lru_cache
|
| 10 |
+
|
| 11 |
+
# Attempt to import Google Gemini library
|
| 12 |
+
try:
|
| 13 |
+
import google.generativeai as genai
|
| 14 |
+
except ImportError:
|
| 15 |
+
genai = None
|
| 16 |
+
print("WARNING: google-generativeai library not found. Install with: pip install google-generativeai")
|
| 17 |
+
|
| 18 |
+
# Attempt to import search libraries (DuckDuckGo, Google Search)
|
| 19 |
+
try:
|
| 20 |
+
from duckduckgo_search import DDGS
|
| 21 |
+
except ImportError:
|
| 22 |
+
DDGS = None
|
| 23 |
+
print("WARNING: duckduckgo_search library not found. Gaia agent's DDG search will be unavailable.")
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
from googleapiclient.discovery import build as build_google_search_service
|
| 27 |
+
from googleapiclient.errors import HttpError as GoogleAPIHttpError
|
| 28 |
+
except ImportError:
|
| 29 |
+
build_google_search_service = None
|
| 30 |
+
GoogleAPIHttpError = None
|
| 31 |
+
print("WARNING: google-api-python-client not found. Gaia agent's Google Custom Search will be unavailable.")
|
| 32 |
+
|
| 33 |
|
|
|
|
| 34 |
# --- Constants ---
|
| 35 |
+
DEFAULT_API_URL = os.getenv("SCORING_API_URL", "https://agents-course-unit4-scoring.hf.space")
|
| 36 |
+
|
| 37 |
+
# API Keys from Environment Variables
|
| 38 |
+
GOOGLE_CUSTOM_SEARCH_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 39 |
+
GOOGLE_CUSTOM_SEARCH_CSE_ID = os.getenv("GOOGLE_CSE_ID")
|
| 40 |
+
GOOGLE_GEMINI_API_KEY = os.getenv("GOOGLE_GEMINI_API_KEY")
|
| 41 |
+
|
| 42 |
+
AGENT_DEFAULT_TIMEOUT = 15 # seconds
|
| 43 |
+
|
| 44 |
+
# --- GaiaLevel1Agent Definition ---
|
| 45 |
+
logging.basicConfig(
|
| 46 |
+
level=logging.INFO,
|
| 47 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 48 |
+
)
|
| 49 |
+
gaia_logger = logging.getLogger('gaia_agent_llm')
|
| 50 |
+
|
| 51 |
+
class GoogleRateLimitError(Exception):
|
| 52 |
+
"""Custom exception for Google API rate limit errors."""
|
| 53 |
+
pass
|
| 54 |
+
|
| 55 |
+
class GaiaLevel1Agent:
|
| 56 |
"""
|
| 57 |
+
GAIA Level 1 Agent that answers questions using file content, web search,
|
| 58 |
+
and a Google Gemini LLM for interpretation and answer generation.
|
| 59 |
"""
|
| 60 |
+
def __init__(self, api_url: str = DEFAULT_API_URL):
|
| 61 |
+
self.api_url = api_url
|
| 62 |
+
self.search_providers: Dict[str, Any] = {}
|
| 63 |
+
self.llm_model: Optional[Any] = None # For Gemini model
|
| 64 |
|
| 65 |
+
# Initialize DuckDuckGo search
|
| 66 |
+
if DDGS:
|
| 67 |
+
try:
|
| 68 |
+
self.search_providers['duckduckgo'] = DDGS()
|
| 69 |
+
gaia_logger.info("DuckDuckGo search initialized for GaiaAgent")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
gaia_logger.error(f"Error initializing DuckDuckGo search for GaiaAgent: {e}")
|
| 72 |
+
|
| 73 |
+
# Initialize Google Custom Search
|
| 74 |
+
if build_google_search_service and GOOGLE_CUSTOM_SEARCH_API_KEY and GOOGLE_CUSTOM_SEARCH_CSE_ID:
|
| 75 |
+
try:
|
| 76 |
+
self.search_providers['google_custom_search'] = build_google_search_service(
|
| 77 |
+
"customsearch", "v1", developerKey=GOOGLE_CUSTOM_SEARCH_API_KEY
|
| 78 |
+
)
|
| 79 |
+
gaia_logger.info("Google Custom Search service initialized for GaiaAgent")
|
| 80 |
+
except Exception as e:
|
| 81 |
+
gaia_logger.error(f"Error initializing Google Custom Search service for GaiaAgent: {e}")
|
| 82 |
+
elif build_google_search_service:
|
| 83 |
+
gaia_logger.warning("Google Custom Search API Key or CSE ID not found. Google Custom Search for GaiaAgent will be unavailable.")
|
| 84 |
+
|
| 85 |
+
# Initialize Google Gemini LLM
|
| 86 |
+
if genai and GOOGLE_GEMINI_API_KEY:
|
| 87 |
+
try:
|
| 88 |
+
genai.configure(api_key=GOOGLE_GEMINI_API_KEY)
|
| 89 |
+
self.llm_model = genai.GenerativeModel('gemini-pro')
|
| 90 |
+
gaia_logger.info("Google Gemini LLM ('gemini-pro') initialized successfully.")
|
| 91 |
+
except Exception as e:
|
| 92 |
+
gaia_logger.error(f"Error initializing Google Gemini LLM: {e}")
|
| 93 |
+
self.llm_model = None
|
| 94 |
+
else:
|
| 95 |
+
if not genai:
|
| 96 |
+
gaia_logger.warning("Google Gemini library (google-generativeai) not installed.")
|
| 97 |
+
if not GOOGLE_GEMINI_API_KEY:
|
| 98 |
+
gaia_logger.warning("GOOGLE_GEMINI_API_KEY not found. Google Gemini LLM will be unavailable.")
|
| 99 |
+
self.llm_model = None
|
| 100 |
|
| 101 |
+
if not self.search_providers:
|
| 102 |
+
gaia_logger.warning("No web search providers available for GaiaAgent.")
|
| 103 |
+
if not self.llm_model:
|
| 104 |
+
gaia_logger.warning("LLM model (Gemini) is not available. Agent will have limited capabilities.")
|
| 105 |
+
|
| 106 |
+
gaia_logger.info(f"GaiaLevel1Agent initialized with scoring API: {self.api_url}")
|
| 107 |
+
|
| 108 |
+
@lru_cache(maxsize=32)
|
| 109 |
+
def _fetch_file_content(self, task_id: str) -> Optional[str]:
|
| 110 |
+
# (Same as before, logging prefixes might change if desired)
|
| 111 |
+
file_url = f"{self.api_url}/files/{task_id}"
|
| 112 |
+
gaia_logger.info(f"GaiaAgent fetching file from: {file_url}")
|
| 113 |
+
for attempt in range(3):
|
| 114 |
+
try:
|
| 115 |
+
response = requests.get(file_url, timeout=AGENT_DEFAULT_TIMEOUT)
|
| 116 |
+
response.raise_for_status()
|
| 117 |
+
content_type = response.headers.get("Content-Type", "")
|
| 118 |
+
if "text" in content_type or "json" in content_type or "csv" in content_type:
|
| 119 |
+
return response.text
|
| 120 |
+
return None
|
| 121 |
+
except requests.exceptions.HTTPError as e:
|
| 122 |
+
if e.response.status_code == 404: return None
|
| 123 |
+
gaia_logger.warning(f"GaiaAgent: HTTP error fetching file: {e}")
|
| 124 |
+
except requests.exceptions.Timeout:
|
| 125 |
+
if attempt < 2: time.sleep(1)
|
| 126 |
+
except Exception as e:
|
| 127 |
+
if attempt < 2: time.sleep(1)
|
| 128 |
+
return None
|
| 129 |
+
|
| 130 |
+
def _perform_duckduckgo_search(self, query: str, max_results: int = 3) -> Optional[str]:
|
| 131 |
+
# (Same as before)
|
| 132 |
+
if 'duckduckgo' not in self.search_providers: return None
|
| 133 |
+
gaia_logger.info(f"GaiaAgent performing DuckDuckGo search for: '{query}'")
|
| 134 |
+
try:
|
| 135 |
+
results = self.search_providers['duckduckgo'].text(query, max_results=max_results)
|
| 136 |
+
if not results: return None
|
| 137 |
+
return "\n\n".join([f"DDG Result: {r.get('title', '')}\n{r.get('body', '')}" for r in results])
|
| 138 |
+
except Exception as e: gaia_logger.error(f"GaiaAgent: DuckDuckGo search error: {e}"); return None
|
| 139 |
+
|
| 140 |
+
def _perform_google_custom_search(self, query: str, num_results: int = 3) -> Optional[str]:
|
| 141 |
+
# (Modified to use renamed constants, otherwise same logic)
|
| 142 |
+
if 'google_custom_search' not in self.search_providers or not GoogleAPIHttpError: return None
|
| 143 |
+
gaia_logger.info(f"GaiaAgent performing Google Custom Search for: '{query}'")
|
| 144 |
+
try:
|
| 145 |
+
google_service: Any = self.search_providers['google_custom_search']
|
| 146 |
+
result: Any = google_service.cse().list(q=query, cx=GOOGLE_CUSTOM_SEARCH_CSE_ID, num=num_results).execute()
|
| 147 |
+
items = result.get('items', [])
|
| 148 |
+
if not items: gaia_logger.info("GaiaAgent: Google Custom Search returned no results."); return None
|
| 149 |
+
return "\n\n".join([f"Google Result: {item.get('title', '')}\n{item.get('snippet', '')}" for item in items])
|
| 150 |
+
except GoogleAPIHttpError as e:
|
| 151 |
+
# (Rate limit detection logic remains the same)
|
| 152 |
+
is_rate_limit = False; api_error_reason = "Unknown"
|
| 153 |
+
if e.resp.status == 429 or e.resp.status == 403:
|
| 154 |
+
try:
|
| 155 |
+
error_details = json.loads(e.content.decode('utf-8'))
|
| 156 |
+
if isinstance(error_details.get('error'), dict):
|
| 157 |
+
errors_list = error_details['error'].get('errors', [])
|
| 158 |
+
if errors_list and isinstance(errors_list[0], dict): api_error_reason = errors_list[0].get('reason', 'UnknownReason')
|
| 159 |
+
message = error_details['error'].get('message', '').lower()
|
| 160 |
+
if "rate limit" in message or "quota exceeded" in message or "daily limit exceeded" in message:
|
| 161 |
+
if api_error_reason == 'UnknownReason' or api_error_reason == 'forbidden' or api_error_reason == 'backendError': api_error_reason = "rateLimitSuspectedInMessage"
|
| 162 |
+
rate_limit_reasons = ['rateLimitExceeded', 'userRateLimitExceeded', 'quotaExceeded', 'dailyLimitExceeded', 'rateLimitSuspectedInMessage']
|
| 163 |
+
if api_error_reason in rate_limit_reasons: is_rate_limit = True
|
| 164 |
+
except Exception: pass # Ignore parsing errors for simplicity here
|
| 165 |
+
if is_rate_limit:
|
| 166 |
+
log_msg = f"GaiaAgent: Google Custom Search API rate limit identified. Reason: {api_error_reason}."
|
| 167 |
+
if api_error_reason in ['quotaExceeded', 'dailyLimitExceeded']: log_msg += " (Daily free tier quota might be exceeded)."
|
| 168 |
+
gaia_logger.warning(log_msg)
|
| 169 |
+
raise GoogleRateLimitError(f"Google API rate limit: {api_error_reason}") from e
|
| 170 |
+
gaia_logger.error(f"GaiaAgent: Google Custom Search API HttpError: {e}"); return None
|
| 171 |
+
except Exception as e: gaia_logger.error(f"GaiaAgent: Unexpected error during Google Custom Search: {e}", exc_info=True); return None
|
| 172 |
+
|
| 173 |
+
def _formulate_answer_with_llm(self, question: str, file_context: Optional[str], web_context: Optional[str]) -> str:
|
| 174 |
+
if not self.llm_model:
|
| 175 |
+
gaia_logger.warning("LLM model (Gemini) not available for answer formulation.")
|
| 176 |
+
# Fallback to a simple rule if LLM is not there
|
| 177 |
+
if web_context: return f"Based on web search: {web_context.splitlines()[0] if web_context.splitlines() else 'No specific snippet found.'}"
|
| 178 |
+
if file_context: return f"Based on the provided document (first 100 chars): {file_context[:100]}..."
|
| 179 |
+
return "I am currently unable to process this request fully as the LLM is not available. I don't have enough information without it."
|
| 180 |
+
|
| 181 |
+
prompt_parts = [
|
| 182 |
+
"You are a helpful AI assistant. Your task is to answer the user's question based *only* on the provided context from a document and/or web search results. Be concise and directly answer the question.",
|
| 183 |
+
"If the provided context is insufficient to answer the question, clearly state that the information is not available in the provided materials.",
|
| 184 |
+
"\nUser Question: ",
|
| 185 |
+
question
|
| 186 |
+
]
|
| 187 |
+
|
| 188 |
+
if file_context:
|
| 189 |
+
prompt_parts.append("\n\nContext from Provided Document:\n---")
|
| 190 |
+
prompt_parts.append(file_context[:4000]) # Limit context size for the prompt
|
| 191 |
+
prompt_parts.append("---")
|
| 192 |
+
else:
|
| 193 |
+
prompt_parts.append("\n\nContext from Provided Document:\n---")
|
| 194 |
+
prompt_parts.append("No document was provided or it could not be retrieved.")
|
| 195 |
+
prompt_parts.append("---")
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
if web_context:
|
| 199 |
+
prompt_parts.append("\n\nContext from Web Search Results:\n---")
|
| 200 |
+
prompt_parts.append(web_context[:4000]) # Limit context size
|
| 201 |
+
prompt_parts.append("---")
|
| 202 |
+
else:
|
| 203 |
+
prompt_parts.append("\n\nContext from Web Search Results:\n---")
|
| 204 |
+
prompt_parts.append("No web search was performed or no relevant results were found.")
|
| 205 |
+
prompt_parts.append("---")
|
| 206 |
+
|
| 207 |
+
prompt_parts.append("\n\nAnswer:")
|
| 208 |
+
final_prompt = "\n".join(prompt_parts)
|
| 209 |
+
|
| 210 |
+
gaia_logger.info(f"GaiaAgent sending prompt to Gemini (first 300 chars): {final_prompt[:300]}...")
|
| 211 |
+
|
| 212 |
+
try:
|
| 213 |
+
# More robust generation config
|
| 214 |
+
generation_config = genai.types.GenerationConfig(
|
| 215 |
+
# candidate_count=1, # default
|
| 216 |
+
# stop_sequences=["\n\n"], # Optional: if you want to force shorter answers
|
| 217 |
+
max_output_tokens=1024, # Or a number appropriate for your expected answer length
|
| 218 |
+
temperature=0.7, # Adjust for creativity vs. factuality
|
| 219 |
+
# top_p=0.9, # Adjust as needed
|
| 220 |
+
# top_k=40 # Adjust as needed
|
| 221 |
+
)
|
| 222 |
+
# Safety settings (example - adjust as needed)
|
| 223 |
+
safety_settings = [
|
| 224 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 225 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 226 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 227 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 228 |
+
]
|
| 229 |
+
|
| 230 |
+
response = self.llm_model.generate_content(
|
| 231 |
+
final_prompt,
|
| 232 |
+
generation_config=generation_config,
|
| 233 |
+
safety_settings=safety_settings
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
# Check for blocked response due to safety
|
| 237 |
+
if not response.candidates or response.prompt_feedback.block_reason:
|
| 238 |
+
block_reason_str = response.prompt_feedback.block_reason.name if response.prompt_feedback.block_reason else "Unknown"
|
| 239 |
+
safety_ratings_str = ", ".join([f"{rating.category.name}: {rating.probability.name}" for rating in response.prompt_feedback.safety_ratings])
|
| 240 |
+
gaia_logger.warning(f"Gemini response blocked. Reason: {block_reason_str}. Safety ratings: [{safety_ratings_str}]")
|
| 241 |
+
return f"My response was blocked due to safety concerns (Reason: {block_reason_str}). Please rephrase your question or check the context."
|
| 242 |
+
|
| 243 |
+
llm_answer = response.text
|
| 244 |
+
gaia_logger.info(f"GaiaAgent received answer from Gemini (first 100 chars): {llm_answer[:100]}...")
|
| 245 |
+
return llm_answer
|
| 246 |
+
except Exception as e:
|
| 247 |
+
gaia_logger.error(f"Error calling Google Gemini API: {e}", exc_info=True)
|
| 248 |
+
return "I encountered an error while trying to generate an answer using the LLM. Please try again."
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
def __call__(self, question: str, task_id: Optional[str] = None) -> str:
|
| 252 |
+
gaia_logger.info(f"GaiaAgent processing question: '{question[:100]}...', Task ID: {task_id}")
|
| 253 |
+
|
| 254 |
+
# Simple identity check - can be handled without LLM
|
| 255 |
+
question_lower = question.lower().strip()
|
| 256 |
+
if "what is your name" in question_lower or "who are you" in question_lower:
|
| 257 |
+
return "I am a GAIA-oriented AI assistant. I use web search and document content, processed by an LLM, to answer questions."
|
| 258 |
+
|
| 259 |
+
context_from_file = ""
|
| 260 |
+
if task_id and any(kw in question.lower() for kw in ["document", "file", "provided text", "attached", "read", "content"]):
|
| 261 |
+
file_content = self._fetch_file_content(task_id)
|
| 262 |
+
if file_content:
|
| 263 |
+
context_from_file = file_content
|
| 264 |
+
gaia_logger.info(f"GaiaAgent: File content fetched ({len(context_from_file)} chars)")
|
| 265 |
+
|
| 266 |
+
context_from_web = ""
|
| 267 |
+
needs_web_search = False
|
| 268 |
+
web_search_keywords = ["what is", "who is", "how to", "when did", "where is", "current",
|
| 269 |
+
"latest", "news", "define", "capital", "president", "recent", "explain"]
|
| 270 |
+
if any(kw in question.lower() for kw in web_search_keywords):
|
| 271 |
+
needs_web_search = True
|
| 272 |
+
if not context_from_file and len(question.split()) > 4: # If no file and question is not trivial
|
| 273 |
+
needs_web_search = True
|
| 274 |
+
|
| 275 |
+
if needs_web_search:
|
| 276 |
+
search_query = question.replace("?", "").strip()
|
| 277 |
+
web_search_results: Optional[str] = None
|
| 278 |
+
if 'google_custom_search' in self.search_providers:
|
| 279 |
+
try:
|
| 280 |
+
gaia_logger.info("GaiaAgent: Attempting Google Custom Search.")
|
| 281 |
+
web_search_results = self._perform_google_custom_search(search_query)
|
| 282 |
+
except GoogleRateLimitError:
|
| 283 |
+
gaia_logger.warning("GaiaAgent: Google Custom Search rate limit hit. Falling back to DuckDuckGo.")
|
| 284 |
+
if 'duckduckgo' in self.search_providers:
|
| 285 |
+
web_search_results = self._perform_duckduckgo_search(search_query)
|
| 286 |
+
except Exception as e:
|
| 287 |
+
gaia_logger.error(f"GaiaAgent: Error during Google Custom Search attempt: {e}")
|
| 288 |
+
|
| 289 |
+
if not web_search_results and 'duckduckgo' in self.search_providers: # Fallback or primary if Google CS not used/failed
|
| 290 |
+
gaia_logger.info("GaiaAgent: Attempting DuckDuckGo Search.")
|
| 291 |
+
web_search_results = self._perform_duckduckgo_search(search_query)
|
| 292 |
+
|
| 293 |
+
if web_search_results:
|
| 294 |
+
context_from_web = web_search_results
|
| 295 |
+
gaia_logger.info(f"GaiaAgent: Web search results obtained ({len(context_from_web)} chars)")
|
| 296 |
+
else:
|
| 297 |
+
gaia_logger.warning("GaiaAgent: Web search did not yield results.")
|
| 298 |
+
|
| 299 |
+
# Use LLM to formulate the final answer
|
| 300 |
+
answer = self._formulate_answer_with_llm(question, context_from_file, context_from_web)
|
| 301 |
+
gaia_logger.info(f"GaiaAgent final answer (first 100 chars): {answer[:100]}...")
|
| 302 |
+
return answer
|
| 303 |
+
|
| 304 |
+
# --- Evaluation Runner (largely unchanged, agent instantiation and call are key) ---
|
| 305 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 306 |
+
space_id = os.getenv("SPACE_ID")
|
| 307 |
+
if profile: username = f"{profile.username}"; print(f"User logged in: {username}")
|
| 308 |
+
else: print("User not logged in."); return "Please Login to Hugging Face with the button.", None
|
| 309 |
+
|
| 310 |
+
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 311 |
+
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 312 |
|
|
|
|
| 313 |
try:
|
| 314 |
+
agent = GaiaLevel1Agent(api_url=DEFAULT_API_URL) # Instantiates our LLM agent
|
| 315 |
+
print("GaiaLevel1Agent (LLM-enabled) initialized for evaluation.")
|
| 316 |
except Exception as e:
|
| 317 |
+
print(f"Error instantiating agent: {e}"); return f"Error initializing agent: {e}", None
|
| 318 |
+
|
| 319 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code link not available"
|
| 320 |
+
print(f"Agent code link: {agent_code}")
|
| 321 |
+
|
|
|
|
|
|
|
|
|
|
| 322 |
try:
|
| 323 |
+
response = requests.get(questions_url, timeout=15); response.raise_for_status()
|
|
|
|
| 324 |
questions_data = response.json()
|
| 325 |
+
if not questions_data or not isinstance(questions_data, list):
|
| 326 |
+
print(f"Fetched questions list is empty/invalid: {questions_data}"); return "Fetched questions list is empty/invalid.", None
|
|
|
|
| 327 |
print(f"Fetched {len(questions_data)} questions.")
|
| 328 |
+
except Exception as e: print(f"Error fetching questions: {e}"); return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
+
results_log = []; answers_payload = []
|
|
|
|
|
|
|
| 331 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 332 |
for item in questions_data:
|
| 333 |
+
task_id = item.get("task_id"); question_text = item.get("question")
|
|
|
|
| 334 |
if not task_id or question_text is None:
|
| 335 |
+
print(f"Skipping item with missing data: {item}")
|
| 336 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": "SKIPPED - Missing data"})
|
| 337 |
continue
|
| 338 |
try:
|
| 339 |
+
submitted_answer = agent(question=question_text, task_id=task_id) # Agent call
|
| 340 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 341 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 342 |
except Exception as e:
|
| 343 |
print(f"Error running agent on task {task_id}: {e}")
|
| 344 |
+
gaia_logger.error(f"Exception in agent call for task {task_id}", exc_info=True)
|
| 345 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 346 |
|
| 347 |
if not answers_payload:
|
| 348 |
+
if not results_log: results_log.append({"Task ID": "N/A", "Question": "N/A", "Submitted Answer": "No questions processed."})
|
| 349 |
+
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
| 350 |
|
|
|
|
| 351 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 352 |
+
print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
try:
|
| 354 |
+
response = requests.post(submit_url, json=submission_data, timeout=60); response.raise_for_status()
|
|
|
|
| 355 |
result_data = response.json()
|
| 356 |
+
final_status = (f"Submission Successful!\nUser: {result_data.get('username')}\nOverall Score: {result_data.get('score', 'N/A')}% "
|
| 357 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 358 |
+
f"Message: {result_data.get('message', 'No message.')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
print("Submission successful.")
|
| 360 |
+
return final_status, pd.DataFrame(results_log)
|
|
|
|
| 361 |
except requests.exceptions.HTTPError as e:
|
| 362 |
+
error_detail = f"Server responded: {e.response.status_code}."
|
| 363 |
+
try: error_detail += f" Detail: {e.response.json().get('detail', e.response.text)}"
|
| 364 |
+
except Exception: error_detail += f" Response: {e.response.text[:200]}"
|
| 365 |
+
print(f"Submission Failed: {error_detail}")
|
| 366 |
+
return f"Submission Failed: {error_detail}", pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
except Exception as e:
|
| 368 |
+
print(f"Submission Failed: {e}"); return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
# --- Gradio Interface (Unchanged) ---
|
| 371 |
with gr.Blocks() as demo:
|
| 372 |
+
gr.Markdown("# Gaia Level 1 Agent (LLM-Powered) Evaluation Runner")
|
| 373 |
gr.Markdown(
|
| 374 |
"""
|
| 375 |
**Instructions:**
|
| 376 |
+
1. This space runs the Gaia Level 1 Agent, now powered by Google Gemini for answer generation.
|
| 377 |
+
2. Log in to your Hugging Face account using the button below.
|
| 378 |
+
3. Click 'Run Evaluation & Submit All Answers' to process questions and see the score.
|
|
|
|
|
|
|
| 379 |
---
|
| 380 |
+
**Note:** Ensure `GOOGLE_GEMINI_API_KEY`, `GOOGLE_API_KEY` (for Custom Search), and `GOOGLE_CSE_ID` are set as secrets in your Space for full functionality.
|
|
|
|
|
|
|
| 381 |
"""
|
| 382 |
)
|
|
|
|
| 383 |
gr.LoginButton()
|
|
|
|
| 384 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 385 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 386 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 387 |
+
run_button.click(fn=run_and_submit_all, inputs=[gr.State()], outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
|
| 389 |
if __name__ == "__main__":
|
| 390 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 391 |
+
# Startup checks for API keys
|
| 392 |
+
if not GOOGLE_GEMINI_API_KEY: print("⚠️ WARNING: GOOGLE_GEMINI_API_KEY not set. LLM functionality will be disabled.")
|
| 393 |
+
else: print("✅ GOOGLE_GEMINI_API_KEY found.")
|
| 394 |
+
if not GOOGLE_CUSTOM_SEARCH_API_KEY or not GOOGLE_CUSTOM_SEARCH_CSE_ID: print("⚠️ WARNING: GOOGLE_API_KEY (for Custom Search) or GOOGLE_CSE_ID not set. Google Custom Search will be unavailable.")
|
| 395 |
+
else: print("✅ Google Custom Search API Key and CSE ID found.")
|
| 396 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 398 |
+
print("Launching Gradio Interface for Gaia Level 1 Agent (LLM-Powered) Evaluation...")
|
|
|
|
| 399 |
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,7 @@
|
|
| 1 |
gradio
|
| 2 |
-
requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
+
requests
|
| 3 |
+
duckduckgo-search
|
| 4 |
+
google-api-python-client
|
| 5 |
+
httpx[http2]
|
| 6 |
+
google-generativeai
|
| 7 |
+
pandas
|