Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,147 +2,72 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
import subprocess
|
| 6 |
-
import sys
|
| 7 |
|
| 8 |
-
# --- START: Force
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
#
|
| 11 |
import duckduckgo_search
|
| 12 |
print("duckduckgo_search (via ddgs) is already installed.")
|
| 13 |
except ImportError:
|
| 14 |
print("duckduckgo_search not found. Attempting to install ddgs...")
|
| 15 |
try:
|
| 16 |
-
# Use ddgs as it's the updated package name
|
| 17 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "ddgs>=4.0.0"])
|
| 18 |
print("ddgs installed successfully.")
|
| 19 |
except Exception as e:
|
| 20 |
print(f"Failed to install ddgs: {e}")
|
| 21 |
-
# Critical error
|
| 22 |
raise RuntimeError(f"CRITICAL: Failed to install ddgs: {e}")
|
| 23 |
-
# --- END: Force
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
api_url = DEFAULT_API_URL
|
| 40 |
-
questions_url = f"{api_url}/questions"
|
| 41 |
-
submit_url = f"{api_url}/submit"
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
agent = GaiaAgent()
|
| 45 |
-
except Exception as e:
|
| 46 |
-
return f"Error initializing agent: {e}", None
|
| 47 |
-
|
| 48 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 49 |
-
|
| 50 |
-
try:
|
| 51 |
-
response = requests.get(questions_url, timeout=15)
|
| 52 |
-
response.raise_for_status()
|
| 53 |
-
questions_data = response.json()
|
| 54 |
-
except Exception as e:
|
| 55 |
-
return f"Error fetching questions: {e}", None
|
| 56 |
-
|
| 57 |
-
results_log = []
|
| 58 |
-
answers_payload = []
|
| 59 |
-
|
| 60 |
-
print("\n--- STARTING AGENT RUN ---")
|
| 61 |
-
for item in questions_data:
|
| 62 |
-
task_id = item.get("task_id")
|
| 63 |
-
question_text = item.get("question")
|
| 64 |
-
if not task_id or question_text is None:
|
| 65 |
-
continue
|
| 66 |
-
try:
|
| 67 |
-
final_answer, trace = agent(question_text)
|
| 68 |
-
|
| 69 |
-
print("\n--- QUESTION ---")
|
| 70 |
-
print(f"Task ID: {task_id}")
|
| 71 |
-
print(f"Question: {question_text}")
|
| 72 |
-
print("\n--- REASONING TRACE ---")
|
| 73 |
-
print(trace)
|
| 74 |
-
print("\n--- FINAL ANSWER (SUBMITTED) ---")
|
| 75 |
-
print(final_answer)
|
| 76 |
-
|
| 77 |
-
answers_payload.append({
|
| 78 |
-
"task_id": task_id,
|
| 79 |
-
"submitted_answer": final_answer,
|
| 80 |
-
"reasoning_trace": trace
|
| 81 |
-
})
|
| 82 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_answer})
|
| 83 |
-
except Exception as e:
|
| 84 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 85 |
-
|
| 86 |
-
if not answers_payload:
|
| 87 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 88 |
-
|
| 89 |
-
submission_data = {
|
| 90 |
-
"username": username.strip(),
|
| 91 |
-
"agent_code": agent_code,
|
| 92 |
-
"answers": answers_payload
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
try:
|
| 96 |
-
response = requests.post(
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
final_status = (
|
| 100 |
-
f"Submission Successful!\n"
|
| 101 |
-
f"User: {result_data.get('username')}\n"
|
| 102 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 103 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 104 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
except Exception as e:
|
| 109 |
-
return f"
|
| 110 |
-
|
| 111 |
|
|
|
|
| 112 |
with gr.Blocks() as demo:
|
| 113 |
-
gr.Markdown("# GAIA Agent
|
| 114 |
-
gr.Markdown(""
|
| 115 |
-
Logga in och kör agenten.\n
|
| 116 |
-
Du behöver INTE en OpenAI API-nyckel längre. Agenten kör en lokal modell.
|
| 117 |
-
""")
|
| 118 |
-
gr.LoginButton()
|
| 119 |
-
|
| 120 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 121 |
-
status_output = gr.Textbox(label="Submission Result")
|
| 122 |
-
results_table = gr.DataFrame(label="Answers")
|
| 123 |
-
|
| 124 |
-
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
| 125 |
-
|
| 126 |
-
if __name__ == "__main__":
|
| 127 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 128 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
| 129 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 134 |
-
else:
|
| 135 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 136 |
|
| 137 |
-
|
| 138 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 139 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 140 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 141 |
-
else:
|
| 142 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 143 |
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
import subprocess # Needed for runtime pip install
|
| 6 |
+
import sys # Needed for runtime pip install
|
| 7 |
|
| 8 |
+
# --- START: Force ddgs installation workaround ---
|
| 9 |
+
# This block ensures 'ddgs' (which provides 'duckduckgo_search') is installed
|
| 10 |
+
# early, before smolagents tries to use its DuckDuckGoSearchTool.
|
| 11 |
try:
|
| 12 |
+
# Attempt to import duckduckgo_search to check if it's already available
|
| 13 |
import duckduckgo_search
|
| 14 |
print("duckduckgo_search (via ddgs) is already installed.")
|
| 15 |
except ImportError:
|
| 16 |
print("duckduckgo_search not found. Attempting to install ddgs...")
|
| 17 |
try:
|
| 18 |
+
# Use 'ddgs' as it's the updated package name
|
| 19 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "ddgs>=4.0.0"])
|
| 20 |
print("ddgs installed successfully.")
|
| 21 |
except Exception as e:
|
| 22 |
print(f"Failed to install ddgs: {e}")
|
| 23 |
+
# Critical error: if ddgs can't be installed, the app can't function.
|
| 24 |
raise RuntimeError(f"CRITICAL: Failed to install ddgs: {e}")
|
| 25 |
+
# --- END: Force ddgs installation workaround ---
|
| 26 |
|
| 27 |
+
# Now import the agent, as its dependencies (smolagents, duckduckgo_search) should be ready
|
| 28 |
+
from agent import GaiaAgent
|
| 29 |
|
| 30 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 31 |
|
| 32 |
+
def run_agent_and_score(task_description: str) -> str:
|
| 33 |
+
# Initialize the agent within the function, so it's fresh for each run
|
| 34 |
+
# This also helps if the agent initialization is heavy or stateful
|
| 35 |
+
gaia_agent = GaiaAgent()
|
| 36 |
+
|
| 37 |
+
# Process the task
|
| 38 |
+
agent_output = gaia_agent.process_task(task_description)
|
| 39 |
+
|
| 40 |
+
# Send output to the scoring API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
+
response = requests.post(
|
| 43 |
+
f"{DEFAULT_API_URL}/score_agent",
|
| 44 |
+
json={"task_description": task_description, "agent_response": agent_output}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
+
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
|
| 47 |
+
scoring_result = response.json()
|
| 48 |
+
score_info = f"Scoring Result:\nTotal Score: {scoring_result.get('total_score')}\nCorrectness Score: {scoring_result.get('correctness_score')}\nExplanation: {scoring_result.get('explanation', 'No explanation provided.')}"
|
| 49 |
+
return f"Agent Output:\n{agent_output}\n\n---\n\n{score_info}"
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
return f"Agent Output:\n{agent_output}\n\n---\n\nError connecting to scoring API: {e}"
|
| 52 |
except Exception as e:
|
| 53 |
+
return f"Agent Output:\n{agent_output}\n\n---\n\nAn unexpected error occurred during scoring: {e}"
|
|
|
|
| 54 |
|
| 55 |
+
# Gradio Interface setup
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("# GAIA Basic Agent Evaluator (Freddolin)")
|
| 58 |
+
gr.Markdown("Enter a task description for the agent to process. The agent's output will be displayed, followed by its score from the GAIA scoring API.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
task_input = gr.Textbox(label="Task Description", placeholder="e.g., 'What is the capital of France?'")
|
| 61 |
+
output_text = gr.Textbox(label="Agent Output & Score", interactive=False)
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
run_button = gr.Button("Run Agent & Score")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
run_button.click(
|
| 66 |
+
fn=run_agent_and_score,
|
| 67 |
+
inputs=task_input,
|
| 68 |
+
outputs=output_text
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
# Launch the Gradio app
|
| 72 |
+
demo.launch(debug=True) # debug=True can provide more info in logs during development
|
| 73 |
|