File size: 4,277 Bytes
d477d5c
 
 
 
 
 
2e9d7fb
 
d477d5c
 
 
 
 
 
 
 
 
 
 
 
 
2e9d7fb
d477d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
f1bae18
 
 
 
 
 
 
 
 
 
d274da8
f1bae18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d477d5c
 
 
c56aab6
d477d5c
2e9d7fb
e7445ae
2e9d7fb
d477d5c
 
 
 
 
 
 
 
 
 
 
2e9d7fb
 
e7445ae
d477d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e9d7fb
 
 
 
 
d477d5c
63e3e29
1874ad1
889f3de
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
from pathlib import Path
import gradio as gr
from typing import Any, Callable
import contextvars
from uw_programmatic.uw_machine import UWMachine
from dotenv import load_dotenv
import os


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],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)
    try:
        machine.value.send(
            "process_event",
            event_={
                "type": "user_input",
                "value": {
                    "page_range": (page_lower, page_higher),
                    "question_number": question_number,
                    "taxonomy": taxonomy,
                },
            },
        )
    except AttributeError:
        create_statemachine()
        machine.value.start_machine()
        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.csv')}"
        ),
        gr.update(
            visible=False
        )
    )


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
    
def questions_downloaded() -> dict[str,Any]:
    return gr.update(interactive=True, visible=True)


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, start_button],
    )
    download_professor.click(
        fn=run_with_context(questions_downloaded),
        outputs=[start_button]
    )
load_dotenv()
demo.launch(share=True, ssr_mode=False,
    auth=(os.environ.get('HF_USERNAME',''),os.environ.get('HF_PASSWORD','')))