Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
# JSON с тестами, вопросами, правильными ответами и текстом при неправильном ответе
|
| 4 |
json_data = """
|
| 5 |
{
|
| 6 |
"tests": {
|
|
@@ -20,67 +20,40 @@ json_data = """
|
|
| 20 |
}
|
| 21 |
"""
|
| 22 |
|
| 23 |
-
|
| 24 |
-
data = gr.Interface.load(json_data)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
return test_data, test_name
|
| 30 |
|
| 31 |
def check(answers, test_name):
|
| 32 |
-
|
| 33 |
results = []
|
| 34 |
-
for
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
# css
|
| 40 |
css = """
|
| 41 |
footer {visibility: hidden !important;}
|
| 42 |
"""
|
| 43 |
|
| 44 |
-
# ui
|
| 45 |
with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
| 46 |
with gr.Row():
|
| 47 |
test_selector = gr.Radio(label="Выберите тест", choices=list(data['tests'].keys()))
|
| 48 |
select_button = gr.Button("Выбрать")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
question_blocks = [
|
| 54 |
-
gr.Radio(label=question['text'], choices=["А", "Б", "В"], info=question['text'])
|
| 55 |
-
for question in data['tests'][test_name]['questions']
|
| 56 |
-
]
|
| 57 |
-
submit_button = gr.Button("Проверить")
|
| 58 |
-
back_button = gr.Button("Назад")
|
| 59 |
-
test_tabs[test_name] = (tab, question_blocks, submit_button, back_button)
|
| 60 |
-
|
| 61 |
-
results_tab = gr.Tab("Результаты")
|
| 62 |
-
with results_tab:
|
| 63 |
-
text_output = gr.Markdown("")
|
| 64 |
-
|
| 65 |
-
def on_select(test_data, test_name):
|
| 66 |
-
tab, question_blocks, _, _ = test_tabs[test_name]
|
| 67 |
-
tab.update(visible=True)
|
| 68 |
-
test_selector.update(visible=False)
|
| 69 |
-
select_button.update(visible=False)
|
| 70 |
-
return ([gr.update(value='') for _ in question_blocks], test_name)
|
| 71 |
-
|
| 72 |
-
def on_back(test_name):
|
| 73 |
-
tab, _, _, _ = test_tabs[test_name]
|
| 74 |
-
tab.update(visible=False)
|
| 75 |
-
test_selector.update(visible=True)
|
| 76 |
-
select_button.update(visible=True)
|
| 77 |
-
return gr.update(value='')
|
| 78 |
-
|
| 79 |
-
select_button.click(show_test, inputs=[test_selector], outputs=[gr.Group(*[block for _, blocks, _, _ in test_tabs.values() for block in blocks]), test_selector])
|
| 80 |
-
|
| 81 |
-
for test_name, (tab, question_blocks, submit_button, back_button) in test_tabs.items():
|
| 82 |
-
submit_button.click(check, inputs=question_blocks, outputs=[text_output])
|
| 83 |
-
back_button.click(on_back, inputs=[test_name], outputs=[test_selector])
|
| 84 |
|
| 85 |
-
#end
|
| 86 |
vui.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
|
|
|
|
| 4 |
json_data = """
|
| 5 |
{
|
| 6 |
"tests": {
|
|
|
|
| 20 |
}
|
| 21 |
"""
|
| 22 |
|
| 23 |
+
data = json.loads(json_data)
|
|
|
|
| 24 |
|
| 25 |
+
def show_questions(test_name):
|
| 26 |
+
questions = data['tests'][test_name]['questions']
|
| 27 |
+
return {f"question{i+1}": gr.update(visible=True, label=question['text'], info=question['text']) for i, question in enumerate(questions)}, gr.update(visible=False)
|
|
|
|
| 28 |
|
| 29 |
def check(answers, test_name):
|
| 30 |
+
questions = data['tests'][test_name]['questions']
|
| 31 |
results = []
|
| 32 |
+
for answer, question in zip(answers, questions):
|
| 33 |
+
if answer == question['correct_answer']:
|
| 34 |
+
results.append("✔️")
|
| 35 |
+
else:
|
| 36 |
+
results.append(f"✖️ ({question['incorrect_text']})")
|
| 37 |
+
return "\n".join(results), gr.update(visible=True)
|
| 38 |
+
|
| 39 |
+
def reset(questions):
|
| 40 |
+
return {f"question{i+1}": gr.update(visible=False) for i in range(len(questions))}, gr.update(visible=True)
|
| 41 |
|
|
|
|
| 42 |
css = """
|
| 43 |
footer {visibility: hidden !important;}
|
| 44 |
"""
|
| 45 |
|
|
|
|
| 46 |
with gr.Blocks(css=css, theme='YTheme/TehnoX') as vui:
|
| 47 |
with gr.Row():
|
| 48 |
test_selector = gr.Radio(label="Выберите тест", choices=list(data['tests'].keys()))
|
| 49 |
select_button = gr.Button("Выбрать")
|
| 50 |
+
questions = [gr.Radio(label="", choices=["А", "Б", "В"], visible=False) for _ in range(len(max(data['tests'].values(), key=lambda t: len(t['questions']))['questions']))]
|
| 51 |
+
submit_button = gr.Button("Проверить", visible=False)
|
| 52 |
+
back_button = gr.Button("Назад", visible=False)
|
| 53 |
+
result_output = gr.Markdown(visible=False)
|
| 54 |
|
| 55 |
+
select_button.click(show_questions, inputs=[test_selector], outputs=[questions, select_button])
|
| 56 |
+
submit_button.click(check, inputs=questions + [test_selector], outputs=[result_output, submit_button])
|
| 57 |
+
back_button.click(reset, inputs=[questions], outputs=[questions, test_selector])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
|
|
|
| 59 |
vui.launch()
|