Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import requests | |
import pandas as pd | |
import subprocess # Needed for runtime pip install | |
import sys # Needed for runtime pip install | |
# --- START: Force ddgs installation workaround --- | |
# This block ensures 'ddgs' (which provides 'duckduckgo_search') is installed | |
# early, before smolagents tries to use its DuckDuckGoSearchTool. | |
try: | |
# Attempt to import duckduckgo_search to check if it's already available | |
import duckduckgo_search | |
print("duckduckgo_search (via ddgs) is already installed.") | |
except ImportError: | |
print("duckduckgo_search not found. Attempting to install ddgs...") | |
try: | |
# Use 'ddgs' as it's the updated package name | |
subprocess.check_call([sys.executable, "-m", "pip", "install", "ddgs>=4.0.0"]) | |
print("ddgs installed successfully.") | |
except Exception as e: | |
print(f"Failed to install ddgs: {e}") | |
# Critical error: if ddgs can't be installed, the app can't function. | |
raise RuntimeError(f"CRITICAL: Failed to install ddgs: {e}") | |
# --- END: Force ddgs installation workaround --- | |
# Now import the agent, as its dependencies (smolagents, duckduckgo_search) should be ready | |
from agent import GaiaAgent | |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
def run_agent_and_score(task_description: str) -> str: | |
# Initialize the agent within the function, so it's fresh for each run | |
# This also helps if the agent initialization is heavy or stateful | |
gaia_agent = GaiaAgent() | |
# Process the task | |
agent_output = gaia_agent.process_task(task_description) | |
# Send output to the scoring API | |
try: | |
response = requests.post( | |
f"{DEFAULT_API_URL}/score_agent", | |
json={"task_description": task_description, "agent_response": agent_output} | |
) | |
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx) | |
scoring_result = response.json() | |
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.')}" | |
return f"Agent Output:\n{agent_output}\n\n---\n\n{score_info}" | |
except requests.exceptions.RequestException as e: | |
return f"Agent Output:\n{agent_output}\n\n---\n\nError connecting to scoring API: {e}" | |
except Exception as e: | |
return f"Agent Output:\n{agent_output}\n\n---\n\nAn unexpected error occurred during scoring: {e}" | |
# Gradio Interface setup | |
with gr.Blocks() as demo: | |
gr.Markdown("# GAIA Basic Agent Evaluator (Freddolin)") | |
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.") | |
task_input = gr.Textbox(label="Task Description", placeholder="e.g., 'What is the capital of France?'") | |
output_text = gr.Textbox(label="Agent Output & Score", interactive=False) | |
run_button = gr.Button("Run Agent & Score") | |
run_button.click( | |
fn=run_agent_and_score, | |
inputs=task_input, | |
outputs=output_text | |
) | |
# Launch the Gradio app | |
demo.launch(debug=True) # debug=True can provide more info in logs during development | |