from pymongo_get_database import MongoDBManager from utils import process_markdown, save_answer, randomly_select_prompts, generate_unassisted_question from experiment_details import problem_topics, problems_per_topic, writing_skills_questions from data_storage import store_user_data import gradio as gr # mongo_manager = MongoDBManager() def generate_questionnaire(all_results, completed_screen, mongo_manager): global dem_answers dem_answers = {} global usercap_answers usercap_answers = {} global reln_answers reln_answers = {} global pref_answers pref_answers = {} with gr.Column("Questionnaire", render=False, visible=False) as questionnaire: questionnaire_instr_md = process_markdown(False, 'instr_questionnaire.md') questionnaire_instr = gr.Markdown(value=questionnaire_instr_md) def finish_pref(): all_results['System Output Preferences'] = pref_answers store_user_data(mongo_manager.user_id, "questionnaire", all_results) print(mongo_manager.user_id) mongo_manager.update_questionnaire_answers(all_results) return { pref: gr.update(visible=False), completed_screen: gr.update(visible=True), questionnaire_instr: gr.update(visible=False) } with gr.Column("System Output Preferences", render=True, visible=False) as pref: def save_results(question, answer, total_num_questions): pref_answers[question] = answer questions_answered = int(total_num_questions) == len(pref_answers) if questions_answered: return str(pref_answers), questions_answered, gr.Button(interactive=True) else: return str(pref_answers), questions_answered, gr.Button(interactive=False) q_num = 1 pref_questions = writing_skills_questions['System Output Preferences'] total_questions_len = str(len(pref_questions)) with gr.Row(visible=False): radio_outputs = gr.Textbox(label="Choices") all_questions_answered = gr.Textbox(label="All Questions Answered") with gr.Row(render=False) as output_row: finish_pref_btn = gr.Button( "Submit Questionnaire", variant="primary", interactive=False) with gr.Group(): for q in pref_questions: with gr.Row(): # Hidden textbox to hold the question value question_input = gr.Textbox(value=q['Question'], visible=False) total_num_questions = gr.Textbox(value=total_questions_len, visible=False) single_question = gr.Radio( choices=q['AnswerChoices'], label=str(q_num) + '. ' + q['Question'], interactive=True, visible=True ) # Use the hidden textbox as part of the inputs single_question.change( fn=save_results, inputs=[question_input, single_question, total_num_questions], outputs=[radio_outputs, all_questions_answered, finish_pref_btn] ) q_num += 1 output_row.render() finish_pref_btn.click( fn=finish_pref, inputs=[], outputs=[pref, completed_screen, questionnaire_instr] ) def finish_reln(): all_results['Relationship to System'] = reln_answers return { reln: gr.update(visible=False), pref: gr.update(visible=True) } with gr.Column("Relationship to System", render=True, visible=False) as reln: def save_results(question, answer, total_num_questions): reln_answers[question] = answer questions_answered = int(total_num_questions) == len(reln_answers) if questions_answered: return str(reln_answers), questions_answered, gr.Button(interactive=True) else: return str(reln_answers), questions_answered, gr.Button(interactive=False) q_num = 1 reln_questions = writing_skills_questions['Relationship to System'] total_questions_len = str(len(reln_questions)) with gr.Row(visible=False): radio_outputs = gr.Textbox(label="Choices") all_questions_answered = gr.Textbox(label="All Questions Answered") with gr.Row(render=False) as output_row: finish_reln_btn = gr.Button( "Next", variant="primary", interactive=False) with gr.Group(): for q in reln_questions: with gr.Row(): # Hidden textbox to hold the question value question_input = gr.Textbox(value=q['Question'], visible=False) total_num_questions = gr.Textbox(value=total_questions_len, visible=False) single_question = gr.Radio( choices=q['AnswerChoices'], label=str(q_num) + '. ' + q['Question'], interactive=True, visible=True ) # Use the hidden textbox as part of the inputs single_question.change( fn=save_results, inputs=[question_input, single_question, total_num_questions], outputs=[radio_outputs, all_questions_answered, finish_reln_btn] ) q_num += 1 output_row.render() finish_reln_btn.click( fn=finish_reln, inputs=[], outputs=[reln, pref] ) def finish_usercap(): all_results['User Capabilities'] = usercap_answers return { usercap: gr.update(visible=False), reln: gr.update(visible=True) } with gr.Column("User Capabilities", render=True, visible=False) as usercap: def save_results(question, answer, total_num_questions): usercap_answers[question] = answer questions_answered = int(total_num_questions) == len(usercap_answers) if questions_answered: return str(usercap_answers), questions_answered, gr.Button(interactive=True) else: return str(usercap_answers), questions_answered, gr.Button(interactive=False) q_num = 1 usercap_questions = writing_skills_questions['User Capabilities'] total_questions_len = str(len(usercap_questions)) with gr.Row(visible=False): radio_outputs = gr.Textbox(label="Choices") all_questions_answered = gr.Textbox(label="All Questions Answered") with gr.Row(render=False) as output_row: finish_usercap_btn = gr.Button( "Next", variant="primary", interactive=False) with gr.Group(): for q in usercap_questions: with gr.Row(): # Hidden textbox to hold the question value question_input = gr.Textbox(value=q['Question'], visible=False) total_num_questions = gr.Textbox(value=total_questions_len, visible=False) single_question = gr.Radio( choices=q['AnswerChoices'], label=str(q_num) + '. ' + q['Question'], interactive=True, visible=True ) # Use the hidden textbox as part of the inputs single_question.change( fn=save_results, inputs=[question_input, single_question, total_num_questions], outputs=[radio_outputs, all_questions_answered, finish_usercap_btn] ) q_num += 1 output_row.render() finish_usercap_btn.click( fn=finish_usercap, inputs=[], outputs=[usercap, reln] ) def finish_dem(): all_results['Demographic Details'] = dem_answers return { dem: gr.update(visible=False), usercap: gr.update(visible=True) } with gr.Column("Demographic", render = True, visible=False) as dem: def save_results(question, answer, total_num_questions): dem_answers[question] = answer questions_answered = int(total_num_questions) == len(dem_answers) if questions_answered: return str(dem_answers), questions_answered, gr.Button(interactive=True) else: return str(dem_answers), questions_answered, gr.Button(interactive=False) q_num = 1 dem_questions = writing_skills_questions['Demographic Details'] total_questions_len = str(len(dem_questions)) with gr.Row(visible=False): radio_outputs = gr.Textbox(label="Choices") all_questions_answered = gr.Textbox(label="All Questions Answered") with gr.Row(render=False) as output_row: finish_dem_btn = gr.Button( "Next", variant="primary", interactive=False) with gr.Group(): for q in dem_questions: with gr.Row(): # Hidden textbox to hold the question value question_input = gr.Textbox(value=q['Question'], visible=False) total_num_questions = gr.Textbox(value=total_questions_len, visible=False) single_question = gr.Radio( choices=q['AnswerChoices'], label=str(q_num) + '. ' + q['Question'], interactive=True, visible=True ) # Use the hidden textbox as part of the inputs single_question.change( fn=save_results, inputs=[question_input, single_question, total_num_questions], outputs=[radio_outputs, all_questions_answered, finish_dem_btn] ) q_num += 1 output_row.render() finish_dem_btn.click( fn=finish_dem, inputs=[], outputs=[dem, usercap] ) def begin_questionnaire(): global dem_answers dem_answers = {} global usercap_answers usercap_answers = {} global reln_answers reln_answers = {} global pref_answers pref_answers = {} return { welcome: gr.update(visible=False), dem: gr.update(visible=True) } with gr.Column("Welcome", render=True) as welcome: begin_questionnaire_btn = gr.Button( "Begin Questionnaire", variant="primary", interactive=True) begin_questionnaire_btn.click( fn=begin_questionnaire, inputs=[], outputs=[welcome, dem] ) return questionnaire