File size: 3,362 Bytes
d477d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
from pathlib import Path
import gradio as gr
from typing import Any, Callable
import contextvars
from uw_programmatic.uw_machine import UWMachine


def run_with_context(func: Callable) -> Callable:
    ctx = contextvars.copy_context()

    def wrapper(*args, **kwargs) -> Any:
        return ctx.run(func, *args, **kwargs)

    return wrapper


def generate_questions(
    page_lower, page_higher, question_number, taxonomy
) -> tuple[str, dict[str, Any]]:
    if machine.value and machine.value.current_state_value == "start":
        machine.value.start_machine()  # Start the machine!
    if not question_number or question_number <= 0:
        msg = "Choose a valid question number."
        raise gr.Error(msg)
    if not page_lower or not page_higher or page_higher < page_lower:
        msg = "Choose a valid page range."
        raise gr.Error(msg)
    if page_higher - page_lower <= 6:
        msg = "Page range must be >6."
        raise gr.Error(msg)
    if not taxonomy or len(taxonomy) == 0:
        msg = "Choose at least one taxonomy."
        raise gr.Error(msg)
    machine.value.send(
        "process_event",
        event_={
            "type": "user_input",
            "value": {
                "page_range": (page_lower, page_higher),
                "question_number": question_number,
                "taxonomy": taxonomy,
            },
        },
    )
    return (
        "## Questions Ready for Download Below",
        gr.update(
            visible=True, value=f"{Path.cwd().joinpath('outputs/professor_guide.xlsx')}"
        ),
    )


def create_statemachine() -> None:
    # Creates GoapMachine from the config.yaml in current directory
    cwd_path = Path.cwd() / "uw_programmatic"
    config_path = cwd_path.joinpath(Path("config.yaml"))
    try:
        machine.value = UWMachine.from_config_file(config_path)
    except Exception as e:
        raise gr.Error(str(e)) from e


with gr.Blocks() as demo:
    gr.Markdown("# UW Quiz Generator")
    machine = gr.State(value=None)
    with gr.Row():
        with gr.Column(scale=2):
            taxonomy = gr.CheckboxGroup(
                choices=["Knowledge", "Comprehension", "Application"],
                label="Taxonomy",
                value="Knowledge",
            )
            question_number = gr.Number(
                minimum=1, maximum=15, label="Number of Questions", value=3
            )
            gr.Markdown("For Chapter 3 - Pages 88-309")
            with gr.Row():
                page_lower = gr.Number(
                    label="First Page", minimum=88, value=88, maximum=309
                )
                page_higher = gr.Number(
                    label="Last Page", minimum=88, value=309, maximum=309
                )
            start_button = gr.Button(value="Generate Questions")

        with gr.Column(scale=1):
            output = gr.Markdown("## Questions Not Ready for Download", visible=True)
            download_professor = gr.DownloadButton(
                label="Download Questions", visible=False
            )
    create_statemachine()
    start_button.click(
        fn=run_with_context(generate_questions),
        inputs=[page_lower, page_higher, question_number, taxonomy],
        outputs=[output, download_professor],
    )
# TODO: Add a username and password here.
demo.launch()