|
import os |
|
import gradio as gr |
|
import requests |
|
import inspect |
|
import pandas as pd |
|
|
|
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, HfApiModel, load_tool, tool |
|
from huggingface_hub import InferenceClient |
|
import json |
|
|
|
api_url = "https://agents-course-unit4-scoring.hf.space" |
|
questions_url = f"{api_url}/questions" |
|
submit_url = f"{api_url}/submit" |
|
|
|
class BasicAgent: |
|
def __init__(self): |
|
print("BasicAgent initialized.") |
|
def __call__(self, question: str) -> str: |
|
print(f"Agent received question (first 50 chars): {question[:50]}...") |
|
|
|
fixed_answer = "This is a default answer." |
|
print(f"Agent returning fixed answer: {fixed_answer}") |
|
return fixed_answer |
|
|
|
def load_questions_from_file(filepath="questions.json"): |
|
try: |
|
with open(filepath, "r", encoding="utf-8") as f: |
|
questions_data = json.load(f) |
|
if not questions_data: |
|
print("Loaded file is empty.") |
|
return "Loaded file is empty.", None |
|
print(f"Loaded {len(questions_data)} questions from file.") |
|
return "Loaded questions successfully.", questions_data |
|
except FileNotFoundError: |
|
print("File not found. Please run the API fetch first.") |
|
return "File not found.", None |
|
except json.JSONDecodeError as e: |
|
print(f"Error decoding JSON: {e}") |
|
return f"Error decoding JSON: {e}", None |
|
except Exception as e: |
|
print(f"Unexpected error: {e}") |
|
return f"Unexpected error: {e}", None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_and_submit_one(): |
|
|
|
try: |
|
agent = BasicAgent() |
|
|
|
except Exception as e: |
|
print(f"Error instantiating agent: {e}") |
|
return f"Error initializing agent: {e}", None |
|
|
|
|
|
status_message, questions_data = load_questions_from_file() |
|
|
|
if questions_data is not None and len(questions_data) > 0: |
|
first_question = questions_data[0] |
|
print("First question object:", first_question) |
|
|
|
|
|
question_text = first_question.get("question") |
|
task_id = first_question.get("task_id") |
|
print(f"\nTask ID: {task_id}") |
|
print(f"Question: {question_text}") |
|
else: |
|
print("No data found.") |
|
|
|
|
|
results_log = [] |
|
answers_payload = [] |
|
|
|
try: |
|
submitted_answer = agent(question_text) |
|
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) |
|
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}) |
|
except Exception as e: |
|
print(f"Error running agent on task {task_id}: {e}") |
|
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}) |
|
|
|
if not answers_payload: |
|
print("Agent did not produce any answers to submit.") |
|
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log) |
|
|
|
run_and_submit_one() |
|
|
|
|