import json import os import gradio as gr from matplotlib import pyplot as plt from pymongo_get_database import MongoDBManager from utils import process_markdown, save_answer, randomly_select_prompts, generate_unassisted_question, \ generate_assisted_question, randomize_questions from experiment_details import problem_topics, problems_per_topic, writing_skills_questions from questionnaire import generate_questionnaire from data import problems from datetime import datetime from data_storage import store_user_data import uuid mongo_manager = MongoDBManager() user_id = str(uuid.uuid4()) mongo_manager.create_user_record(user_id) # radio button and button rendering: https://www.gradio.app/docs/radio questionnaire_results = { 'Demographic Details': {}, 'User Capabilities': {}, 'Relationship to System': {}, 'System Output Preferences': {} } question_answers = { 'q1': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, 'q2': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, 'q3': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, 'q4': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, 'q5': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, 'q6': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, 'q7': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, 'q8': { 'Response': None, 'Assistance': None, 'CompletionTime': None, 'StartTime': None, 'EndTime': None }, } with gr.Blocks(title="wrAIte") as demo: with gr.Column("Survey Complete", render=True, visible=False) as completed_screen: instr_complete = process_markdown(False, 'instr_completed.md') survey_complete = gr.Markdown(value=instr_complete) questionnaire = generate_questionnaire(questionnaire_results, completed_screen, user_id) questionnaire.render() with gr.Column("", render=True, visible=False) as assessment: selected_questions = randomly_select_prompts() question_prompts = randomize_questions(selected_questions) def next_q8(): question_answers['q8']['EndTime'] = datetime.now().isoformat() question_answers['q8']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q8']['EndTime']) - datetime.fromisoformat(question_answers['q8']['StartTime'])) store_user_data(user_id, "questions", question_answers) mongo_manager.update_question_answers(user_id, question_answers) return { question8: gr.update(visible=False), questionnaire: gr.update(visible=True) } def next_q7(): question_answers['q7']['EndTime'] = datetime.now().isoformat() question_answers['q7']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q7']['EndTime']) - datetime.fromisoformat(question_answers['q7']['StartTime'])) question_answers['q8']['StartTime'] = datetime.now().isoformat() return { question7: gr.update(visible=False), question8: gr.update(visible=True) } def next_q6(): question_answers['q6']['EndTime'] = datetime.now().isoformat() question_answers['q6']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q6']['EndTime']) - datetime.fromisoformat(question_answers['q6']['StartTime'])) question_answers['q7']['StartTime'] = datetime.now().isoformat() return { question6: gr.update(visible=False), question7: gr.update(visible=True) } def next_q5(): question_answers['q5']['EndTime'] = datetime.now().isoformat() question_answers['q5']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q5']['EndTime']) - datetime.fromisoformat(question_answers['q5']['StartTime'])) question_answers['q6']['StartTime'] = datetime.now().isoformat() return { question5: gr.update(visible=False), question6: gr.update(visible=True) } def next_q4(): question_answers['q4']['EndTime'] = datetime.now().isoformat() question_answers['q4']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q4']['EndTime']) - datetime.fromisoformat(question_answers['q4']['StartTime'])) question_answers['q5']['StartTime'] = datetime.now().isoformat() return { question4: gr.update(visible=False), question5: gr.update(visible=True) } def next_q3(): question_answers['q3']['EndTime'] = datetime.now().isoformat() question_answers['q3']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q3']['EndTime']) - datetime.fromisoformat(question_answers['q3']['StartTime'])) question_answers['q4']['StartTime'] = datetime.now().isoformat() return { question3: gr.update(visible=False), question4: gr.update(visible=True) } def next_q2(): question_answers['q2']['EndTime'] = datetime.now().isoformat() question_answers['q2']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q2']['EndTime']) - datetime.fromisoformat(question_answers['q2']['StartTime'])) question_answers['q3']['StartTime'] = datetime.now().isoformat() return { question2: gr.update(visible=False), question3: gr.update(visible=True) } def next_q1(): question_answers['q1']['EndTime'] = datetime.now().isoformat() question_answers['q1']['CompletionTime'] = str(datetime.fromisoformat(question_answers['q1']['EndTime']) - datetime.fromisoformat(question_answers['q1']['StartTime'])) question_answers['q2']['StartTime'] = datetime.now().isoformat() return { question1: gr.update(visible=False), question2: gr.update(visible=True) } with gr.Column("Question 8", render=True, visible=False) as question8: next_q8_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q8 = generate_assisted_question(question_prompts[7], next_q8_btn, 8, question_answers) q8.render() next_q8_btn.render() next_q8_btn.click( fn=next_q8, inputs=[], outputs=[question8, questionnaire] ) with gr.Column("Question 7", render=True, visible=False) as question7: next_q7_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q7 = generate_unassisted_question(question_prompts[6], next_q7_btn, 7, question_answers) q7.render() next_q7_btn.render() next_q7_btn.click( fn=next_q7, inputs=[], outputs=[question7, question8] ) with gr.Column("Question 6", render=True, visible=False) as question6: next_q6_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q6 = generate_unassisted_question(question_prompts[5], next_q6_btn, 6, question_answers) q6.render() next_q6_btn.render() next_q6_btn.click( fn=next_q6, inputs=[], outputs=[question6, question7] ) with gr.Column("Question 5", render=True, visible=False) as question5: next_q5_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q5 = generate_unassisted_question(question_prompts[4], next_q5_btn, 5, question_answers) q5.render() next_q5_btn.render() next_q5_btn.click( fn=next_q5, inputs=[], outputs=[question5, question6] ) with gr.Column("Question 4", render=True, visible=False) as question4: next_q4_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q4 = generate_assisted_question(question_prompts[3], next_q4_btn, 4, question_answers) q4.render() next_q4_btn.render() next_q4_btn.click( fn=next_q4, inputs=[], outputs=[question4, question5] ) with gr.Column("Question 3", render=True, visible=False) as question3: next_q3_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q3 = generate_unassisted_question(question_prompts[2], next_q3_btn, 3, question_answers) q3.render() next_q3_btn.render() next_q3_btn.click( fn=next_q3, inputs=[], outputs=[question3, question4] ) with gr.Column("Question 2", render=True, visible=False) as question2: next_q2_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q2 = generate_assisted_question(question_prompts[1], next_q2_btn, 2, question_answers) q2.render() next_q2_btn.render() next_q2_btn.click( fn=next_q2, inputs=[], outputs=[question2, question3] ) with gr.Column("Question 1", render=True, visible=True) as question1: next_q1_btn = gr.Button("Next Question", render=False, visible=False, variant="primary") q1 = generate_unassisted_question(question_prompts[0], next_q1_btn, 1, question_answers) q1.render() next_q1_btn.render() next_q1_btn.click( fn=next_q1, inputs=[], outputs=[question1, question2] ) def start_assessment(): question_answers['q1']['StartTime'] = datetime.now().isoformat() return { welcome_pg: gr.update(visible=False), assessment: gr.update(visible=True) } with gr.Column("Welcome to wrAIte", visible=True) as welcome_pg: welcome_instr = process_markdown(False, 'instr_welcome.md') welcome_text = gr.Markdown(value=welcome_instr, visible=True) welcome_continue_btn = gr.Button("Continue", variant="primary", visible=True) study_instr = process_markdown(False, 'instr_study.md') study_text = gr.Markdown(value=study_instr, visible=False) study_continue_btn = gr.Button("Begin Study", variant="primary", visible=False) def show_study_instructions(): return { welcome_text: gr.update(visible=False), welcome_continue_btn: gr.update(visible=False), study_text: gr.update(visible=True), study_continue_btn: gr.update(visible=True), } welcome_continue_btn.click( fn=show_study_instructions, inputs=[], outputs=[welcome_text, welcome_continue_btn, study_text, study_continue_btn] ) study_continue_btn.click( fn=start_assessment, inputs=[], outputs=[welcome_pg, assessment] ) # if __name__ == "__main__": # demo.launch() demo.launch(share=True)