File size: 5,592 Bytes
193db9d
 
973519b
 
193db9d
633b045
193db9d
 
 
 
 
633b045
973519b
 
 
 
 
 
 
 
 
193db9d
 
973519b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193db9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
973519b
193db9d
 
 
973519b
 
 
193db9d
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import datasets
import gradio as gr
from apscheduler.schedulers.background import BackgroundScheduler
from huggingface_hub import snapshot_download

from app_configs import AVAILABLE_MODELS, DEFAULT_SELECTIONS, THEME
from components.quizbowl.bonus import BonusInterface
from components.quizbowl.tossup import TossupInterface
from display.custom_css import css_pipeline, css_tossup

# Constants
from envs import (
    API,
    EVAL_REQUESTS_PATH,
    EVAL_RESULTS_PATH,
    PLAYGROUND_DATASET_NAMES,
    QUEUE_REPO,
    REPO_ID,
    RESULTS_REPO,
    TOKEN,
)
from workflows import factory


def restart_space():
    API.restart_space(repo_id=REPO_ID)


### Space initialisation
try:
    print(EVAL_REQUESTS_PATH)
    snapshot_download(
        repo_id=QUEUE_REPO,
        local_dir=EVAL_REQUESTS_PATH,
        repo_type="dataset",
        tqdm_class=None,
        etag_timeout=30,
        token=TOKEN,
    )
except Exception:
    restart_space()
try:
    print(EVAL_RESULTS_PATH)
    snapshot_download(
        repo_id=RESULTS_REPO,
        local_dir=EVAL_RESULTS_PATH,
        repo_type="dataset",
        tqdm_class=None,
        etag_timeout=30,
        token=TOKEN,
    )
except Exception:
    restart_space()


js_preamble = """
<link href="https://fonts.cdnfonts.com/css/roboto-mono" rel="stylesheet">

<script>
    const gradioApp = document.getElementsByTagName('gradio-app')[0];
    console.log("Gradio app:", gradioApp);
    console.log(gradioApp.querySelectorAll('.token'));
    console.log(document.querySelectorAll('.token'));

    // Function to trigger Python callback
    const setHiddenIndex = (index) => {
        console.log("Setting hidden index to:", index);
        const hiddenIndex = gradioApp.querySelector("#hidden-index textarea");
        if (hiddenIndex) {
            hiddenIndex.value = index;
            let event = new Event("input", { bubbles: true});
            Object.defineProperty(event, "target", { value: hiddenIndex});
            hiddenIndex.dispatchEvent(event);
        }
    };

    // Add event listeners to all tokens
    function setupTokenListeners() {
        const tokens = gradioApp.querySelectorAll('.token');
        console.log("Tokens:", tokens);
        tokens.forEach(token => {
            token.addEventListener('mouseover', function() {
                const index = parseInt(this.getAttribute('data-index'));
                console.log("Mouseover token index:", index);

                // Reset all tokens
                gradioApp.querySelectorAll('.token').forEach(el => {
                    el.classList.remove('highlighted');
                });

                // Highlight this token
                this.classList.add('highlighted');

                // Update the hidden index to trigger the Python callback
                setHiddenIndex(index);
            });
        });
    }
    console.log("Preamble complete");

    document.addEventListener("DOMContentLoaded", function() {
        // Setup initial listeners
        console.log("DOM fully loaded and parsed");
        setupTokenListeners();

        // Setup a mutation observer to handle dynamically added tokens
        const observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                if (mutation.addedNodes.length) {
                    setupTokenListeners();
                }
            });
        });

        // Start observing the token container for changes
        const tokenContainer = gradioApp.querySelector('.token-container');
        console.log("Token container:", tokenContainer);
        if (tokenContainer) {
            observer.observe(tokenContainer.parentNode, { childList: true, subtree: true });
        }
        console.log("Listener setup complete");
    });
</script>
"""


def load_dataset(mode: str):
    if mode == "tossup":
        ds = datasets.load_dataset(PLAYGROUND_DATASET_NAMES["tossup"], split="eval")
        ds = ds.filter(lambda x: x["qid"].split("-")[2] == "1" and int(x["qid"].split("-")[3]) <= 10)
    elif mode == "bonus":
        ds = datasets.load_dataset(PLAYGROUND_DATASET_NAMES["bonus"], split="eval")
        ds = ds.filter(lambda x: x["qid"].split("-")[2] == "1" and int(x["qid"].split("-")[3]) <= 10)
    else:
        raise ValueError(f"Invalid mode: {mode}")

    return ds


def main():
    tossup_ds = load_dataset("tossup")
    bonus_ds = load_dataset("bonus")
    app = gr.Blocks(
        css=css_pipeline + css_tossup,
        head=js_preamble,
        theme=THEME,
        title="Quizbowl Bot",
    )
    with app:
        with gr.Tabs():
            with gr.Tab("Tossup Agents"):
                defaults = DEFAULT_SELECTIONS["tossup"] | {
                    "init_workflow": factory.create_quizbowl_simple_workflow(),
                    "simple_workflow": False,
                }
                tossup_interface = TossupInterface(app, tossup_ds, AVAILABLE_MODELS, defaults)
                # ModelStepComponent(value=factory.create_quizbowl_simple_step())
            with gr.Tab("Bonus Round Agents"):
                defaults = DEFAULT_SELECTIONS["bonus"] | {
                    "init_workflow": factory.create_quizbowl_bonus_simple_workflow(),
                    "simple_workflow": True,
                }
                bonus_interface = BonusInterface(app, bonus_ds, AVAILABLE_MODELS, defaults)

    app.queue(default_concurrency_limit=40).launch()


if __name__ == "__main__":
    scheduler = BackgroundScheduler()
    scheduler.add_job(restart_space, "interval", seconds=1800)
    scheduler.start()
    main()