File size: 4,548 Bytes
b6fa5fc
 
10e9b7d
b6fa5fc
10e9b7d
3c4371f
10e9b7d
b6fa5fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e80aab9
b6fa5fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ee0419
b6fa5fc
 
e80aab9
 
b6fa5fc
 
 
e80aab9
b6fa5fc
 
 
 
e80aab9
b6fa5fc
 
e80aab9
b6fa5fc
 
 
 
 
 
 
 
 
 
e80aab9
b6fa5fc
 
 
 
 
 
 
 
 
 
 
e80aab9
 
b6fa5fc
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from __future__ import annotations

import os

import gradio as gr
import pandas as pd

from gaia_workbench import (
    API_URL,
    answer_payload,
    download_files,
    fetch_questions,
    load_answers,
    load_questions,
    status_rows,
    submit_answers,
    write_answer,
)


def refresh_questions() -> tuple[str, pd.DataFrame]:
    questions = fetch_questions()
    return f"Fetched {len(questions)} questions from {API_URL}", pd.DataFrame(status_rows())


def refresh_status() -> pd.DataFrame:
    return pd.DataFrame(status_rows())


def save_answer(task_id: str, answer: str) -> tuple[str, pd.DataFrame]:
    if not task_id.strip():
        return "Missing task_id", refresh_status()
    if not answer.strip():
        return "Missing answer", refresh_status()
    write_answer(task_id.strip(), answer.strip())
    return f"Saved answer for {task_id.strip()}", refresh_status()


def download_attached_files() -> str:
    downloaded, failed = download_files()
    parts = []
    if downloaded:
        parts.append("Downloaded:\n" + "\n".join(str(path) for path in downloaded))
    if failed:
        parts.append("Failed:\n" + "\n".join(failed))
    return "\n\n".join(parts) if parts else "No attached files found."


def _default_agent_code() -> str:
    space_id = os.getenv("SPACE_ID", "").strip()
    return f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""


def _submit_with(username: str, agent_code: str) -> tuple[str, pd.DataFrame]:
    agent_code = agent_code.strip() or _default_agent_code()

    try:
        result = submit_answers(username=username, agent_code=agent_code)
    except Exception as exc:
        return f"Submission failed: {exc}", refresh_status()

    return (
        "Submission successful\n"
        f"User: {result.get('username')}\n"
        f"Score: {result.get('score')}%\n"
        f"Correct: {result.get('correct_count')}/{result.get('total_attempted')}\n"
        f"Message: {result.get('message')}",
        refresh_status(),
    )


def submit_logged_in(profile: gr.OAuthProfile | None) -> tuple[str, pd.DataFrame]:
    if not profile:
        return "Please login with Hugging Face first, or use manual submit.", refresh_status()
    return _submit_with(profile.username, _default_agent_code())


def submit_manual(manual_username: str, manual_agent_code: str) -> tuple[str, pd.DataFrame]:
    return _submit_with(manual_username.strip(), manual_agent_code.strip())


with gr.Blocks(title="GAIA Unit 4 Workbench") as demo:
    gr.Markdown("# GAIA Unit 4 Workbench")
    gr.Markdown(
        "Fetch the course questions, fill exact-match answers, then submit. "
        "On Hugging Face Spaces, login with the button before submission."
    )

    is_space_runtime = bool(os.getenv("SPACE_ID", "").strip())
    if is_space_runtime:
        gr.LoginButton()

    with gr.Row():
        fetch_button = gr.Button("Fetch Questions")
        download_button = gr.Button("Download Attached Files")
        refresh_button = gr.Button("Refresh Status")

    status = gr.Textbox(label="Status", lines=5)
    table = gr.DataFrame(label="Questions / Answers", wrap=True, value=pd.DataFrame(status_rows()))

    with gr.Row():
        task_id_input = gr.Textbox(label="Task ID")
        answer_input = gr.Textbox(label="Exact Answer")
    save_button = gr.Button("Save Answer")

    gr.Markdown("## Submit")
    manual_username = gr.Textbox(label="HF Username fallback", placeholder="Used only when not logged in via HF OAuth")
    manual_agent_code = gr.Textbox(
        label="Agent Code URL fallback",
        placeholder="https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE/tree/main",
    )
    with gr.Row():
        submit_login_button = gr.Button("Submit With HF Login", visible=is_space_runtime)
        submit_manual_button = gr.Button("Submit Manual")

    fetch_button.click(fn=refresh_questions, outputs=[status, table])
    download_button.click(fn=download_attached_files, outputs=status)
    refresh_button.click(fn=refresh_status, outputs=table)
    save_button.click(fn=save_answer, inputs=[task_id_input, answer_input], outputs=[status, table])
    submit_login_button.click(fn=submit_logged_in, outputs=[status, table])
    submit_manual_button.click(fn=submit_manual, inputs=[manual_username, manual_agent_code], outputs=[status, table])


if __name__ == "__main__":
    print(f"Loaded {len(load_questions())} questions and {len(load_answers())} local answers.")
    print(f"Prepared {len(answer_payload())} answers for submission.")
    demo.launch()