Spaces:
Running
Running
johnpaulbin
commited on
Commit
•
1da0920
1
Parent(s):
61e5b61
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
import os
|
|
|
4 |
import google.generativeai as genai
|
5 |
|
6 |
# Function to get available JSON files in the working directory
|
@@ -41,83 +42,104 @@ def change_question(week_file, index, direction):
|
|
41 |
index = (index + direction) % total
|
42 |
return index, *flashcard_ui(week_file, index)
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
"top_p": 0.98,
|
53 |
-
"top_k": 64,
|
54 |
-
"max_output_tokens": 8192,
|
55 |
-
"response_mime_type": "text/plain",
|
56 |
-
}
|
57 |
-
|
58 |
-
model = genai.GenerativeModel(
|
59 |
-
model_name="gemini-1.5-flash-8b-exp-0827",
|
60 |
-
generation_config=generation_config,
|
61 |
-
)
|
62 |
-
|
63 |
-
# AI Chatbot function
|
64 |
-
def chat_with_ai(user_input):
|
65 |
-
chat_session = model.start_chat(
|
66 |
-
history=[
|
67 |
-
{
|
68 |
-
"role": "user",
|
69 |
-
"parts": [ prompt ],
|
70 |
-
},
|
71 |
-
{
|
72 |
-
"role": "model",
|
73 |
-
"parts": [
|
74 |
-
"Sure, I can answer your question. \n",
|
75 |
-
],
|
76 |
-
},
|
77 |
-
]
|
78 |
-
)
|
79 |
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
82 |
|
83 |
# Gradio interface
|
84 |
with gr.Blocks() as demo:
|
85 |
available_weeks = get_available_weeks()
|
86 |
|
87 |
-
#
|
88 |
-
with gr.
|
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 |
# Launch the app
|
123 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import json
|
3 |
import os
|
4 |
+
import random
|
5 |
import google.generativeai as genai
|
6 |
|
7 |
# Function to get available JSON files in the working directory
|
|
|
42 |
index = (index + direction) % total
|
43 |
return index, *flashcard_ui(week_file, index)
|
44 |
|
45 |
+
# Function to load mock test questions
|
46 |
+
def load_mocktest():
|
47 |
+
try:
|
48 |
+
with open("mocktest.json", "r") as f:
|
49 |
+
data = json.load(f)
|
50 |
+
return data
|
51 |
+
except FileNotFoundError:
|
52 |
+
return None
|
53 |
|
54 |
+
# Function to display the mock test question and answers
|
55 |
+
def display_mock_question(index, score, incorrect_questions):
|
56 |
+
data = load_mocktest()
|
57 |
+
if not data:
|
58 |
+
return "Mocktest file not found.", None, None, None, None, None
|
59 |
+
|
60 |
+
question_data = data[index]
|
61 |
+
question = f"Question {index + 1}: {question_data['question']}"
|
62 |
+
correct_answer = question_data["correct-answer"]
|
63 |
+
all_answers = question_data["wrong-answers"] + [correct_answer]
|
64 |
+
random.shuffle(all_answers)
|
65 |
+
|
66 |
+
return question, all_answers[0], all_answers[1], all_answers[2], all_answers[3], correct_answer
|
67 |
+
|
68 |
+
# Function to handle answer submission
|
69 |
+
def handle_answer(index, answer, score, incorrect_questions):
|
70 |
+
data = load_mocktest()
|
71 |
+
correct_answer = data[index]["correct-answer"]
|
72 |
+
|
73 |
+
if answer == correct_answer:
|
74 |
+
score += 1
|
75 |
+
else:
|
76 |
+
incorrect_questions.append(data[index]["question"])
|
77 |
|
78 |
+
index += 1
|
79 |
+
if index >= len(data):
|
80 |
+
return gr.update(visible=False), f"Your score: {score}/{len(data)}", incorrect_questions, gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
+
return *display_mock_question(index, score, incorrect_questions), score, incorrect_questions, gr.update(visible=True)
|
83 |
+
|
84 |
+
# Function to restart the test
|
85 |
+
def restart_test():
|
86 |
+
return 0, "", [], *display_mock_question(0, 0, []), gr.update(visible=False), gr.update(visible=True)
|
87 |
|
88 |
# Gradio interface
|
89 |
with gr.Blocks() as demo:
|
90 |
available_weeks = get_available_weeks()
|
91 |
|
92 |
+
# Tabs for Flashcards and Mock Test
|
93 |
+
with gr.Tabs():
|
94 |
+
with gr.TabItem("Flashcards"):
|
95 |
+
# Splash page to select the week
|
96 |
+
with gr.Row():
|
97 |
+
week_file = gr.Dropdown(choices=available_weeks, label="Select CAM Week to Study")
|
98 |
+
start_button = gr.Button("Start Studying")
|
99 |
+
|
100 |
+
# Flashcard UI (hidden until week is selected)
|
101 |
+
with gr.Column(visible=False) as flashcard_section:
|
102 |
+
index = gr.State(0)
|
103 |
+
question_output = gr.Textbox(label="Flashcard", interactive=False)
|
104 |
+
answer_output = gr.Textbox(label="Answer", interactive=False)
|
105 |
+
|
106 |
+
with gr.Row():
|
107 |
+
prev_btn = gr.Button("Previous")
|
108 |
+
reveal_btn = gr.Button("Reveal Answer")
|
109 |
+
next_btn = gr.Button("Next")
|
110 |
+
|
111 |
+
# Start button action to reveal the flashcard section
|
112 |
+
start_button.click(lambda: gr.update(visible=True), inputs=[], outputs=flashcard_section)
|
113 |
+
week_file.change(flashcard_ui, inputs=[week_file, index], outputs=[question_output, answer_output])
|
114 |
+
reveal_btn.click(reveal_answer, inputs=[week_file, index], outputs=answer_output)
|
115 |
+
prev_btn.click(change_question, inputs=[week_file, index, gr.Number(-1, visible=False)], outputs=[index, question_output, answer_output])
|
116 |
+
next_btn.click(change_question, inputs=[week_file, index, gr.Number(1, visible=False)], outputs=[index, question_output, answer_output])
|
117 |
+
|
118 |
+
with gr.TabItem("Mock Test"):
|
119 |
+
mock_index = gr.State(0)
|
120 |
+
score = gr.State(0)
|
121 |
+
incorrect_questions = gr.State([])
|
122 |
+
|
123 |
+
question_output = gr.Markdown(label="Question")
|
124 |
+
answer_1 = gr.Button("Answer 1")
|
125 |
+
answer_2 = gr.Button("Answer 2")
|
126 |
+
answer_3 = gr.Button("Answer 3")
|
127 |
+
answer_4 = gr.Button("Answer 4")
|
128 |
+
|
129 |
+
result_output = gr.Markdown(visible=False)
|
130 |
+
restart_button = gr.Button("Restart Test", visible=False)
|
131 |
+
|
132 |
+
# Initialize the first question
|
133 |
+
demo.load(display_mock_question, inputs=[mock_index, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4])
|
134 |
+
|
135 |
+
# Handle answer selection
|
136 |
+
answer_1.click(handle_answer, inputs=[mock_index, answer_1, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4, score, incorrect_questions, result_output])
|
137 |
+
answer_2.click(handle_answer, inputs=[mock_index, answer_2, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4, score, incorrect_questions, result_output])
|
138 |
+
answer_3.click(handle_answer, inputs=[mock_index, answer_3, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4, score, incorrect_questions, result_output])
|
139 |
+
answer_4.click(handle_answer, inputs=[mock_index, answer_4, score, incorrect_questions], outputs=[question_output, answer_1, answer_2, answer_3, answer_4, score, incorrect_questions, result_output])
|
140 |
+
|
141 |
+
# Restart test button
|
142 |
+
restart_button.click(restart_test, inputs=[], outputs=[mock_index, score, incorrect_questions, question_output, answer_1, answer_2, answer_3, answer_4, result_output, restart_button])
|
143 |
|
144 |
# Launch the app
|
145 |
demo.launch()
|