|
|
|
|
|
import os |
|
import requests |
|
import pandas as pd |
|
import gradio as gr |
|
import asyncio |
|
import tempfile |
|
from agent import answer_question, transcribe_audio, extract_excel_total_food_sales |
|
|
|
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" |
|
|
|
class GAIALlamaAgent: |
|
def __call__(self, question: str, file_path: str = None) -> str: |
|
|
|
if file_path: |
|
if "mp3" in file_path: |
|
return transcribe_audio(file_path) |
|
elif "xlsx" in file_path or "xls" in file_path: |
|
return extract_excel_total_food_sales(file_path) |
|
|
|
return asyncio.run(answer_question(question)) |
|
|
|
def run_and_submit_all(profile: gr.OAuthProfile | None): |
|
space_id = os.getenv("SPACE_ID") |
|
username = profile.username if profile else None |
|
if not username: |
|
return "Please login to Hugging Face.", 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" if space_id else "" |
|
|
|
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 |
|
|
|
agent = GAIALlamaAgent() |
|
answers_payload = [] |
|
results_log = [] |
|
|
|
for item in questions_data: |
|
qid = item.get("task_id") |
|
question = item.get("question") |
|
if not qid or not question: |
|
continue |
|
try: |
|
answer = agent(question) |
|
except Exception as e: |
|
answer = f"[AGENT ERROR] {e}" |
|
answers_payload.append({"task_id": qid, "submitted_answer": answer}) |
|
results_log.append({"Task ID": qid, "Question": question, "Submitted Answer": answer}) |
|
|
|
submission_data = { |
|
"username": username, |
|
"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() |
|
status = ( |
|
f"β
Submission Successful!\n" |
|
f"User: {result_data.get('username')}\n" |
|
f"Score: {result_data.get('score')}%\n" |
|
f"Correct: {result_data.get('correct_count')}/{result_data.get('total_attempted')}\n" |
|
f"Message: {result_data.get('message')}" |
|
) |
|
return status, pd.DataFrame(results_log) |
|
except Exception as e: |
|
return f"β Submission failed: {e}", pd.DataFrame(results_log) |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown(""" |
|
# π§ GAIA Agent Evaluation |
|
|
|
Upload your files if needed and evaluate the agent's answers. |
|
""") |
|
gr.LoginButton() |
|
|
|
with gr.Row(): |
|
question_box = gr.Textbox(label="Manual Question (optional)", lines=3) |
|
file_input = gr.File(label="Optional File (.mp3 or .xlsx)", file_types=[".mp3", ".xlsx"]) |
|
submit_btn = gr.Button("Ask Agent") |
|
|
|
output_text = gr.Textbox(label="Answer") |
|
submit_btn.click( |
|
fn=lambda q, f: GAIALlamaAgent()(q, f.name if f else None), |
|
inputs=[question_box, file_input], |
|
outputs=output_text |
|
) |
|
|
|
gr.Markdown("## Or run full benchmark submission") |
|
run_btn = gr.Button("Run Evaluation & Submit All Answers") |
|
run_out = gr.Textbox(label="Status", lines=4) |
|
run_table = gr.DataFrame(label="Questions and Agent Answers") |
|
run_btn.click(fn=run_and_submit_all, outputs=[run_out, run_table]) |
|
|
|
if __name__ == "__main__": |
|
print("\nπ App Starting Up...") |
|
if os.getenv("SPACE_ID"): |
|
print(f"π Space: https://huggingface.co/spaces/{os.getenv('SPACE_ID')}") |
|
demo.launch(debug=True) |