Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
@@ -9,7 +10,6 @@ from agents import Agent
|
|
| 9 |
from tool import get_tools
|
| 10 |
from model import get_model
|
| 11 |
|
| 12 |
-
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
MODEL_ID = "gemini/gemini-2.5-flash-preview-04-17"
|
|
@@ -31,11 +31,13 @@ async def process_question(agent, question: str, task_id: str) -> Dict:
|
|
| 31 |
}
|
| 32 |
|
| 33 |
async def run_questions_async(agent, questions_data: List[Dict]) -> tuple:
|
| 34 |
-
"""Process questions sequentially
|
| 35 |
submissions = []
|
| 36 |
logs = []
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
result = await process_question(agent, q["question"], q["task_id"])
|
| 40 |
submissions.append(result["submission"])
|
| 41 |
logs.append(result["log"])
|
|
@@ -43,16 +45,15 @@ async def run_questions_async(agent, questions_data: List[Dict]) -> tuple:
|
|
| 43 |
return submissions, logs
|
| 44 |
|
| 45 |
|
| 46 |
-
async def run_and_submit_all(
|
| 47 |
"""
|
| 48 |
-
Fetches all questions, runs the
|
| 49 |
and displays the results.
|
| 50 |
"""
|
| 51 |
-
|
| 52 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 53 |
|
| 54 |
if profile:
|
| 55 |
-
username= f"{profile.username}"
|
| 56 |
print(f"User logged in: {username}")
|
| 57 |
else:
|
| 58 |
print("User not logged in.")
|
|
@@ -71,9 +72,9 @@ async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 71 |
except Exception as e:
|
| 72 |
print(f"Error instantiating agent: {e}")
|
| 73 |
return f"Error initializing agent: {e}", None
|
| 74 |
-
|
| 75 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 76 |
-
print(agent_code)
|
| 77 |
|
| 78 |
# 2. Fetch Questions
|
| 79 |
print(f"Fetching questions from: {questions_url}")
|
|
@@ -82,22 +83,15 @@ async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 82 |
response.raise_for_status()
|
| 83 |
questions_data = response.json()
|
| 84 |
if not questions_data:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
print(f"Fetched {len(questions_data)} questions.")
|
| 88 |
-
questions_data = questions_data[:2]
|
| 89 |
-
except
|
| 90 |
print(f"Error fetching questions: {e}")
|
| 91 |
return f"Error fetching questions: {e}", None
|
| 92 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 93 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 94 |
-
print(f"Response text: {response.text[:500]}")
|
| 95 |
-
return f"Error decoding server response for questions: {e}", None
|
| 96 |
-
except Exception as e:
|
| 97 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
| 98 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 99 |
|
| 100 |
-
# 3. Run
|
| 101 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 102 |
answers_payload, results_log = await run_questions_async(agent, questions_data)
|
| 103 |
|
|
@@ -106,73 +100,58 @@ async def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 106 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 107 |
|
| 108 |
# 4. Prepare Submission
|
| 109 |
-
submission_data = {
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# 5. Submit
|
| 114 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 115 |
try:
|
| 116 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 117 |
response.raise_for_status()
|
| 118 |
result_data = response.json()
|
| 119 |
final_status = (
|
| 120 |
-
f"Submission Successful!\n"
|
| 121 |
f"User: {result_data.get('username')}\n"
|
| 122 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 123 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 124 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
|
|
|
| 125 |
)
|
| 126 |
print("Submission successful.")
|
| 127 |
results_df = pd.DataFrame(results_log)
|
| 128 |
return final_status, results_df
|
| 129 |
-
except requests.exceptions.HTTPError as e:
|
| 130 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 131 |
-
try:
|
| 132 |
-
error_json = e.response.json()
|
| 133 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 134 |
-
except requests.exceptions.JSONDecodeError:
|
| 135 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 136 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 137 |
-
print(status_message)
|
| 138 |
-
results_df = pd.DataFrame(results_log)
|
| 139 |
-
return status_message, results_df
|
| 140 |
-
except requests.exceptions.Timeout:
|
| 141 |
-
status_message = "Submission Failed: The request timed out."
|
| 142 |
-
print(status_message)
|
| 143 |
-
results_df = pd.DataFrame(results_log)
|
| 144 |
-
return status_message, results_df
|
| 145 |
-
except requests.exceptions.RequestException as e:
|
| 146 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 147 |
-
print(status_message)
|
| 148 |
-
results_df = pd.DataFrame(results_log)
|
| 149 |
-
return status_message, results_df
|
| 150 |
except Exception as e:
|
| 151 |
-
status_message = f"
|
| 152 |
print(status_message)
|
| 153 |
results_df = pd.DataFrame(results_log)
|
| 154 |
return status_message, results_df
|
| 155 |
|
| 156 |
|
| 157 |
-
# --- Build Gradio Interface
|
| 158 |
-
with gr.Blocks() as demo:
|
| 159 |
-
gr.Markdown("#
|
| 160 |
gr.Markdown(
|
| 161 |
"""
|
| 162 |
**Instructions:**
|
| 163 |
-
1.
|
| 164 |
-
2.
|
| 165 |
-
3.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
"""
|
| 167 |
)
|
| 168 |
|
| 169 |
gr.LoginButton()
|
| 170 |
|
| 171 |
-
run_button = gr.Button("Run Evaluation & Submit
|
| 172 |
|
| 173 |
-
status_output = gr.Textbox(label="
|
| 174 |
-
|
| 175 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 176 |
|
| 177 |
run_button.click(
|
| 178 |
fn=run_and_submit_all,
|
|
@@ -180,25 +159,18 @@ with gr.Blocks() as demo:
|
|
| 180 |
)
|
| 181 |
|
| 182 |
if __name__ == "__main__":
|
| 183 |
-
print("\n" + "
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
print(f"β
SPACE_HOST found: {space_host_startup}")
|
| 190 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 191 |
-
else:
|
| 192 |
-
print("βΉοΈ SPACE_HOST environment variable not found (running locally?).")
|
| 193 |
-
|
| 194 |
-
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 195 |
-
print(f"β
SPACE_ID found: {space_id_startup}")
|
| 196 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 197 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 198 |
-
else:
|
| 199 |
-
print("βΉοΈ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 200 |
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 204 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
|
|
|
| 10 |
from tool import get_tools
|
| 11 |
from model import get_model
|
| 12 |
|
|
|
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
MODEL_ID = "gemini/gemini-2.5-flash-preview-04-17"
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
async def run_questions_async(agent, questions_data: List[Dict]) -> tuple:
|
| 34 |
+
"""Process questions sequentially"""
|
| 35 |
submissions = []
|
| 36 |
logs = []
|
| 37 |
|
| 38 |
+
total = len(questions_data)
|
| 39 |
+
for idx, q in enumerate(questions_data):
|
| 40 |
+
print(f"Processing {idx+1}/{total}: {q['question'][:80]}...")
|
| 41 |
result = await process_question(agent, q["question"], q["task_id"])
|
| 42 |
submissions.append(result["submission"])
|
| 43 |
logs.append(result["log"])
|
|
|
|
| 45 |
return submissions, logs
|
| 46 |
|
| 47 |
|
| 48 |
+
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 49 |
"""
|
| 50 |
+
Fetches all questions, runs the Agent on them, submits all answers,
|
| 51 |
and displays the results.
|
| 52 |
"""
|
| 53 |
+
space_id = os.getenv("SPACE_ID")
|
|
|
|
| 54 |
|
| 55 |
if profile:
|
| 56 |
+
username = f"{profile.username}"
|
| 57 |
print(f"User logged in: {username}")
|
| 58 |
else:
|
| 59 |
print("User not logged in.")
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Error instantiating agent: {e}")
|
| 74 |
return f"Error initializing agent: {e}", None
|
| 75 |
+
|
| 76 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 77 |
+
print(f"Agent code: {agent_code}")
|
| 78 |
|
| 79 |
# 2. Fetch Questions
|
| 80 |
print(f"Fetching questions from: {questions_url}")
|
|
|
|
| 83 |
response.raise_for_status()
|
| 84 |
questions_data = response.json()
|
| 85 |
if not questions_data:
|
| 86 |
+
print("Fetched questions list is empty.")
|
| 87 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 88 |
print(f"Fetched {len(questions_data)} questions.")
|
| 89 |
+
# Remove this line to process all questions: questions_data = questions_data[:2]
|
| 90 |
+
except Exception as e:
|
| 91 |
print(f"Error fetching questions: {e}")
|
| 92 |
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# 3. Run Agent
|
| 95 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 96 |
answers_payload, results_log = await run_questions_async(agent, questions_data)
|
| 97 |
|
|
|
|
| 100 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 101 |
|
| 102 |
# 4. Prepare Submission
|
| 103 |
+
submission_data = {
|
| 104 |
+
"username": username.strip(),
|
| 105 |
+
"agent_code": agent_code,
|
| 106 |
+
"answers": answers_payload
|
| 107 |
+
}
|
| 108 |
+
print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
|
| 109 |
|
| 110 |
# 5. Submit
|
|
|
|
| 111 |
try:
|
| 112 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 113 |
response.raise_for_status()
|
| 114 |
result_data = response.json()
|
| 115 |
final_status = (
|
| 116 |
+
f"β
Submission Successful!\n\n"
|
| 117 |
f"User: {result_data.get('username')}\n"
|
| 118 |
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 119 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n\n"
|
| 120 |
+
f"Message: {result_data.get('message', 'No message received.')}\n\n"
|
| 121 |
+
f"Leaderboard: {api_url}/leaderboard"
|
| 122 |
)
|
| 123 |
print("Submission successful.")
|
| 124 |
results_df = pd.DataFrame(results_log)
|
| 125 |
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
except Exception as e:
|
| 127 |
+
status_message = f"β Submission Failed: {e}"
|
| 128 |
print(status_message)
|
| 129 |
results_df = pd.DataFrame(results_log)
|
| 130 |
return status_message, results_df
|
| 131 |
|
| 132 |
|
| 133 |
+
# --- Build Gradio Interface ---
|
| 134 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 135 |
+
gr.Markdown("# π€ GAIA Agent Evaluation")
|
| 136 |
gr.Markdown(
|
| 137 |
"""
|
| 138 |
**Instructions:**
|
| 139 |
+
1. Log in to your Hugging Face account using the button below
|
| 140 |
+
2. Click 'Run Evaluation & Submit' to test your agent
|
| 141 |
+
3. The agent will use web search and other tools to answer questions
|
| 142 |
+
|
| 143 |
+
**Current Setup:**
|
| 144 |
+
- Model: Gemini 2.5 Flash (via LiteLLM)
|
| 145 |
+
- Tools: Web search, Wikipedia, calculation, and more
|
| 146 |
"""
|
| 147 |
)
|
| 148 |
|
| 149 |
gr.LoginButton()
|
| 150 |
|
| 151 |
+
run_button = gr.Button("π Run Evaluation & Submit", variant="primary")
|
| 152 |
|
| 153 |
+
status_output = gr.Textbox(label="π Status / Results", lines=8, interactive=False)
|
| 154 |
+
results_table = gr.DataFrame(label="π Questions and Answers", wrap=True, max_height=400)
|
|
|
|
| 155 |
|
| 156 |
run_button.click(
|
| 157 |
fn=run_and_submit_all,
|
|
|
|
| 159 |
)
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
+
print("\n" + "="*70)
|
| 163 |
+
print("π€ GAIA Agent Starting")
|
| 164 |
+
print("="*70)
|
| 165 |
+
|
| 166 |
+
space_host = os.getenv("SPACE_HOST")
|
| 167 |
+
space_id = os.getenv("SPACE_ID")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
if space_host:
|
| 170 |
+
print(f"β
Runtime URL: https://{space_host}.hf.space")
|
| 171 |
+
if space_id:
|
| 172 |
+
print(f"β
Repo URL: https://huggingface.co/spaces/{space_id}")
|
| 173 |
+
|
| 174 |
+
print("="*70 + "\n")
|
| 175 |
+
demo.launch(debug=True, share=False)
|
| 176 |
|
|
|
|
|
|