johnpaulbin commited on
Commit
d5977e3
1 Parent(s): 43d724a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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"]
20
+ answer = data[index]["answer"]
21
+ total = len(data)
22
+ return f"Question {index + 1}/{total}: {question}", ""
23
+
24
+ # Reveal answer function
25
+ def reveal_answer(week, index):
26
+ data, error = load_questions(week)
27
+ if error:
28
+ return None
29
+ answer = data[index]["answer"]
30
+ return answer
31
+
32
+ # Function to handle navigation
33
+ def change_question(week, index, direction):
34
+ data, _ = load_questions(week)
35
+ total = len(data)
36
+ index = (index + direction) % total
37
+ return index, *flashcard_ui(week, index)
38
+
39
+ # Gradio interface
40
+ with gr.Blocks() as demo:
41
+ week = gr.Number(label="Enter CAM Week to Study", value=1)
42
+ index = gr.Number(value=0, visible=False)
43
+ question_output = gr.Textbox(label="Flashcard", interactive=False)
44
+ answer_output = gr.Textbox(label="Answer", interactive=False)
45
+
46
+ with gr.Row():
47
+ prev_btn = gr.Button("Previous")
48
+ reveal_btn = gr.Button("Reveal Answer")
49
+ next_btn = gr.Button("Next")
50
+
51
+ # Event handling
52
+ week.change(flashcard_ui, inputs=[week, index], outputs=[question_output, answer_output])
53
+ reveal_btn.click(reveal_answer, inputs=[week, index], outputs=answer_output)
54
+ prev_btn.click(change_question, inputs=[week, index, gr.Number(-1)], outputs=[index, question_output, answer_output])
55
+ next_btn.click(change_question, inputs=[week, index, gr.Number(1)], outputs=[index, question_output, answer_output])
56
+
57
+ # Launch the app
58
+ demo.launch()