Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
|
|
|
|
| 4 |
json_data = """
|
| 5 |
{
|
| 6 |
-
"
|
| 7 |
-
"
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
{"text": "Выберите Б", "correct_answer": "Б", "incorrect_text": "Правильный ответ - Б."}
|
| 11 |
-
]
|
| 12 |
-
},
|
| 13 |
-
"test2": {
|
| 14 |
-
"questions": [
|
| 15 |
-
{"text": "Выберите В", "correct_answer": "В", "incorrect_text": "Правильный ответ - В."},
|
| 16 |
-
{"text": "Выберите А", "correct_answer": "А", "incorrect_text": "Правильный ответ - А."}
|
| 17 |
-
]
|
| 18 |
-
}
|
| 19 |
-
}
|
| 20 |
}
|
| 21 |
"""
|
| 22 |
|
|
|
|
| 23 |
data = json.loads(json_data)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
css = """
|
| 28 |
footer {visibility: hidden !important;}
|
| 29 |
"""
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
back_button = gr.Button("Назад", visible=False)
|
| 40 |
-
|
| 41 |
-
def show_questions(test_name):
|
| 42 |
-
questions = data['tests'][test_name]['questions']
|
| 43 |
-
for i, question_block in enumerate(question_blocks):
|
| 44 |
-
if i < len(questions):
|
| 45 |
-
question_block.label = questions[i]['text']
|
| 46 |
-
question_block.visible = True
|
| 47 |
-
else:
|
| 48 |
-
question_block.visible = False
|
| 49 |
-
submit_button.visible = True
|
| 50 |
-
back_button.visible = True
|
| 51 |
-
result_output.visible = False
|
| 52 |
-
test_selector.visible = False
|
| 53 |
-
select_button.visible = False
|
| 54 |
-
return test_name # Возвращаем выбранный тест, чтобы использовать его в дальнейшем
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
for answer, question in zip(answers, questions):
|
| 60 |
-
if answer == question['correct_answer']:
|
| 61 |
-
result_text += "✔️ Ответ верный\n"
|
| 62 |
-
else:
|
| 63 |
-
result_text += f"✖️ {question['incorrect_text']}\n"
|
| 64 |
-
result_output.visible = True
|
| 65 |
-
return result_text
|
| 66 |
|
| 67 |
-
def
|
| 68 |
-
|
| 69 |
-
question_block.visible = False
|
| 70 |
-
question_block.value = ''
|
| 71 |
-
submit_button.visible = False
|
| 72 |
-
back_button.visible = False
|
| 73 |
-
result_output.visible = False
|
| 74 |
-
test_selector.visible = True
|
| 75 |
-
select_button.visible = True
|
| 76 |
-
return "", "", "" # Сбросить все выходные данные
|
| 77 |
|
| 78 |
-
|
| 79 |
-
submit_button.click(fn=check, inputs=question_blocks + [test_selector], outputs=[result_output])
|
| 80 |
-
back_button.click(fn=go_back, inputs=[], outputs=[result_output, test_selector, select_button])
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
|
| 4 |
+
# JSON с вопросами, правильными ответами и текстом при неправильном ответе
|
| 5 |
json_data = """
|
| 6 |
{
|
| 7 |
+
"questions": [
|
| 8 |
+
{"text": "Выберите А", "correct_answer": "А", "wrong_answer_text": "Неправильно. Правильный ответ: А"},
|
| 9 |
+
{"text": "Выберите Б", "correct_answer": "Б", "wrong_answer_text": "Неправильно. Правильный ответ: Б"}
|
| 10 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
}
|
| 12 |
"""
|
| 13 |
|
| 14 |
+
# Загрузка данных из JSON
|
| 15 |
data = json.loads(json_data)
|
| 16 |
|
| 17 |
+
# functions
|
| 18 |
+
def check(answers, questions_data):
|
| 19 |
+
results = []
|
| 20 |
+
for i, (answer, question_data) in enumerate(zip(answers, questions_data), start=1):
|
| 21 |
+
if answer == question_data['correct_answer']:
|
| 22 |
+
result = f"{i}: ✔"
|
| 23 |
+
else:
|
| 24 |
+
result = f"{i}: ✖️ ({question_data['wrong_answer_text']})"
|
| 25 |
+
results.append(result)
|
| 26 |
+
|
| 27 |
+
return "\n".join(results)
|
| 28 |
+
|
| 29 |
+
# Список вопросов
|
| 30 |
+
questions_data = data['questions']
|
| 31 |
+
|
| 32 |
+
# css
|
| 33 |
css = """
|
| 34 |
footer {visibility: hidden !important;}
|
| 35 |
"""
|
| 36 |
|
| 37 |
+
# ui
|
| 38 |
+
with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
| 39 |
+
question_blocks = []
|
| 40 |
+
for i, question in enumerate(data['questions'], start=1):
|
| 41 |
+
with gr.Row():
|
| 42 |
+
with gr.Column():
|
| 43 |
+
radio = gr.Radio(label=f"Вопрос {i}", choices=["А", "Б", "В"], info=question['text'])
|
| 44 |
+
question_blocks.append(radio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
text_button = gr.Button("Проверить", variant='primary')
|
| 47 |
+
with gr.Tab("Результаты"):
|
| 48 |
+
text_output = gr.Markdown("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
def on_click(*args):
|
| 51 |
+
return check(args, questions_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
text_button.click(on_click, inputs=question_blocks, outputs=[text_output], queue=False)
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
#end
|
| 56 |
+
vui.launch()
|