Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from smolagents import tool
|
| 2 |
|
| 3 |
# TOOL 1: Wikipedia-based Search Tool
|
|
@@ -98,9 +105,89 @@ class BasicAgent:
|
|
| 98 |
except Exception as e:
|
| 99 |
return f"Agent error: {str(e)}"
|
| 100 |
|
| 101 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if __name__ == "__main__":
|
| 103 |
-
|
| 104 |
-
while True:
|
| 105 |
-
q = input("Ask GAIA: ")
|
| 106 |
-
print(agent(q))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
#from smolagents import Agent, tool
|
| 6 |
+
from duckduckgo_search import DDGS
|
| 7 |
+
from transformers import pipeline
|
| 8 |
from smolagents import tool
|
| 9 |
|
| 10 |
# TOOL 1: Wikipedia-based Search Tool
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
return f"Agent error: {str(e)}"
|
| 107 |
|
| 108 |
+
# --- Evaluation Logic ---
|
| 109 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 110 |
+
|
| 111 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 112 |
+
space_id = os.getenv("SPACE_ID")
|
| 113 |
+
if profile:
|
| 114 |
+
username = profile.username
|
| 115 |
+
else:
|
| 116 |
+
return "Please Login to Hugging Face with the button.", None
|
| 117 |
+
|
| 118 |
+
api_url = DEFAULT_API_URL
|
| 119 |
+
questions_url = f"{api_url}/questions"
|
| 120 |
+
submit_url = f"{api_url}/submit"
|
| 121 |
+
|
| 122 |
+
try:
|
| 123 |
+
agent = BasicAgent()
|
| 124 |
+
except Exception as e:
|
| 125 |
+
return f"Error initializing agent: {e}", None
|
| 126 |
+
|
| 127 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 128 |
+
|
| 129 |
+
try:
|
| 130 |
+
response = requests.get(questions_url, timeout=15)
|
| 131 |
+
response.raise_for_status()
|
| 132 |
+
questions_data = response.json()
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return f"Error fetching questions: {e}", None
|
| 135 |
+
|
| 136 |
+
results_log = []
|
| 137 |
+
answers_payload = []
|
| 138 |
+
|
| 139 |
+
for item in questions_data:
|
| 140 |
+
task_id = item.get("task_id")
|
| 141 |
+
question_text = item.get("question")
|
| 142 |
+
if not task_id or question_text is None:
|
| 143 |
+
continue
|
| 144 |
+
try:
|
| 145 |
+
submitted_answer = agent(question_text)
|
| 146 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 147 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 148 |
+
except Exception as e:
|
| 149 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 150 |
+
|
| 151 |
+
if not answers_payload:
|
| 152 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 153 |
+
|
| 154 |
+
submission_data = {
|
| 155 |
+
"username": username.strip(),
|
| 156 |
+
"agent_code": agent_code,
|
| 157 |
+
"answers": answers_payload
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
try:
|
| 161 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 162 |
+
response.raise_for_status()
|
| 163 |
+
result_data = response.json()
|
| 164 |
+
final_status = (
|
| 165 |
+
f"Submission Successful!\n"
|
| 166 |
+
f"User: {result_data.get('username')}\n"
|
| 167 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 168 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 169 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 170 |
+
)
|
| 171 |
+
results_df = pd.DataFrame(results_log)
|
| 172 |
+
return final_status, results_df
|
| 173 |
+
except Exception as e:
|
| 174 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
| 175 |
+
|
| 176 |
+
# --- Gradio UI ---
|
| 177 |
+
with gr.Blocks() as demo:
|
| 178 |
+
gr.Markdown("# Smart Agent Evaluation Runner")
|
| 179 |
+
gr.Markdown("""
|
| 180 |
+
**Instructions:**
|
| 181 |
+
1. Login to your HF account using the button.
|
| 182 |
+
2. Click 'Run Evaluation & Submit All Answers' to test your agent.
|
| 183 |
+
""")
|
| 184 |
+
|
| 185 |
+
gr.LoginButton()
|
| 186 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 187 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 188 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 189 |
+
|
| 190 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 191 |
+
|
| 192 |
if __name__ == "__main__":
|
| 193 |
+
demo.launch()
|
|
|
|
|
|
|
|
|