File size: 3,281 Bytes
10e9b7d
 
eccf8e4
ed08848
d0afda5
 
fbae0df
d0afda5
 
 
ed08848
d0afda5
ed08848
 
 
 
 
d0afda5
ed08848
 
 
 
d0afda5
ed08848
d0afda5
ed08848
d0afda5
 
10e9b7d
3db6293
e80aab9
d0afda5
 
 
 
 
 
 
 
 
e80aab9
d0afda5
 
 
e80aab9
d0afda5
 
 
 
 
 
7d65c66
d0afda5
e80aab9
d0afda5
d3eacbd
d0afda5
 
7d65c66
d0afda5
 
7d65c66
d0afda5
7d65c66
d0afda5
 
 
 
 
3c4371f
d0afda5
 
d3eacbd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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