lea-zukerman's picture
Update app.py
45e678e verified
Raw
History Blame Contribute Delete
2.91 kB
import os
import gradio as gr
import requests
import pandas as pd
from smolagents import CodeAgent, Model
# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
# ื‘ืžืงื•ื HfApiModel, ื ืฉืชืžืฉ ื‘-Model ืขื ืฉื ื”ืžื•ื“ืœ
model = Model.from_pretrained("Qwen/Qwen2.5-Coder-32B-Instruct")
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
# --- Agent Definition ---
# ... (ืฉืืจ ื”ืงื•ื“ ืฉืœ run_and_submit_all ื•ื”ืžืžืฉืง ื ืฉืืจื™ื ืื•ืชื• ื“ื‘ืจ)
def run_and_submit_all(profile: gr.OAuthProfile | None):
# 1. ืื™ืžื•ืช ืžืฉืชืžืฉ
space_id = os.getenv("SPACE_ID")
if profile:
username = f"{profile.username}"
else:
return "Please Login to Hugging Face with the button.", None
api_url = DEFAULT_API_URL
questions_url = f"{api_url}/questions"
submit_url = f"{api_url}/submit"
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
# 2. ืžืฉื™ื›ืช ื”ืฉืืœื•ืช
try:
response = requests.get(questions_url, timeout=15)
response.raise_for_status()
questions_data = response.json()
except Exception as e:
return f"Error fetching questions: {e}", None
# 3. ื”ืจืฆืช ื”ืกื•ื›ืŸ ืขืœ ื”ืฉืืœื•ืช
results_log = []
answers_payload = []
for item in questions_data:
task_id = item.get("task_id")
question_text = item.get("question")
# ื”ืจืฆืช ื”ืกื•ื›ืŸ ื”ื—ื›ื
submitted_answer = agent.run(question_text)
answers_payload.append({"task_id": task_id, "submitted_answer": str(submitted_answer)})
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": str(submitted_answer)})
# 4. ื”ื’ืฉืช ื”ืชืฉื•ื‘ื•ืช
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
try:
response = requests.post(submit_url, json=submission_data, timeout=60)
response.raise_for_status()
result_data = response.json()
final_status = (
f"Submission Successful!\n"
f"Overall Score: {result_data.get('score', 'N/A')}% "
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)"
)
return final_status, pd.DataFrame(results_log)
except Exception as e:
return f"Submission Failed: {e}", pd.DataFrame(results_log)
# --- ืžืžืฉืง ื”ืžืฉืชืžืฉ (Gradio) ---
with gr.Blocks() as demo:
gr.Markdown("# Advanced Agent Evaluation Runner")
gr.LoginButton()
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Status", lines=5)
results_table = gr.DataFrame(label="Results")
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
if __name__ == "__main__":
demo.launch()