johnpaulbin commited on
Commit
1da0920
1 Parent(s): 61e5b61

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -69
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
- with open("aa2.txt", "r") as f:
45
- prompt = f.read()
 
 
 
 
 
 
46
 
47
- genai.configure(api_key=os.environ["GEMINI_API_KEY"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- # Create the model configuration
50
- generation_config = {
51
- "temperature": 0.5,
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
- response = chat_session.send_message(user_input)
81
- return response.text
 
 
 
82
 
83
  # Gradio interface
84
  with gr.Blocks() as demo:
85
  available_weeks = get_available_weeks()
86
 
87
- # Splash page to select the week
88
- with gr.Row():
89
- week_file = gr.Dropdown(choices=available_weeks, label="Select CAM Week to Study")
90
- start_button = gr.Button("Start Studying")
91
-
92
- # Flashcard UI (hidden until week is selected)
93
- with gr.Column(visible=False) as flashcard_section:
94
- index = gr.State(0)
95
- question_output = gr.Textbox(label="Flashcard", interactive=False)
96
- answer_output = gr.Textbox(label="Answer", interactive=False)
97
-
98
- with gr.Row():
99
- prev_btn = gr.Button("Previous")
100
- reveal_btn = gr.Button("Reveal Answer")
101
- next_btn = gr.Button("Next")
102
-
103
- with gr.Column(visible=True):
104
- chat_input = gr.Textbox(label="Ask a question to the A.I.")
105
- with gr.Row():
106
- gr.Markdown(">Note: This A.I. has only been given the NBAA Management guide. Use the flashcards for specific week information.")
107
- chat_output = gr.Markdown(label="AI Response")
108
- chat_button = gr.Button("Ask AI")
109
-
110
- # Start button action to reveal the flashcard section
111
- start_button.click(lambda: gr.update(visible=True), inputs=[], outputs=flashcard_section)
112
-
113
- # Event handling for flashcards
114
- week_file.change(flashcard_ui, inputs=[week_file, index], outputs=[question_output, answer_output])
115
- reveal_btn.click(reveal_answer, inputs=[week_file, index], outputs=answer_output)
116
- prev_btn.click(change_question, inputs=[week_file, index, gr.Number(-1, visible=False)], outputs=[index, question_output, answer_output])
117
- next_btn.click(change_question, inputs=[week_file, index, gr.Number(1, visible=False)], outputs=[index, question_output, answer_output])
118
-
119
- # Chatbot button action
120
- chat_button.click(chat_with_ai, inputs=[chat_input], outputs=[chat_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()