Spaces:
Running
Running
Commit
·
d0b115f
1
Parent(s):
207870e
added ability to see generated questions in gradio app!
Browse files- app.py +24 -6
- uw_programmatic/csv_to_markdown.py +49 -0
app.py
CHANGED
@@ -4,10 +4,12 @@ import gradio as gr
|
|
4 |
from typing import Any, Callable
|
5 |
import contextvars
|
6 |
from uw_programmatic.uw_machine import UWMachine
|
|
|
|
|
7 |
from dotenv import load_dotenv
|
8 |
import os
|
9 |
|
10 |
-
|
11 |
def run_with_context(func: Callable) -> Callable:
|
12 |
ctx = contextvars.copy_context()
|
13 |
|
@@ -16,7 +18,7 @@ def run_with_context(func: Callable) -> Callable:
|
|
16 |
|
17 |
return wrapper
|
18 |
|
19 |
-
|
20 |
def generate_questions(
|
21 |
page_lower, page_higher, question_number, taxonomy
|
22 |
) -> tuple[str, gr.DownloadButton, gr.DownloadButton]:
|
@@ -79,11 +81,23 @@ def generate_questions(
|
|
79 |
markdown = "## Questions Not Ready for Download"
|
80 |
exception = "Bad Page Range Selected"
|
81 |
raise gr.Error(exception)
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
|
85 |
def create_statemachine() -> None:
|
86 |
-
# Creates
|
87 |
cwd_path = Path.cwd() / "uw_programmatic"
|
88 |
config_path = cwd_path.joinpath(Path("config.yaml"))
|
89 |
try:
|
@@ -127,11 +141,15 @@ with gr.Blocks() as demo:
|
|
127 |
download_failures = gr.DownloadButton(
|
128 |
label="Download Failed Questions", visible=False, scale=1
|
129 |
)
|
|
|
|
|
|
|
|
|
130 |
create_statemachine()
|
131 |
start_button.click(
|
132 |
fn=run_with_context(generate_questions),
|
133 |
inputs=[page_lower, page_higher, question_number, taxonomy],
|
134 |
-
outputs=[output, download_professor, download_failures],
|
135 |
)
|
136 |
download_professor.click(
|
137 |
fn=run_with_context(questions_downloaded), outputs=[download_professor]
|
@@ -146,4 +164,4 @@ demo.launch(
|
|
146 |
ssr_mode=False,
|
147 |
auth=(os.environ.get("HF_USERNAME", ""), os.environ.get("HF_PASSWORD", "")),
|
148 |
)
|
149 |
-
#
|
|
|
4 |
from typing import Any, Callable
|
5 |
import contextvars
|
6 |
from uw_programmatic.uw_machine import UWMachine
|
7 |
+
from uw_programmatic.csv_to_markdown import convert_csv_to_markdown
|
8 |
+
import pandas as pd
|
9 |
from dotenv import load_dotenv
|
10 |
import os
|
11 |
|
12 |
+
# Used to ensure that EventBus works across Gradio and the StateMachine.
|
13 |
def run_with_context(func: Callable) -> Callable:
|
14 |
ctx = contextvars.copy_context()
|
15 |
|
|
|
18 |
|
19 |
return wrapper
|
20 |
|
21 |
+
# Runs the state machine - sends the parameters over and kicks off the whole process.
|
22 |
def generate_questions(
|
23 |
page_lower, page_higher, question_number, taxonomy
|
24 |
) -> tuple[str, gr.DownloadButton, gr.DownloadButton]:
|
|
|
81 |
markdown = "## Questions Not Ready for Download"
|
82 |
exception = "Bad Page Range Selected"
|
83 |
raise gr.Error(exception)
|
84 |
+
# After questions are generated, read the CSV file
|
85 |
+
csv_path = Path.cwd().joinpath("outputs/professor_guide.csv")
|
86 |
+
if csv_path.exists():
|
87 |
+
# Generate markdown content
|
88 |
+
markdown_content = convert_csv_to_markdown(csv_path)
|
89 |
+
return (
|
90 |
+
markdown,
|
91 |
+
button,
|
92 |
+
button_2,
|
93 |
+
gr.update(value=markdown_content, visible=True)
|
94 |
+
)
|
95 |
+
else:
|
96 |
+
return (markdown, button, button_2, gr.update(visible=False))
|
97 |
|
98 |
|
99 |
def create_statemachine() -> None:
|
100 |
+
# Creates UWMachine from the config.yaml in current directory
|
101 |
cwd_path = Path.cwd() / "uw_programmatic"
|
102 |
config_path = cwd_path.joinpath(Path("config.yaml"))
|
103 |
try:
|
|
|
141 |
download_failures = gr.DownloadButton(
|
142 |
label="Download Failed Questions", visible=False, scale=1
|
143 |
)
|
144 |
+
with gr.Row():
|
145 |
+
#Markdown component to display generated questions as an ordered list
|
146 |
+
question_output = gr.Markdown(visible=False)
|
147 |
+
# State machine must be initialized in gr.Blocks for the context wrapper to work.
|
148 |
create_statemachine()
|
149 |
start_button.click(
|
150 |
fn=run_with_context(generate_questions),
|
151 |
inputs=[page_lower, page_higher, question_number, taxonomy],
|
152 |
+
outputs=[output, download_professor, download_failures, question_output],
|
153 |
)
|
154 |
download_professor.click(
|
155 |
fn=run_with_context(questions_downloaded), outputs=[download_professor]
|
|
|
164 |
ssr_mode=False,
|
165 |
auth=(os.environ.get("HF_USERNAME", ""), os.environ.get("HF_PASSWORD", "")),
|
166 |
)
|
167 |
+
#demo.launch()
|
uw_programmatic/csv_to_markdown.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from pathlib import Path
|
3 |
+
from string import ascii_lowercase
|
4 |
+
|
5 |
+
def convert_csv_to_markdown(csv_path: Path) -> str:
|
6 |
+
# Load the CSV file without using the first row as headers
|
7 |
+
df = pd.read_csv(csv_path, header=None)
|
8 |
+
|
9 |
+
# Manually set column names
|
10 |
+
df.columns = ["Type", "Group", "Points", "Question", "Correct Answer",
|
11 |
+
"Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5",
|
12 |
+
"Page", "Taxonomy"]
|
13 |
+
|
14 |
+
# Generate Markdown output
|
15 |
+
markdown_output = "## Generated Questions\n\n"
|
16 |
+
|
17 |
+
for index, row in df.iterrows():
|
18 |
+
question = row["Question"]
|
19 |
+
correct_index = row["Correct Answer"]
|
20 |
+
|
21 |
+
# Skip rows where the correct answer index is missing
|
22 |
+
if pd.isna(correct_index):
|
23 |
+
continue
|
24 |
+
|
25 |
+
correct_index = int(correct_index) - 1 # Convert 1-based index to 0-based
|
26 |
+
choices = row[["Choice 1", "Choice 2", "Choice 3", "Choice 4", "Choice 5"]].dropna().tolist()
|
27 |
+
metadata = row[["Page", "Taxonomy"]].dropna().tolist()
|
28 |
+
|
29 |
+
markdown_output += f"{index+1}. {question}<br>"
|
30 |
+
|
31 |
+
for i, choice in enumerate(choices):
|
32 |
+
letter = ascii_lowercase[i] # Get letter a, b, c, etc.
|
33 |
+
# Indent choices and bold the correct answer
|
34 |
+
if i == correct_index:
|
35 |
+
markdown_output += f"  {letter}. **{choice}**<br>"
|
36 |
+
else:
|
37 |
+
markdown_output += f"  {letter}. {choice}<br>"
|
38 |
+
# Add metadata as additional choices, with modified page format
|
39 |
+
for meta in metadata:
|
40 |
+
if meta == row["Page"]: # If this metadata item is the page
|
41 |
+
# Remove first character and prepend "Pages"
|
42 |
+
page_number = meta[1:] # Remove first character
|
43 |
+
markdown_output += f" - Pages {page_number}<br>"
|
44 |
+
else:
|
45 |
+
markdown_output += f" - {meta}<br><br>"
|
46 |
+
|
47 |
+
markdown_output += "<br>" # Space between questions
|
48 |
+
|
49 |
+
return markdown_output
|