Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,28 +3,30 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
6 |
-
#
|
7 |
from agent import GeminiAgent
|
8 |
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
-
|
|
|
12 |
|
13 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
14 |
"""
|
15 |
Fetches all questions, runs the GeminiAgent on them, submits all answers,
|
16 |
-
and displays the results. This function is restricted to a specific user
|
|
|
17 |
"""
|
18 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
19 |
space_id = os.getenv("SPACE_ID")
|
20 |
|
|
|
21 |
if not profile:
|
22 |
return "Please Login to Hugging Face with the button to run the evaluation.", None
|
23 |
|
24 |
username = profile.username
|
25 |
print(f"User logged in: {username}")
|
26 |
|
27 |
-
# --- NEW: Restrict submission to a specific user ---
|
28 |
if username != MY_HF_USERNAME:
|
29 |
print(f"Access denied for user: {username}. Allowed user is {MY_HF_USERNAME}.")
|
30 |
return f"Error: This Space is configured for a specific user. Access denied for '{username}'.", None
|
@@ -33,8 +35,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
33 |
questions_url = f"{api_url}/questions"
|
34 |
submit_url = f"{api_url}/submit"
|
35 |
|
36 |
-
# 1. Instantiate
|
37 |
-
# The agent will fail to initialize if the GEMINI_API_KEY secret is not set.
|
38 |
print("Instantiating agent...")
|
39 |
try:
|
40 |
agent = GeminiAgent()
|
@@ -66,18 +67,30 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
66 |
print(f"Response text: {response.text[:500]}")
|
67 |
return error_msg, None
|
68 |
|
69 |
-
# 3. Run your Agent
|
70 |
results_log = []
|
71 |
answers_payload = []
|
72 |
print(f"Running agent on {len(questions_data)} questions...")
|
73 |
for item in questions_data:
|
74 |
task_id = item.get("task_id")
|
75 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
76 |
if not task_id or question_text is None:
|
77 |
print(f"Skipping item with missing task_id or question: {item}")
|
78 |
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
try:
|
80 |
-
|
|
|
81 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
82 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
83 |
except Exception as e:
|
@@ -95,7 +108,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
95 |
# 5. Submit
|
96 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
97 |
try:
|
98 |
-
response = requests.post(submit_url, json=submission_data, timeout=
|
99 |
response.raise_for_status()
|
100 |
result_data = response.json()
|
101 |
final_status = (
|
@@ -125,21 +138,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
125 |
results_df = pd.DataFrame(results_log)
|
126 |
return status_message, results_df
|
127 |
|
128 |
-
|
129 |
-
# --- Build Gradio Interface using Blocks (No changes needed here) ---
|
130 |
with gr.Blocks() as demo:
|
131 |
-
gr.Markdown("# Gemini Agent
|
132 |
gr.Markdown(
|
133 |
"""
|
134 |
**Instructions:**
|
135 |
-
1.
|
136 |
-
2.
|
137 |
-
3.
|
138 |
-
|
139 |
-
**Note:**
|
140 |
"""
|
141 |
)
|
142 |
-
# The `gr.LoginButton()` passes the OAuthProfile to any function that accepts it as an argument
|
143 |
gr.LoginButton()
|
144 |
|
145 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
@@ -149,7 +160,6 @@ with gr.Blocks() as demo:
|
|
149 |
|
150 |
run_button.click(
|
151 |
fn=run_and_submit_all,
|
152 |
-
# The profile object from the LoginButton is automatically passed to the first argument of the function
|
153 |
outputs=[status_output, results_table]
|
154 |
)
|
155 |
|
|
|
3 |
import requests
|
4 |
import pandas as pd
|
5 |
|
6 |
+
# Import your upgraded agent
|
7 |
from agent import GeminiAgent
|
8 |
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
+
# This is the security gate. Only this user can run submissions.
|
12 |
+
MY_HF_USERNAME = "benjipeng"
|
13 |
|
14 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
15 |
"""
|
16 |
Fetches all questions, runs the GeminiAgent on them, submits all answers,
|
17 |
+
and displays the results. This function is restricted to a specific user and
|
18 |
+
provides file context to the agent.
|
19 |
"""
|
20 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
21 |
space_id = os.getenv("SPACE_ID")
|
22 |
|
23 |
+
# --- User Authentication and Authorization ---
|
24 |
if not profile:
|
25 |
return "Please Login to Hugging Face with the button to run the evaluation.", None
|
26 |
|
27 |
username = profile.username
|
28 |
print(f"User logged in: {username}")
|
29 |
|
|
|
30 |
if username != MY_HF_USERNAME:
|
31 |
print(f"Access denied for user: {username}. Allowed user is {MY_HF_USERNAME}.")
|
32 |
return f"Error: This Space is configured for a specific user. Access denied for '{username}'.", None
|
|
|
35 |
questions_url = f"{api_url}/questions"
|
36 |
submit_url = f"{api_url}/submit"
|
37 |
|
38 |
+
# 1. Instantiate Agent
|
|
|
39 |
print("Instantiating agent...")
|
40 |
try:
|
41 |
agent = GeminiAgent()
|
|
|
67 |
print(f"Response text: {response.text[:500]}")
|
68 |
return error_msg, None
|
69 |
|
70 |
+
# 3. Run your Agent (with context injection)
|
71 |
results_log = []
|
72 |
answers_payload = []
|
73 |
print(f"Running agent on {len(questions_data)} questions...")
|
74 |
for item in questions_data:
|
75 |
task_id = item.get("task_id")
|
76 |
question_text = item.get("question")
|
77 |
+
|
78 |
+
# This is the key improvement: check if a file is associated with the question
|
79 |
+
has_file = item.get("file", None) is not None
|
80 |
+
|
81 |
if not task_id or question_text is None:
|
82 |
print(f"Skipping item with missing task_id or question: {item}")
|
83 |
continue
|
84 |
+
|
85 |
+
# Modify the question to give the agent context about the file's existence
|
86 |
+
if has_file:
|
87 |
+
modified_question = f"{question_text}\n\n[Agent Note: A file is attached to this question. Use the 'read_file_from_api' tool to access it if needed.]"
|
88 |
+
else:
|
89 |
+
modified_question = question_text
|
90 |
+
|
91 |
try:
|
92 |
+
# Pass BOTH the modified question and the task_id to the agent
|
93 |
+
submitted_answer = agent(modified_question, task_id)
|
94 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
95 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
96 |
except Exception as e:
|
|
|
108 |
# 5. Submit
|
109 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
110 |
try:
|
111 |
+
response = requests.post(submit_url, json=submission_data, timeout=120) # Increased timeout
|
112 |
response.raise_for_status()
|
113 |
result_data = response.json()
|
114 |
final_status = (
|
|
|
138 |
results_df = pd.DataFrame(results_log)
|
139 |
return status_message, results_df
|
140 |
|
141 |
+
# --- Build Gradio Interface using Blocks ---
|
|
|
142 |
with gr.Blocks() as demo:
|
143 |
+
gr.Markdown("# Gemini ReAct Agent for GAIA")
|
144 |
gr.Markdown(
|
145 |
"""
|
146 |
**Instructions:**
|
147 |
+
1. Log in using the Hugging Face login button below.
|
148 |
+
2. Click 'Run Evaluation & Submit' to start the process.
|
149 |
+
3. The agent will fetch all 20 questions, reason about them step-by-step, use tools (like web search and a file reader), and submit the final answers for scoring.
|
150 |
+
|
151 |
+
**Note:** This process can take several minutes. Please be patient.
|
152 |
"""
|
153 |
)
|
|
|
154 |
gr.LoginButton()
|
155 |
|
156 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
160 |
|
161 |
run_button.click(
|
162 |
fn=run_and_submit_all,
|
|
|
163 |
outputs=[status_output, results_table]
|
164 |
)
|
165 |
|