johnpaulbin commited on
Commit
36ab372
1 Parent(s): dcb4152

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -24
app.py CHANGED
@@ -2,18 +2,23 @@ import gradio as gr
2
  import json
3
  import os
4
 
 
 
 
 
 
5
  # Function to load questions from the specified week file
6
- def load_questions(week):
7
  try:
8
- with open(f"week-{week}.json", "r") as f:
9
  data = json.load(f)
10
  return data, None
11
  except FileNotFoundError:
12
- return None, f"Week {week} file not found."
13
 
14
  # Flashcard UI function
15
- def flashcard_ui(week, index):
16
- data, error = load_questions(week)
17
  if error:
18
  return f"Error: {error}", None
19
  question = data[index]["question"]
@@ -21,37 +26,48 @@ def flashcard_ui(week, index):
21
  return f"Question {index + 1}/{total}: {question}", ""
22
 
23
  # Reveal answer function
24
- def reveal_answer(week, index):
25
- data, error = load_questions(week)
26
  if error:
27
  return None
28
  answer = data[index]["answer"]
29
  return answer
30
 
31
  # Function to handle navigation
32
- def change_question(week, index, direction):
33
- data, _ = load_questions(week)
34
  total = len(data)
35
  index = (index + direction) % total
36
- return index, *flashcard_ui(week, index)
37
 
38
  # Gradio interface
39
  with gr.Blocks() as demo:
40
- week = gr.Number(label="Enter CAM Week to Study", value=1)
41
- index = gr.State(0) # State to keep track of the current index
42
- question_output = gr.Textbox(label="Flashcard", interactive=False)
43
- answer_output = gr.Textbox(label="Answer", interactive=False)
44
-
45
  with gr.Row():
46
- prev_btn = gr.Button("Previous")
47
- reveal_btn = gr.Button("Reveal Answer")
48
- next_btn = gr.Button("Next")
49
-
50
- # Event handling
51
- week.change(flashcard_ui, inputs=[week, index], outputs=[question_output, answer_output])
52
- reveal_btn.click(reveal_answer, inputs=[week, index], outputs=answer_output)
53
- prev_btn.click(change_question, inputs=[week, index, gr.Number(-1, visible=False)], outputs=[index, question_output, answer_output])
54
- next_btn.click(change_question, inputs=[week, index, gr.Number(1, visible=False)], outputs=[index, question_output, answer_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  # Launch the app
57
  demo.launch()
 
2
  import json
3
  import os
4
 
5
+ # Function to get available JSON files in the working directory
6
+ def get_available_weeks():
7
+ files = [f for f in os.listdir() if f.startswith('week-') and f.endswith('.json')]
8
+ return files
9
+
10
  # Function to load questions from the specified week file
11
+ def load_questions(week_file):
12
  try:
13
+ with open(week_file, "r") as f:
14
  data = json.load(f)
15
  return data, None
16
  except FileNotFoundError:
17
+ return None, f"File {week_file} not found."
18
 
19
  # Flashcard UI function
20
+ def flashcard_ui(week_file, index):
21
+ data, error = load_questions(week_file)
22
  if error:
23
  return f"Error: {error}", None
24
  question = data[index]["question"]
 
26
  return f"Question {index + 1}/{total}: {question}", ""
27
 
28
  # Reveal answer function
29
+ def reveal_answer(week_file, index):
30
+ data, error = load_questions(week_file)
31
  if error:
32
  return None
33
  answer = data[index]["answer"]
34
  return answer
35
 
36
  # Function to handle navigation
37
+ def change_question(week_file, index, direction):
38
+ data, _ = load_questions(week_file)
39
  total = len(data)
40
  index = (index + direction) % total
41
+ return index, *flashcard_ui(week_file, index)
42
 
43
  # Gradio interface
44
  with gr.Blocks() as demo:
45
+ available_weeks = get_available_weeks()
46
+
47
+ # Splash page to select the week
 
 
48
  with gr.Row():
49
+ week_file = gr.Dropdown(choices=available_weeks, label="Select CAM Week to Study")
50
+ start_button = gr.Button("Start Studying")
51
+
52
+ # Flashcard UI (hidden until week is selected)
53
+ with gr.Column(visible=False) as flashcard_section:
54
+ index = gr.State(0)
55
+ question_output = gr.Textbox(label="Flashcard", interactive=False)
56
+ answer_output = gr.Textbox(label="Answer", interactive=False)
57
+
58
+ with gr.Row():
59
+ prev_btn = gr.Button("Previous")
60
+ reveal_btn = gr.Button("Reveal Answer")
61
+ next_btn = gr.Button("Next")
62
+
63
+ # Start button action to reveal the flashcard section
64
+ start_button.click(lambda: gr.update(visible=True), inputs=[], outputs=flashcard_section)
65
+
66
+ # Event handling for flashcards
67
+ week_file.change(flashcard_ui, inputs=[week_file, index], outputs=[question_output, answer_output])
68
+ reveal_btn.click(reveal_answer, inputs=[week_file, index], outputs=answer_output)
69
+ prev_btn.click(change_question, inputs=[week_file, index, gr.Number(-1, visible=False)], outputs=[index, question_output, answer_output])
70
+ next_btn.click(change_question, inputs=[week_file, index, gr.Number(1, visible=False)], outputs=[index, question_output, answer_output])
71
 
72
  # Launch the app
73
  demo.launch()