Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,10 +27,12 @@ except ImportError:
|
|
| 27 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 28 |
|
| 29 |
# --- Basic Agent Definition ---
|
|
|
|
| 30 |
class BasicAgent:
|
| 31 |
def __init__(self):
|
| 32 |
print("Initializing Advanced Agent logic...")
|
| 33 |
try:
|
|
|
|
| 34 |
self.llm = AzureChatOpenAI(
|
| 35 |
azure_endpoint="https://dsap.openai.azure.com/",
|
| 36 |
api_key=os.environ["AZURE_API_KEY"],
|
|
@@ -41,10 +43,11 @@ class BasicAgent:
|
|
| 41 |
)
|
| 42 |
print("AzureChatOpenAI client initialized successfully.")
|
| 43 |
except KeyError:
|
| 44 |
-
raise KeyError("CRITICAL: Missing 'AZURE_API_KEY'. Please add it to your Space
|
| 45 |
except Exception as e:
|
| 46 |
print(f"FAILED to initialize Azure client. Error: {e}")
|
| 47 |
raise
|
|
|
|
| 48 |
self.tools = {
|
| 49 |
"search": self.search, "browse": self.browse,
|
| 50 |
"python": self.python_interpreter, "get_youtube_transcript": self.get_youtube_transcript,
|
|
@@ -145,47 +148,58 @@ Thought:
|
|
| 145 |
return llm_response
|
| 146 |
return "Agent failed to find an answer within the loop limit."
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 151 |
space_id = os.getenv("SPACE_ID")
|
| 152 |
|
| 153 |
-
if profile
|
| 154 |
-
username
|
| 155 |
print(f"User logged in: {username}")
|
| 156 |
else:
|
| 157 |
-
|
| 158 |
-
return "
|
| 159 |
|
| 160 |
-
api_url
|
| 161 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 162 |
|
| 163 |
-
try:
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "
|
|
|
|
| 167 |
|
| 168 |
try:
|
| 169 |
response = requests.get(questions_url, timeout=20)
|
| 170 |
response.raise_for_status()
|
| 171 |
questions_data = response.json()
|
| 172 |
-
if not questions_data:
|
|
|
|
| 173 |
print(f"Fetched {len(questions_data)} questions.")
|
| 174 |
-
except Exception as e:
|
|
|
|
| 175 |
|
| 176 |
results_log, answers_payload = [], []
|
|
|
|
| 177 |
for item in questions_data:
|
| 178 |
-
task_id
|
| 179 |
-
|
|
|
|
|
|
|
| 180 |
try:
|
|
|
|
|
|
|
| 181 |
submitted_answer = agent(item)
|
| 182 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 183 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 184 |
-
except Exception:
|
| 185 |
-
|
| 186 |
-
|
| 187 |
|
| 188 |
-
if not answers_payload:
|
|
|
|
| 189 |
|
| 190 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 191 |
print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
|
|
@@ -193,40 +207,43 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 193 |
try:
|
| 194 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 195 |
response.raise_for_status()
|
| 196 |
-
|
| 197 |
-
final_status = (
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
| 200 |
return final_status, pd.DataFrame(results_log)
|
| 201 |
-
except Exception as e:
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
#
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
| 225 |
|
| 226 |
-
#
|
| 227 |
run_button.click(
|
| 228 |
fn=run_and_submit_all,
|
| 229 |
-
inputs=[
|
| 230 |
outputs=[status_output, results_table]
|
| 231 |
)
|
| 232 |
|
|
|
|
| 27 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 28 |
|
| 29 |
# --- Basic Agent Definition ---
|
| 30 |
+
# ----- THIS IS WHERE THE ADVANCED AGENT LOGIC IS BUILT -----
|
| 31 |
class BasicAgent:
|
| 32 |
def __init__(self):
|
| 33 |
print("Initializing Advanced Agent logic...")
|
| 34 |
try:
|
| 35 |
+
# Using hardcoded values for endpoint/deployment and loading the key from secrets
|
| 36 |
self.llm = AzureChatOpenAI(
|
| 37 |
azure_endpoint="https://dsap.openai.azure.com/",
|
| 38 |
api_key=os.environ["AZURE_API_KEY"],
|
|
|
|
| 43 |
)
|
| 44 |
print("AzureChatOpenAI client initialized successfully.")
|
| 45 |
except KeyError:
|
| 46 |
+
raise KeyError("CRITICAL: Missing 'AZURE_API_KEY' secret. Please add it to your Space settings.")
|
| 47 |
except Exception as e:
|
| 48 |
print(f"FAILED to initialize Azure client. Error: {e}")
|
| 49 |
raise
|
| 50 |
+
|
| 51 |
self.tools = {
|
| 52 |
"search": self.search, "browse": self.browse,
|
| 53 |
"python": self.python_interpreter, "get_youtube_transcript": self.get_youtube_transcript,
|
|
|
|
| 148 |
return llm_response
|
| 149 |
return "Agent failed to find an answer within the loop limit."
|
| 150 |
|
| 151 |
+
# Using your original submission function structure
|
| 152 |
+
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 153 |
space_id = os.getenv("SPACE_ID")
|
| 154 |
|
| 155 |
+
if profile:
|
| 156 |
+
username= f"{profile.username}"
|
| 157 |
print(f"User logged in: {username}")
|
| 158 |
else:
|
| 159 |
+
print("User not logged in.")
|
| 160 |
+
return "Please Login to Hugging Face with the button.", None
|
| 161 |
|
| 162 |
+
api_url = DEFAULT_API_URL
|
| 163 |
questions_url = f"{api_url}/questions"
|
| 164 |
+
submit_url = f"{api_url}/submit"
|
| 165 |
|
| 166 |
+
try:
|
| 167 |
+
agent = BasicAgent()
|
| 168 |
+
except Exception as e:
|
| 169 |
+
return f"Error initializing agent: {e}", None
|
| 170 |
|
| 171 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
| 172 |
+
print(f"Agent code URL: {agent_code}")
|
| 173 |
|
| 174 |
try:
|
| 175 |
response = requests.get(questions_url, timeout=20)
|
| 176 |
response.raise_for_status()
|
| 177 |
questions_data = response.json()
|
| 178 |
+
if not questions_data:
|
| 179 |
+
return "Fetched questions list is empty.", None
|
| 180 |
print(f"Fetched {len(questions_data)} questions.")
|
| 181 |
+
except Exception as e:
|
| 182 |
+
return f"Error fetching questions: {e}", None
|
| 183 |
|
| 184 |
results_log, answers_payload = [], []
|
| 185 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
| 186 |
for item in questions_data:
|
| 187 |
+
task_id = item.get("task_id")
|
| 188 |
+
question_text = item.get("question")
|
| 189 |
+
if not task_id or question_text is None:
|
| 190 |
+
continue
|
| 191 |
try:
|
| 192 |
+
# *** The only required change to your original function ***
|
| 193 |
+
# We pass the whole 'item' so the agent can see file URLs.
|
| 194 |
submitted_answer = agent(item)
|
| 195 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 196 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 197 |
+
except Exception as e:
|
| 198 |
+
error_msg = f"AGENT ERROR: {traceback.format_exc()}"
|
| 199 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_msg})
|
| 200 |
|
| 201 |
+
if not answers_payload:
|
| 202 |
+
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
| 203 |
|
| 204 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 205 |
print(f"Submitting {len(answers_payload)} answers for user '{username}'...")
|
|
|
|
| 207 |
try:
|
| 208 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 209 |
response.raise_for_status()
|
| 210 |
+
result__data = response.json()
|
| 211 |
+
final_status = (
|
| 212 |
+
f"Submission Successful!\nUser: {result_data.get('username')}\n"
|
| 213 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 214 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)"
|
| 215 |
+
)
|
| 216 |
return final_status, pd.DataFrame(results_log)
|
| 217 |
+
except Exception as e:
|
| 218 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# --- Build Gradio Interface using Blocks ---
|
| 222 |
+
# Using the corrected wiring for the Gradio interface
|
| 223 |
+
with gr.Blocks() as demo:
|
| 224 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 225 |
+
gr.Markdown(
|
| 226 |
+
"""
|
| 227 |
+
**Instructions:**
|
| 228 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 229 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 230 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 231 |
+
---
|
| 232 |
+
**Disclaimers:**
|
| 233 |
+
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).
|
| 234 |
+
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution.
|
| 235 |
+
"""
|
| 236 |
+
)
|
| 237 |
+
# The fix is to give the login button a variable name...
|
| 238 |
+
login_button = gr.LoginButton()
|
| 239 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 240 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 241 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 242 |
|
| 243 |
+
# ...and pass it as an input to the click event. This is the standard, working method.
|
| 244 |
run_button.click(
|
| 245 |
fn=run_and_submit_all,
|
| 246 |
+
inputs=[login_button],
|
| 247 |
outputs=[status_output, results_table]
|
| 248 |
)
|
| 249 |
|