Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,16 @@
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
import traceback
|
| 4 |
-
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
| 7 |
import pandas as pd
|
| 8 |
from openai import OpenAI
|
| 9 |
-
|
| 10 |
-
# Assuming these imports from smolagents are correct
|
| 11 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
| 12 |
from smolagents.models import OpenAIServerModel
|
| 13 |
|
| 14 |
-
# --- Basic Logging Setup ---
|
| 15 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
-
# --- Configuration ---
|
| 19 |
SUBMISSION_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 21 |
if not GITHUB_TOKEN:
|
|
@@ -23,7 +18,6 @@ if not GITHUB_TOKEN:
|
|
| 23 |
GITHUB_ENDPOINT = "https://models.github.ai/inference"
|
| 24 |
MODEL_ID = os.getenv("MODEL_ID", "openai/gpt-4o-mini")
|
| 25 |
|
| 26 |
-
# --- Tool Definitions ---
|
| 27 |
try:
|
| 28 |
search_tool_instance = DuckDuckGoSearchTool()
|
| 29 |
logger.info("DuckDuckGoSearchTool initialized successfully.")
|
|
@@ -31,7 +25,6 @@ except Exception as e:
|
|
| 31 |
logger.error(f"Failed to instantiate DuckDuckGoSearchTool: {e}. Web search will not work.")
|
| 32 |
search_tool_instance = None
|
| 33 |
|
| 34 |
-
# *** FIX: Added Args description to docstrings ***
|
| 35 |
@tool
|
| 36 |
def web_search(query: str) -> str:
|
| 37 |
"""
|
|
@@ -52,7 +45,6 @@ def web_search(query: str) -> str:
|
|
| 52 |
logger.exception(f"web_search failed for query: {query}")
|
| 53 |
return f"Search Error: {e}"
|
| 54 |
|
| 55 |
-
# *** FIX: Added Args description to docstrings ***
|
| 56 |
@tool
|
| 57 |
def wikipedia_lookup(page_title: str) -> str:
|
| 58 |
"""
|
|
@@ -93,7 +85,6 @@ def wikipedia_lookup(page_title: str) -> str:
|
|
| 93 |
logger.exception(f"wikipedia_lookup failed for page: {page_safe}")
|
| 94 |
return f"Wikipedia Error: Unexpected error: {e}"
|
| 95 |
|
| 96 |
-
# --- The ReACT Prompt (ensure this is the *only* main prompt definition) ---
|
| 97 |
REACT_INSTRUCTION_PROMPT = """You are a helpful assistant using tools to answer questions.
|
| 98 |
|
| 99 |
Available Tools:
|
|
@@ -117,24 +108,24 @@ Formatting Rules for FINAL ANSWER:
|
|
| 117 |
Let's begin!
|
| 118 |
"""
|
| 119 |
|
| 120 |
-
# --- SmolAgent Setup ---
|
| 121 |
logger.info(f"Initializing LLM connection: {MODEL_ID} @ {GITHUB_ENDPOINT}")
|
| 122 |
try:
|
|
|
|
| 123 |
llm_model = OpenAIServerModel(
|
| 124 |
model_id=MODEL_ID,
|
| 125 |
api_key=GITHUB_TOKEN,
|
| 126 |
-
|
| 127 |
request_timeout=60
|
| 128 |
)
|
| 129 |
-
logger.info("LLM connection
|
| 130 |
except Exception as e:
|
| 131 |
-
logger.exception("CRITICAL: Failed to configure OpenAIServerModel")
|
| 132 |
-
raise RuntimeError(f"Could not configure SmolAgents model: {e}") from e
|
| 133 |
|
| 134 |
logger.info("Initializing CodeAgent...")
|
| 135 |
try:
|
| 136 |
agent = CodeAgent(
|
| 137 |
-
tools=[web_search, wikipedia_lookup],
|
| 138 |
model=llm_model
|
| 139 |
)
|
| 140 |
logger.info("CodeAgent initialized OK.")
|
|
@@ -142,17 +133,13 @@ except Exception as e:
|
|
| 142 |
logger.exception("CRITICAL: Failed to initialize CodeAgent")
|
| 143 |
raise RuntimeError(f"Could not initialize CodeAgent: {e}") from e
|
| 144 |
|
| 145 |
-
# --- Agent Execution Function ---
|
| 146 |
def run_agent_on_question(question: str) -> str:
|
| 147 |
-
"""Runs the agent with the CORRECT prompt."""
|
| 148 |
question = question.strip()
|
| 149 |
if not question: return "AGENT_ERROR: Question cannot be empty."
|
| 150 |
|
| 151 |
-
# *** CRITICAL: Construct the prompt HERE using the correct variable ***
|
| 152 |
full_prompt = REACT_INSTRUCTION_PROMPT.strip() + "\n\nQUESTION: " + question
|
| 153 |
logger.info(f"--- Running Agent for Question: '{question}' ---")
|
| 154 |
-
|
| 155 |
-
logger.info(f"CRITICAL_DEBUG: Using prompt beginning:\n{full_prompt[:400]}\n...") # Log first 400 chars
|
| 156 |
|
| 157 |
try:
|
| 158 |
raw_result = agent.run(full_prompt)
|
|
@@ -162,16 +149,12 @@ def run_agent_on_question(question: str) -> str:
|
|
| 162 |
logger.exception(f"Agent run failed for question '{question}'")
|
| 163 |
return f"AGENT_ERROR: Exception during run: {e}\n{traceback.format_exc()}"
|
| 164 |
|
| 165 |
-
# --- Gradio Interface & Submission Logic ---
|
| 166 |
-
# Using the version without direct profile input to avoid potential TypeErrors
|
| 167 |
def evaluate_and_submit():
|
| 168 |
-
"""Gradio action: Fetches questions, runs agent, submits results."""
|
| 169 |
logger.info("🚀 Starting evaluation run...")
|
| 170 |
-
username = os.getenv("HF_USERNAME", "unknown_user")
|
| 171 |
if username == "unknown_user": logger.warning("Could not get HF username reliably.")
|
| 172 |
logger.info(f"Running as user (best effort): {username}")
|
| 173 |
|
| 174 |
-
# 1. Fetch Questions
|
| 175 |
try:
|
| 176 |
resp = requests.get(f"{SUBMISSION_URL}/questions", timeout=20)
|
| 177 |
resp.raise_for_status()
|
|
@@ -184,7 +167,6 @@ def evaluate_and_submit():
|
|
| 184 |
|
| 185 |
if not questions: return "ℹ️ No questions fetched.", pd.DataFrame()
|
| 186 |
|
| 187 |
-
# 2. Run Agent & Collect Results
|
| 188 |
results_log = []
|
| 189 |
answers_payload = []
|
| 190 |
for i, item in enumerate(questions):
|
|
@@ -202,7 +184,6 @@ def evaluate_and_submit():
|
|
| 202 |
results_df = pd.DataFrame(results_log)
|
| 203 |
if not answers_payload: return "⚠️ Agent ran but produced no answers.", results_df
|
| 204 |
|
| 205 |
-
# 3. Submit Answers
|
| 206 |
logger.info(f"Submitting {len(answers_payload)} answers...")
|
| 207 |
space_id = os.getenv("SPACE_ID", "NA"); agent_code_url = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id != "NA" else "NA"
|
| 208 |
submit_data = {"username": username, "agent_code": agent_code_url, "answers": answers_payload}
|
|
@@ -219,7 +200,6 @@ def evaluate_and_submit():
|
|
| 219 |
if hasattr(e, 'response') and e.response is not None: err_msg += f" | Response: {e.response.text[:300]}"
|
| 220 |
return err_msg, results_df
|
| 221 |
|
| 222 |
-
# --- Build Gradio App ---
|
| 223 |
logger.info("Setting up Gradio interface...")
|
| 224 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 225 |
gr.Markdown("# 🚀 Agent Evaluation Runner 🚀\nEnsure `GITHUB_TOKEN` secret is set. Click Run to start.")
|
|
@@ -229,9 +209,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 229 |
run_button.click(fn=evaluate_and_submit, inputs=None, outputs=[status_textbox, results_df_display])
|
| 230 |
logger.info("Gradio interface setup complete.")
|
| 231 |
|
| 232 |
-
# --- Launch ---
|
| 233 |
if __name__ == "__main__":
|
| 234 |
logger.info("Launching Gradio application...")
|
| 235 |
-
# Setting share=False as recommended for HF Spaces, debug=True for detailed Gradio logs
|
| 236 |
demo.launch(debug=True, share=False)
|
| 237 |
logger.info("Gradio application launched.")
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
import traceback
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
import pandas as pd
|
| 7 |
from openai import OpenAI
|
|
|
|
|
|
|
| 8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, tool
|
| 9 |
from smolagents.models import OpenAIServerModel
|
| 10 |
|
|
|
|
| 11 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
|
|
|
| 14 |
SUBMISSION_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
|
| 16 |
if not GITHUB_TOKEN:
|
|
|
|
| 18 |
GITHUB_ENDPOINT = "https://models.github.ai/inference"
|
| 19 |
MODEL_ID = os.getenv("MODEL_ID", "openai/gpt-4o-mini")
|
| 20 |
|
|
|
|
| 21 |
try:
|
| 22 |
search_tool_instance = DuckDuckGoSearchTool()
|
| 23 |
logger.info("DuckDuckGoSearchTool initialized successfully.")
|
|
|
|
| 25 |
logger.error(f"Failed to instantiate DuckDuckGoSearchTool: {e}. Web search will not work.")
|
| 26 |
search_tool_instance = None
|
| 27 |
|
|
|
|
| 28 |
@tool
|
| 29 |
def web_search(query: str) -> str:
|
| 30 |
"""
|
|
|
|
| 45 |
logger.exception(f"web_search failed for query: {query}")
|
| 46 |
return f"Search Error: {e}"
|
| 47 |
|
|
|
|
| 48 |
@tool
|
| 49 |
def wikipedia_lookup(page_title: str) -> str:
|
| 50 |
"""
|
|
|
|
| 85 |
logger.exception(f"wikipedia_lookup failed for page: {page_safe}")
|
| 86 |
return f"Wikipedia Error: Unexpected error: {e}"
|
| 87 |
|
|
|
|
| 88 |
REACT_INSTRUCTION_PROMPT = """You are a helpful assistant using tools to answer questions.
|
| 89 |
|
| 90 |
Available Tools:
|
|
|
|
| 108 |
Let's begin!
|
| 109 |
"""
|
| 110 |
|
|
|
|
| 111 |
logger.info(f"Initializing LLM connection: {MODEL_ID} @ {GITHUB_ENDPOINT}")
|
| 112 |
try:
|
| 113 |
+
logger.info("Attempting to configure OpenAIServerModel with 'api_base' instead of 'base_url'...")
|
| 114 |
llm_model = OpenAIServerModel(
|
| 115 |
model_id=MODEL_ID,
|
| 116 |
api_key=GITHUB_TOKEN,
|
| 117 |
+
api_base=GITHUB_ENDPOINT,
|
| 118 |
request_timeout=60
|
| 119 |
)
|
| 120 |
+
logger.info("LLM connection configured using 'api_base'.")
|
| 121 |
except Exception as e:
|
| 122 |
+
logger.exception("CRITICAL: Failed to configure OpenAIServerModel (tried with api_base)")
|
| 123 |
+
raise RuntimeError(f"Could not configure SmolAgents model using api_base: {e}") from e
|
| 124 |
|
| 125 |
logger.info("Initializing CodeAgent...")
|
| 126 |
try:
|
| 127 |
agent = CodeAgent(
|
| 128 |
+
tools=[web_search, wikipedia_lookup],
|
| 129 |
model=llm_model
|
| 130 |
)
|
| 131 |
logger.info("CodeAgent initialized OK.")
|
|
|
|
| 133 |
logger.exception("CRITICAL: Failed to initialize CodeAgent")
|
| 134 |
raise RuntimeError(f"Could not initialize CodeAgent: {e}") from e
|
| 135 |
|
|
|
|
| 136 |
def run_agent_on_question(question: str) -> str:
|
|
|
|
| 137 |
question = question.strip()
|
| 138 |
if not question: return "AGENT_ERROR: Question cannot be empty."
|
| 139 |
|
|
|
|
| 140 |
full_prompt = REACT_INSTRUCTION_PROMPT.strip() + "\n\nQUESTION: " + question
|
| 141 |
logger.info(f"--- Running Agent for Question: '{question}' ---")
|
| 142 |
+
logger.info(f"CRITICAL_DEBUG: Using prompt beginning:\n{full_prompt[:400]}\n...")
|
|
|
|
| 143 |
|
| 144 |
try:
|
| 145 |
raw_result = agent.run(full_prompt)
|
|
|
|
| 149 |
logger.exception(f"Agent run failed for question '{question}'")
|
| 150 |
return f"AGENT_ERROR: Exception during run: {e}\n{traceback.format_exc()}"
|
| 151 |
|
|
|
|
|
|
|
| 152 |
def evaluate_and_submit():
|
|
|
|
| 153 |
logger.info("🚀 Starting evaluation run...")
|
| 154 |
+
username = os.getenv("HF_USERNAME", "unknown_user")
|
| 155 |
if username == "unknown_user": logger.warning("Could not get HF username reliably.")
|
| 156 |
logger.info(f"Running as user (best effort): {username}")
|
| 157 |
|
|
|
|
| 158 |
try:
|
| 159 |
resp = requests.get(f"{SUBMISSION_URL}/questions", timeout=20)
|
| 160 |
resp.raise_for_status()
|
|
|
|
| 167 |
|
| 168 |
if not questions: return "ℹ️ No questions fetched.", pd.DataFrame()
|
| 169 |
|
|
|
|
| 170 |
results_log = []
|
| 171 |
answers_payload = []
|
| 172 |
for i, item in enumerate(questions):
|
|
|
|
| 184 |
results_df = pd.DataFrame(results_log)
|
| 185 |
if not answers_payload: return "⚠️ Agent ran but produced no answers.", results_df
|
| 186 |
|
|
|
|
| 187 |
logger.info(f"Submitting {len(answers_payload)} answers...")
|
| 188 |
space_id = os.getenv("SPACE_ID", "NA"); agent_code_url = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id != "NA" else "NA"
|
| 189 |
submit_data = {"username": username, "agent_code": agent_code_url, "answers": answers_payload}
|
|
|
|
| 200 |
if hasattr(e, 'response') and e.response is not None: err_msg += f" | Response: {e.response.text[:300]}"
|
| 201 |
return err_msg, results_df
|
| 202 |
|
|
|
|
| 203 |
logger.info("Setting up Gradio interface...")
|
| 204 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 205 |
gr.Markdown("# 🚀 Agent Evaluation Runner 🚀\nEnsure `GITHUB_TOKEN` secret is set. Click Run to start.")
|
|
|
|
| 209 |
run_button.click(fn=evaluate_and_submit, inputs=None, outputs=[status_textbox, results_df_display])
|
| 210 |
logger.info("Gradio interface setup complete.")
|
| 211 |
|
|
|
|
| 212 |
if __name__ == "__main__":
|
| 213 |
logger.info("Launching Gradio application...")
|
|
|
|
| 214 |
demo.launch(debug=True, share=False)
|
| 215 |
logger.info("Gradio application launched.")
|