Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import requests | |
import pandas as pd | |
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool | |
from smolagents import OpenAIServerModel | |
# Constants | |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
import os | |
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool, VisitWebpageTool | |
class GAIAAgent: | |
def __init__(self): | |
api_key = os.getenv("NEBIUS_API_KEY") | |
if not api_key: | |
raise ValueError("NEBIUS_API_KEY environment variable is not set. Please set this to your Hugging Face API key.") | |
# self.model = InferenceClientModel( | |
# model_id="deepseek-ai/DeepSeek-R1", | |
# api_key=api_key | |
# ) | |
# from smolagents import InferenceClientModel | |
self.model = InferenceClientModel( | |
model_id="Qwen/Qwen3-235B-A22B", | |
provider="nebius") | |
# self.model = OpenAIServerModel( | |
# model_id="Qwen/Qwen3-235B-A22B", | |
# api_base="https://api.studio.nebius.com/", # Leave this blank to query OpenAI servers. | |
# api_key=api_key, # Switch to the API key for the server you're targeting. | |
# ) | |
self.tools = [DuckDuckGoSearchTool(), VisitWebpageTool()] | |
self.agent = CodeAgent(tools=self.tools, model=self.model, max_steps=10) | |
def __call__(self, question: str, file_path: str = None) -> str: | |
try: | |
if file_path: | |
question += f"\nAttached file: {file_path}" | |
response = self.agent.run(question) | |
return response | |
except Exception as e: | |
print(f"Error running agent: {e}") | |
return f"AGENT ERROR: {e}" | |
def run_and_submit_all(profile: gr.OAuthProfile | None): | |
if not profile: | |
return "Please Login to Hugging Face with the button.", None | |
username = profile.username | |
api_url = DEFAULT_API_URL | |
questions_url = f"{api_url}/questions" | |
submit_url = f"{api_url}/submit" | |
agent = GAIAAgent() | |
agent_code = f"https://huggingface.co/spaces/{os.getenv('ArunKr/Assignment_Agents')}/tree/main" | |
response = requests.get(questions_url, timeout=15) | |
questions_data = response.json() | |
results_log = [] | |
answers_payload = [] | |
for item in questions_data: | |
task_id = item.get("task_id") | |
question_text = item.get("question") | |
file_name = item.get("file_name", None) | |
submitted_answer = agent(question_text, file_name) | |
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}) | |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload} | |
try: | |
submission_response = requests.post(submit_url, json=submission_data, timeout=60) | |
result_data = submission_response.json() | |
final_status = ( | |
f"Submission Successful!\n" | |
f"User: {result_data.get('username')}\n" | |
f"Overall Score: {result_data.get('score', 'N/A')}% " | |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n" | |
f"Message: {result_data.get('message', 'No message received.')}" | |
) | |
results_df = pd.DataFrame(results_log) | |
return final_status, results_df | |
except Exception as e: | |
status_message = f"Submission Failed: {e}" | |
results_df = pd.DataFrame(results_log) | |
return status_message, results_df | |
# Gradio Interface | |
with gr.Blocks() as demo: | |
gr.Markdown("# GAIA Benchmark Evaluation Runner") | |
gr.LoginButton() | |
run_button = gr.Button("Run Evaluation & Submit All Answers") | |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False) | |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True) | |
run_button.click( | |
fn=run_and_submit_all, | |
inputs=None, | |
outputs=[status_output, results_table] | |
) | |
if __name__ == "__main__": | |
demo.launch(debug=True, share=False) | |