vladyslav
commited on
Commit
·
f86ba47
1
Parent(s):
eeee501
Saving test results and feedback in MongoDB. utils refactoring
Browse files- app.py +27 -7
- utils/__init__.py +2 -0
- utils/db.py +26 -0
- utils.py → utils/utils.py +0 -0
app.py
CHANGED
@@ -1,8 +1,11 @@
|
|
1 |
import json
|
2 |
import os
|
|
|
3 |
import gradio as gr
|
4 |
from dotenv import load_dotenv
|
5 |
|
|
|
|
|
6 |
load_dotenv()
|
7 |
|
8 |
MODELS = {
|
@@ -64,7 +67,8 @@ def get_next_question(selected_answer):
|
|
64 |
# Writing answer in log
|
65 |
answers_log.append({
|
66 |
"selected": selected_answer,
|
67 |
-
"isCorrect": any(answer['answer'] == selected_answer and answer['isCorrect'] for answer in
|
|
|
68 |
})
|
69 |
|
70 |
# Move to the next question
|
@@ -73,14 +77,26 @@ def get_next_question(selected_answer):
|
|
73 |
next_question = questions_data[current_question_index]
|
74 |
question_text = f"# Питання:\n## {next_question['question']}"
|
75 |
answers = [answer['answer'] for answer in next_question['answers']]
|
76 |
-
return question_text, gr.update(choices=answers, interactive=True), gr.update(visible=True), gr.update(
|
|
|
77 |
else:
|
78 |
# All questions are completed — ask for feedback
|
79 |
question_text = "Дякуємо за участь у тесті! Залиште, будь ласка, свій відгук і оцініть тест."
|
80 |
-
return question_text, gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
|
|
82 |
|
83 |
-
def summarize_results(feedback, rating):
|
84 |
correct_answers_count = sum(1 for answer in answers_log if answer['isCorrect'])
|
85 |
total_questions = len(questions_data)
|
86 |
summary = f"# Усі питання пройдено.\n\n## Ви відповіли правильно на {correct_answers_count} з {total_questions} питань.\n\n### Ваш відгук: {feedback}\n### Ваша оцінка: {rating}/5"
|
@@ -91,6 +107,8 @@ with gr.Blocks() as demo:
|
|
91 |
with gr.Column():
|
92 |
gr.Markdown("# Оберіть модель та книгу, щоб завантажити питання")
|
93 |
|
|
|
|
|
94 |
model_radio = gr.Radio(list(MODELS.keys()), label="Оберіть модель")
|
95 |
book_radio = gr.Radio(list(BOOKS.keys()), label="Оберіть книгу")
|
96 |
|
@@ -103,9 +121,12 @@ with gr.Blocks() as demo:
|
|
103 |
rating_slider = gr.Slider(1, 5, step=1, label="Оцініть тест", visible=False)
|
104 |
submit_feedback_button = gr.Button("Завершити тест", visible=False)
|
105 |
|
|
|
106 |
def update_question(model, book):
|
107 |
question, answers = load_questions(model, book)
|
108 |
-
return question, gr.update(choices=answers, interactive=True, visible=True), gr.update(
|
|
|
|
|
109 |
|
110 |
load_button.click(
|
111 |
update_question,
|
@@ -121,10 +142,9 @@ with gr.Blocks() as demo:
|
|
121 |
|
122 |
submit_feedback_button.click(
|
123 |
summarize_results,
|
124 |
-
inputs=[feedback_input, rating_slider],
|
125 |
outputs=[question_output, feedback_input, rating_slider, submit_feedback_button]
|
126 |
)
|
127 |
|
128 |
-
|
129 |
if __name__ == "__main__":
|
130 |
demo.launch()
|
|
|
1 |
import json
|
2 |
import os
|
3 |
+
|
4 |
import gradio as gr
|
5 |
from dotenv import load_dotenv
|
6 |
|
7 |
+
from utils import save_results
|
8 |
+
|
9 |
load_dotenv()
|
10 |
|
11 |
MODELS = {
|
|
|
67 |
# Writing answer in log
|
68 |
answers_log.append({
|
69 |
"selected": selected_answer,
|
70 |
+
"isCorrect": any(answer['answer'] == selected_answer and answer['isCorrect'] for answer in
|
71 |
+
questions_data[current_question_index]['answers'])
|
72 |
})
|
73 |
|
74 |
# Move to the next question
|
|
|
77 |
next_question = questions_data[current_question_index]
|
78 |
question_text = f"# Питання:\n## {next_question['question']}"
|
79 |
answers = [answer['answer'] for answer in next_question['answers']]
|
80 |
+
return question_text, gr.update(choices=answers, interactive=True), gr.update(visible=True), gr.update(
|
81 |
+
visible=False), gr.update(visible=False), gr.update(visible=False)
|
82 |
else:
|
83 |
# All questions are completed — ask for feedback
|
84 |
question_text = "Дякуємо за участь у тесті! Залиште, будь ласка, свій відгук і оцініть тест."
|
85 |
+
return question_text, gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(
|
86 |
+
visible=True), gr.update(visible=True)
|
87 |
+
|
88 |
+
|
89 |
+
def summarize_results(student_name, class_name, model, book, feedback, rating):
|
90 |
+
global questions_data, answers_log
|
91 |
+
questions = []
|
92 |
+
for question, answer in zip(questions_data, answers_log):
|
93 |
+
questions.append({
|
94 |
+
"question": question,
|
95 |
+
"answer": answer
|
96 |
+
})
|
97 |
|
98 |
+
save_results(student_name, class_name, model, book, questions, feedback, rating)
|
99 |
|
|
|
100 |
correct_answers_count = sum(1 for answer in answers_log if answer['isCorrect'])
|
101 |
total_questions = len(questions_data)
|
102 |
summary = f"# Усі питання пройдено.\n\n## Ви відповіли правильно на {correct_answers_count} з {total_questions} питань.\n\n### Ваш відгук: {feedback}\n### Ваша оцінка: {rating}/5"
|
|
|
107 |
with gr.Column():
|
108 |
gr.Markdown("# Оберіть модель та книгу, щоб завантажити питання")
|
109 |
|
110 |
+
student_name_input = gr.Textbox(label="Ваше ім'я")
|
111 |
+
class_name_input = gr.Textbox(label="Ваш клас")
|
112 |
model_radio = gr.Radio(list(MODELS.keys()), label="Оберіть модель")
|
113 |
book_radio = gr.Radio(list(BOOKS.keys()), label="Оберіть книгу")
|
114 |
|
|
|
121 |
rating_slider = gr.Slider(1, 5, step=1, label="Оцініть тест", visible=False)
|
122 |
submit_feedback_button = gr.Button("Завершити тест", visible=False)
|
123 |
|
124 |
+
|
125 |
def update_question(model, book):
|
126 |
question, answers = load_questions(model, book)
|
127 |
+
return question, gr.update(choices=answers, interactive=True, visible=True), gr.update(
|
128 |
+
visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
129 |
+
|
130 |
|
131 |
load_button.click(
|
132 |
update_question,
|
|
|
142 |
|
143 |
submit_feedback_button.click(
|
144 |
summarize_results,
|
145 |
+
inputs=[student_name_input, class_name_input, model_radio, book_radio, feedback_input, rating_slider],
|
146 |
outputs=[question_output, feedback_input, rating_slider, submit_feedback_button]
|
147 |
)
|
148 |
|
|
|
149 |
if __name__ == "__main__":
|
150 |
demo.launch()
|
utils/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
from .utils import *
|
2 |
+
from .db import *
|
utils/db.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from pymongo import MongoClient
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
client = MongoClient(os.getenv("MONGO_CONNECTION_STRING"))
|
9 |
+
|
10 |
+
db = client[os.getenv("MONGO_DB_NAME")]
|
11 |
+
|
12 |
+
collection_name = os.getenv("DEV_MONGO_COLLECTION") if os.getenv("ENV_TYPE") == "dev" else os.getenv(
|
13 |
+
"PROD_MONGO_COLLECTION")
|
14 |
+
collection = db[collection_name]
|
15 |
+
|
16 |
+
|
17 |
+
def save_results(student_name, class_name, model, book, questions, feedback, rating):
|
18 |
+
collection.insert_one({
|
19 |
+
"student_name": student_name,
|
20 |
+
"class": class_name,
|
21 |
+
"model": model,
|
22 |
+
"book": book,
|
23 |
+
"questions": questions,
|
24 |
+
"feedback": feedback,
|
25 |
+
"rating": rating
|
26 |
+
})
|
utils.py → utils/utils.py
RENAMED
File without changes
|