import gradio as gr import pandas as pd import os from huggingface_hub import HfApi, HfFolder from dotenv import load_dotenv import random import threading import time import boto3 from botocore.config import Config file_lock = threading.Lock() def create_excel_if_missing(filename: str): """Create an empty Excel file with the required columns if it does not exist.""" with file_lock: if not os.path.exists(filename): # Define all required columns columns = [ "ProlificID", # Unique ID for participants "PHQ9", # Total score for PHQ-9 "GAD7", # Total score for GAD-7 "AttentionCheck1", # First attention check pass/fail "AttentionCheck2", # Second attention check pass/fail # ASQ scenario responses (long format) "ScenarioIndex", # Scenario number (1-12) "ScenarioText", # Full scenario text "Cause", # Participant's response for cause # Attribution Dimensions "Internal-External", # Scale from 1 (External) to 10 (Internal) "Stable-Unstable", # Scale from 1 (Unstable) to 10 (Stable) "Global-Specific", # Scale from 1 (Specific) to 10 (Global) # Emotional Response "EmotionalRelevance", # Scale from 1 (None) to 10 (A lot) "PositiveValence", # Scale from 1 (None) to 10 (A lot) "NegativeValence", # Scale from 1 (None) to 10 (A lot) # Usability Ratings "EaseOfUse", # 1-10 scale "ClarityOfInstr", # 1-10 scale "Engagement", # 1-10 scale "Satisfaction", # 1-10 scale "CognitiveLoad" # 1-10 scale ] # Create an empty DataFrame with these columns empty_df = pd.DataFrame(columns=columns) # Save it to an Excel file empty_df.to_excel(filename, index=False) print(f"Created empty Excel file: {filename}") def save_to_excel(df, user_state): """ Saves the user's data into a unique Excel file named: Cond3__.xlsx If a file with that name already exists (e.g., user revisits or calls this function multiple times quickly), it merges the new data with the existing file. Afterwards, uploads the file to an S3-compatible bucket (e.g., Hetzner) under "Cond3/". """ # 1) Grab the unique local filename local_file = user_state.get("excel_file") if not local_file: print("Error: No user_state['excel_file'] found.") return # 2) Read S3 credentials and details from your environment s3_access_key = os.getenv("HETZNER_ACCESS_KEY_ID") s3_secret_key = os.getenv("HETZNER_SECRET_ACCESS_KEY") s3_endpoint = os.getenv("HETZNER_S3_ENDPOINT") bucket_name = os.getenv("HETZNER_BUCKET_NAME") try: with file_lock: # 3) Merge existing data if the file already exists if os.path.exists(local_file): existing = pd.read_excel(local_file, engine="openpyxl") merged = pd.concat([existing, df], ignore_index=True) else: merged = df # 4) Save merged data locally merged.to_excel(local_file, index=False, engine="openpyxl") print(f"Local file updated: {local_file}") # 5) Upload to S3 only if credentials/bucket are present if s3_access_key and s3_secret_key and s3_endpoint and bucket_name: my_config = Config( signature_version="s3v4", s3={"addressing_style": "path"} ) s3_client = boto3.client( "s3", aws_access_key_id=s3_access_key, aws_secret_access_key=s3_secret_key, endpoint_url=s3_endpoint, config=my_config ) # We'll store it in a 'Cond3' subfolder, using the basename to avoid unwanted path details s3_key = f"Cond3/{os.path.basename(local_file)}" # 6) Upload the file s3_client.upload_file(local_file, bucket_name, s3_key) print(f"Uploaded {local_file} to S3 bucket '{bucket_name}' at key: {s3_key}") else: print("No S3 credentials or bucket provided, skipping S3 upload.") except Exception as e: print(f"Error in save_to_excel: {e}") raise ############################################################################### # Define your 12 ASQ Scenarios ############################################################################### ASQ_SCENARIOS = [ # Good Outcomes (Achievement) "You become very rich.", "You apply for a position that you want very badly (important job...) and you get it.", "You get a raise.", # Good Outcomes (Affiliation) "You meet a friend who compliments you on your appearance.", "You do a project that is highly praised.", "Your partner has been treating you more lovingly.", # Bad Outcomes (Achievement) "You have been looking for a job unsuccessfully for some time.", "You give an important talk, and the audience reacts negatively.", "You can’t get all the work done that others expect of you.", # Bad Outcomes (Affiliation) "A friend comes to you with a problem, and you don’t try to help.", "You meet a friend who acts hostilely toward you.", "You go out on a date, and it goes badly." ] def get_default_state(): state = { "prolific_id": None, "phq_score": 0, "gad_score": 0, # We'll store scenario data in a list of dicts "asq_responses": [], "usability": { "Ease of Use": None, "Clarity": None, "Engagement": None, "Satisfaction": None, "Cognitive Load": None }, "current_scenario_index": 0, "Attention Check 1": None, "Attention Check 2": None } random.shuffle(ASQ_SCENARIOS) return state ############################################################################### # 1) PAGE 1: Briefing & Prolific ID ############################################################################### def store_pid(pid_input, user_state): pid = pid_input.strip() if not pid: return "Please enter a valid Prolific ID", user_state, gr.update(visible=True), gr.update(visible=False) user_state["prolific_id"] = pid # Generate a single file name for this user, used throughout the session timestamp = time.strftime("%Y%m%d_%H%M%S") user_state["excel_file"] = f"Cond3_{pid}_{timestamp}.xlsx" return ( pid, user_state, gr.update(visible=False), # Hide page_1 gr.update(visible=True) # Show page_2 ) ############################################################################### # 2) PAGE 2: PHQ-9 ############################################################################### def process_phq9(q1,q2,q3,q4,q5,q6,q7,q8, user_state): # Convert them from str to int scores = [int(x) if x is not None else 0 for x in (q1,q2,q3,q4,q5,q6,q7,q8)] total_phq = sum(scores) user_state["phq_score"] = total_phq # Move to GAD-7 return ( f"PHQ-9 complete, score={total_phq}. Next is GAD-7.", user_state, gr.update(visible=False), # Hide page_2 gr.update(visible=True) # Show page_3 ) ############################################################################### # 3) PAGE 3: GAD-7 ############################################################################### def process_gad7(g1,g2,g3,g4,g5,g6,g7, user_state): scores = [int(x) if x is not None else 0 for x in (g1,g2,g3,g4,g5,g6,g7)] total_gad = sum(scores) user_state["gad_score"] = total_gad # Move on, check if user is eligible combined = user_state["phq_score"] + user_state["gad_score"] if combined>25: return ( "Sorry, your combined score indicates you do not meet the inclusion criteria. Please click the following link to return to Prolific: https://app.prolific.com/submissions/complete?cc=C11ALNAE", user_state, gr.update(visible=True), # page_3 gr.update(visible=False) # We won't show next pages ) return ( f"GAD-7 complete (score={total_gad}). Proceeding to ASQ items...", user_state, gr.update(visible=False), gr.update(visible=True) ) ############################################################################### # 4) PAGE 4: ASQ Items (12 scenarios) ############################################################################### def show_asq_item(user_state): idx = user_state["current_scenario_index"] scenario = ASQ_SCENARIOS[idx] instructions = f""" Instructions: 1) Read each situation carefully and imagine it happening to you. 2) Decide what you believe would be the major cause of the situation if it happened to you. 3) Write this cause in the blank provided. 4) Answer the questions about the cause by moving the sliders. 5) Go on to the next situation. *Scenario {idx+1} of {len(ASQ_SCENARIOS)}* \nPlease imagine the following situation: **{scenario}** """ return instructions def submit_asq_item( cause_text, internal_slider, stable_slider, global_slider, emotional_relevance, positive_valence, negative_valence, user_state, attention_check_slider ): idx = user_state["current_scenario_index"] scenario = ASQ_SCENARIOS[idx] # Now, if we reach this point, we're processing the submission (or attention check response) # If it's an attention check scenario, record the pass/fail: if idx in [3, 7]: if int(attention_check_slider) == 10: if idx == 3: user_state["Attention Check 1"] = True else: user_state["Attention Check 2"] = True else: if idx == 3: user_state["Attention Check 1"] = False else: user_state["Attention Check 2"] = False # For non-attention check scenarios, validate that 'cause_text' isn't empty. if not cause_text.strip(): error_msg = "**Error:** Please provide a cause before proceeding." scenario_text = show_asq_item(user_state) + f"\n\n{error_msg}" return ( scenario_text, user_state, gr.update(visible=True), gr.update(visible=False), cause_text, # Keep existing text internal_slider, stable_slider, global_slider, emotional_relevance, positive_valence, negative_valence, gr.update(visible=False, value=1) ) # Store user input for the scenario (applies to both attention check and normal scenarios) user_state["asq_responses"].append({ "ScenarioIndex": idx + 1, "ScenarioText": scenario, "Cause": cause_text, "Internal-External": internal_slider, "Stable-Unstable": 11 - stable_slider, "Global-Specific": global_slider, "EmotionalRelevance": emotional_relevance, "PositiveValence": positive_valence, "NegativeValence": negative_valence }) df = pd.DataFrame([{ "ProlificID": user_state["prolific_id"], "ScenarioIndex": idx + 1, "ScenarioText": scenario, "Cause": cause_text, "Internal-External": internal_slider, "Stable-Unstable": 11 - stable_slider, "Global-Specific": global_slider, "EmotionalRelevance": emotional_relevance, "PositiveValence": positive_valence, "NegativeValence": negative_valence, "AttentionCheck1": user_state["Attention Check 1"], "AttentionCheck2": user_state["Attention Check 2"] }]) save_to_excel(df, user_state) print("Row successfully added to file!") # Advance the scenario index user_state["current_scenario_index"] += 1 # If we finished all scenarios: if user_state["current_scenario_index"] >= len(ASQ_SCENARIOS): return ( "Thank you for completing the scenarios. Please also answer the following questions.", user_state, gr.update(visible=False), # Hide page_4 gr.update(visible=True), # Show page_5 "", 1,1,1,1,1,1, gr.update(visible=False, value=1) # Hide attention_check_slider ) else: # There's a next scenario. Show or hide the slider depending on if it's #4 or #8 next_idx = user_state["current_scenario_index"] show_slider = (next_idx in [3, 7]) # scenario #4 or #8 next_msg = show_asq_item(user_state) return ( next_msg, user_state, gr.update(visible=True), # keep page_4 visible gr.update(visible=False), # hide page_5 "", 1,1,1,1,1,1, # reset text box & main sliders gr.update(visible=show_slider, value=1) ) ############################################################################### # 5) PAGE 5: Usability ############################################################################### def submit_usability(ease, clarity, engagement, satisfaction, cogload, comments, user_state): # Store usability ratings user_state["usability"]["Ease of Use"] = ease user_state["usability"]["Clarity"] = clarity user_state["usability"]["Engagement"] = engagement user_state["usability"]["Satisfaction"] = satisfaction user_state["usability"]["Cognitive Load"] = cogload user_state["usability"]["Additional Comments"] = comments # Build a DataFrame from asq_responses df = pd.DataFrame(user_state["asq_responses"]) df["ProlificID"] = user_state["prolific_id"] df["PHQ9"] = user_state["phq_score"] df["GAD7"] = user_state["gad_score"] df["AttentionCheck1"] = user_state["Attention Check 1"] df["AttentionCheck2"] = user_state["Attention Check 2"] # Usability data df["EaseOfUse"] = ease df["ClarityOfInstr"] = clarity df["Engagement"] = engagement df["Satisfaction"] = satisfaction df["CognitiveLoad"] = cogload df["Additional Comments"] = comments # Save partial data save_to_excel(df, user_state) return ( "Your responses have been saved. Please use the following link to submit your results and return to Prolific: https://app.prolific.com/submissions/complete?cc=C1ECU6HA", user_state ) ############################################################################### # BUILD THE GRADIO APP ############################################################################### with gr.Blocks(theme= gr.themes.Glass()) as cond3_app: user_state = gr.State(get_default_state()) ############################################################################ # Page 1: Briefing & ID ############################################################################ with gr.Row(visible=True) as page_1: with gr.Column(): gr.Markdown(""" # **Information for Participants and Declaration of Consent** ## **Evaluating the Validity of LLM-Based Psychological Assessments** --- ## **Dear Participant,** ### We invite you to participate in the study described above. Your participation is entirely voluntary. You may refuse to participate or withdraw at any time without needing to provide a reason, and doing so will have no negative consequences for you. ### This study is designed to contribute to academic research by exploring a novel type of psychological assessment using advanced AI technology. ### Please read the following information carefully before deciding whether to participate. You may proceed only after acknowledging that you understand the study and your rights as a participant. --- ## **Purpose of the Study** ### This study aims to evaluate whether an AI-based psychological assessment can provide insights into cognitive patterns comparable to traditional methods. --- ## **Study Procedure** ### - The study consists of three online sessions. Each session lasts approximately 20–30 minutes. The assessment will be conducted over the course of one week with three days in between each session. You must complete all three sessions to receive full compensation. ### - You will reflect on various scenarios and provide feedback on your experience. --- ## **Potential Benefits** ### While there are no direct personal benefits, your participation will contribute to research that could help improve psychological diagnostic tools using AI. --- ## **Potential Risks** ### - The study presents minimal risks. ### - Some scenarios may ask you to reflect on personal responsibility, which could cause minor emotional discomfort. ### - If you feel uncomfortable, you can withdraw at any time. ## **Eligibility Criteria:** ### - Participants must be 18 or older, fluent in English, and score below the clinical threshold on pre-screening mental health questionnaires. ### - Participants with diagnosed mental health disorders or high scores on these questionnaires are excluded. --- ## **Data Handling and Privacy** ### - Your data will be anonymised and used only for statistical purposes. ### - No personal identifying information will be collected. ### - The data will be securely stored and accessed only by authorised study personnel. --- ## **Reimbursement** ### - You will not incur any costs for participating. ### - Compensation will be provided through Prolific based on the time spent participating. ### - You will receive a partial completion code after each session, but full payment will only be processed upon completing all three sessions. --- ## **Withdrawal and Contact Information** ### - You may withdraw at any time without providing a reason. ### - If you experience any discomfort or have questions about the study, please contact the coordinators: #### - **Nace Mikus**: nace.mikus@univie.ac.at #### - **Jakob Möller**: a12028569@unet.univie.ac.at --- ## **Acknowledgment** ### By clicking "I Acknowledge and Consent" you confirm that: ### - You have read and understood the information provided. ### - You are aware of your right to withdraw at any time without consequences. ### - You agree to participate in the study and for your anonymised data to be used for academic research. --- ### Please enter your Prolific ID before moving on: """) pid_in = gr.Textbox(label="Prolific ID (required)") pid_out = gr.Markdown() pid_button = gr.Button("I Acknowledge and Consent") ############################################################################ # Page 2: PHQ-9 ############################################################################ with gr.Row(visible=False) as page_2: with gr.Column(): gr.Markdown("""#### Over the last 2 weeks, how often have you been bothered by #### any of the following problems (0 = "Never", 3 = "Very often")?""") q1 = gr.Radio(["0","1","2","3"], label="Little interest or pleasure in doing things?") q2 = gr.Radio(["0","1","2","3"], label="Feeling down, depressed, or hopeless?") q3 = gr.Radio(["0","1","2","3"], label="Trouble falling or staying asleep?") q4 = gr.Radio(["0","1","2","3"], label="Feeling tired or little energy?") q5 = gr.Radio(["0","1","2","3"], label="Poor appetite or overeating?") q6 = gr.Radio(["0","1","2","3"], label="Feeling bad about yourself?") q7 = gr.Radio(["0","1","2","3"], label="Trouble concentrating on things?") q8 = gr.Radio(["0","1","2","3"], label="Moving or speaking slowly or fidgety?") phq_out = gr.Markdown() phq_next = gr.Button("Next") ############################################################################ # Page 3: GAD-7 ############################################################################ with gr.Row(visible=False) as page_3: with gr.Column(): gr.Markdown("""#### Over the last 2 weeks, how often have you been bothered by #### any of the following problems (0 = "Never", 3 = "Very often")?""") g1 = gr.Radio(["0","1","2","3"], label="Feeling nervous, anxious, on edge?") g2 = gr.Radio(["0","1","2","3"], label="Not being able to stop worrying?") g3 = gr.Radio(["0","1","2","3"], label="Worrying too much about different things?") g4 = gr.Radio(["0","1","2","3"], label="Trouble relaxing?") g5 = gr.Radio(["0","1","2","3"], label="Being so restless it's hard to sit still?") g6 = gr.Radio(["0","1","2","3"], label="Becoming easily annoyed or irritable?") g7 = gr.Radio(["0","1","2","3"], label="Feeling afraid something awful might happen?") gad_out = gr.Markdown() gad_next = gr.Button("Next") ############################################################################ # Page 4: ASQ items ############################################################################ with gr.Row(visible=False) as page_4: with gr.Column(): item_instructions = gr.Markdown("", elem_id="scenario_instructions") cause_box = gr.Textbox(label="Write down the major cause:") internal_slider = gr.Slider(1,10,step=0.1,value=1,label="Is the cause due to something about you or something about other people or circumstances? (1 = Totally due to me, 10 = Totally due to other people)") stable_slider = gr.Slider(1,10,step=0.1,value=1,label="In the future, will this cause again be present? (1 = Will never be present, 10 = Will always be present)") global_slider = gr.Slider(1,10,step=0.1,value=1,label="Is the cause something that just affects this situation or does it also influence other areas of your life? (1 = Influences just this situation, 10 = Influences all situations)") emotional_relevance = gr.Slider(1,10,step=0.1,value=1,label="How much emotional impact did this scenario have on you? (1 = None, 10 = A lot)") positive_valence = gr.Slider(1,10,step=0.1,value=1,label="How much joy, excitement, or satisfaction did this scenario evoke in you? (1 = None, 10 = A lot)") negative_valence = gr.Slider(1,10,step=0.1,value=1,label="How much discomfort or tension did you feel while imagining this scenario? (1 = None, 10 = A lot)") attention_check_slider = gr.Slider( minimum=1, maximum=10, step=1, value=1, label="ATTENTION CHECK: Please select 10 here (option on the very right) to indicate you're paying attention.", visible=False ) asq_out = gr.Markdown() asq_button = gr.Button("Next Scenario") # On first load, show item 1 item_instructions.value = show_asq_item(user_state.value) ############################################################################ # Page 5: Usability ############################################################################ with gr.Row(visible=False) as page_5: with gr.Column(): gr.Markdown("## Usability Questions") ease_box = gr.Slider(1,10,step=0.1,value=1,label="How easy was it to complete the assessment? (1 = Not at all, 10 = Very much)") clarity_box = gr.Slider(1,10,step=0.1,value=1,label="How clear were the instructions? (1 = Not at all, 10 = Very much)") engagement_box = gr.Slider(1,10,step=0.1,value=1,label="How engaging was the assessment experience? (1 = Not at all, 10 = Very much)") satisfaction_box = gr.Slider(1,10,step=0.1,value=1,label="How satisfied are you with this assessment? (1 = Not at all, 10 = Very much)") cogload_box = gr.Slider(1,10,step=0.1,value=1,label="How mentally demanding was the assessment? (1 = Not at all, 10 = Very much)") comments_box = gr.Textbox(label="Additional Comments", placeholder="Any further comments or suggestions?", lines=2) usability_out = gr.Markdown() usability_button = gr.Button("Finish") usability_button.click( fn=submit_usability, inputs=[ease_box, clarity_box, engagement_box, satisfaction_box, cogload_box, comments_box, user_state], outputs=[usability_out, user_state] ) # Transition Logic ######################################################### pid_button.click( fn=store_pid, inputs=[pid_in, user_state], outputs=[pid_out, user_state, page_1, page_2] ) phq_next.click( fn=process_phq9, inputs=[q1,q2,q3,q4,q5,q6,q7,q8, user_state], outputs=[phq_out, user_state, page_2, page_3] ) gad_next.click( fn=process_gad7, inputs=[g1,g2,g3,g4,g5,g6,g7, user_state], outputs=[gad_out, user_state, page_3, page_4] ) asq_button.click( fn=submit_asq_item, inputs=[ cause_box, internal_slider, stable_slider, global_slider, emotional_relevance, positive_valence, negative_valence, user_state, attention_check_slider ], outputs=[ item_instructions, user_state, page_4, page_5, cause_box, internal_slider, stable_slider, global_slider, emotional_relevance, positive_valence, negative_valence, attention_check_slider ] ) cond3_app.launch()