Update app.py for final submission
Browse filesAdd lines to hide API keys and check for required environment variables.
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -12,14 +13,19 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 25 |
and displays the results.
|
|
@@ -33,7 +39,19 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 33 |
else:
|
| 34 |
print("User not logged in.")
|
| 35 |
return "Please Login to Hugging Face with the button.", None
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
api_url = DEFAULT_API_URL
|
| 38 |
questions_url = f"{api_url}/questions"
|
| 39 |
submit_url = f"{api_url}/submit"
|
|
@@ -68,7 +86,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | 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 |
# 3. Run your Agent
|
| 73 |
results_log = []
|
| 74 |
answers_payload = []
|
|
@@ -80,7 +98,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 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:
|
|
@@ -139,14 +157,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 139 |
results_df = pd.DataFrame(results_log)
|
| 140 |
return status_message, results_df
|
| 141 |
|
| 142 |
-
|
| 143 |
# --- Build Gradio Interface using Blocks ---
|
| 144 |
with gr.Blocks() as demo:
|
| 145 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
-
|
| 150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import agents
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
|
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
+
self.gaia = agents.create_general_ai_agent(verbosity=0)
|
| 17 |
+
print('BasicAgent initialized.')
|
| 18 |
+
def __call__(self, task_id: str, question: str) -> str:
|
| 19 |
+
print(f"Agent received questoin (first 50 chars): {question[:50]}...")
|
| 20 |
+
task = self.gaia.invoke({
|
| 21 |
+
'task_id': task_id,
|
| 22 |
+
'question': question,
|
| 23 |
+
})
|
| 24 |
+
final_answer = task.get('final_answer')
|
| 25 |
+
print(f'Agent returning fixed answer: {final_answer}')
|
| 26 |
+
return task['final_answer']
|
| 27 |
+
|
| 28 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 29 |
"""
|
| 30 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 31 |
and displays the results.
|
|
|
|
| 39 |
else:
|
| 40 |
print("User not logged in.")
|
| 41 |
return "Please Login to Hugging Face with the button.", None
|
| 42 |
+
|
| 43 |
+
# --- Allow only space owner to run agent to avoid misuse ---
|
| 44 |
+
if not space_id.startswith(username.strip()):
|
| 45 |
+
print("User is not an owner of the space. Please duplicate space and configure OPENAI_API_KEY, HF_TOKEN, GOOGLE_SEARCH_API_KEY, and GOOGLE_SEARCH_ENGINE_ID environment variables.")
|
| 46 |
+
return "Please duplicate space to your account to run the agent.", None
|
| 47 |
+
|
| 48 |
+
# --- Check for required environment variables ---
|
| 49 |
+
required_env_vars = ["OPENAI_API_KEY", "HF_TOKEN", "GOOGLE_SEARCH_API_KEY", "GOOGLE_SEARCH_ENGINE_ID"]
|
| 50 |
+
missing_env_vars = [var for var in required_env_vars if not os.getenv(var)]
|
| 51 |
+
if missing_env_vars:
|
| 52 |
+
print(f"Missing environment variables: {', '.join(missing_env_vars)}")
|
| 53 |
+
return f"Missing environment variables: {', '.join(missing_env_vars)}", None
|
| 54 |
+
|
| 55 |
api_url = DEFAULT_API_URL
|
| 56 |
questions_url = f"{api_url}/questions"
|
| 57 |
submit_url = f"{api_url}/submit"
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 88 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 89 |
+
|
| 90 |
# 3. Run your Agent
|
| 91 |
results_log = []
|
| 92 |
answers_payload = []
|
|
|
|
| 98 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 99 |
continue
|
| 100 |
try:
|
| 101 |
+
submitted_answer = agent(task_id=task_id, question=question_text)
|
| 102 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 103 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 104 |
except Exception as e:
|
|
|
|
| 157 |
results_df = pd.DataFrame(results_log)
|
| 158 |
return status_message, results_df
|
| 159 |
|
| 160 |
+
|
| 161 |
# --- Build Gradio Interface using Blocks ---
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 164 |
gr.Markdown(
|
| 165 |
"""
|
| 166 |
**Instructions:**
|
| 167 |
+
|
| 168 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 169 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 170 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|