Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ pickle_file = "assessment_data_cond2.pkl"
|
|
| 33 |
if not os.path.exists(excel_file):
|
| 34 |
pd.DataFrame(columns=[
|
| 35 |
'Prolific ID', 'Age', 'Profession', 'Interests', 'Topic Choice',
|
| 36 |
-
'Vignette Number', 'Vignette', 'Tone', 'Vignette Type',
|
| 37 |
'First Reveal', 'Initial Internal',
|
| 38 |
'Initial External', 'Initial Stable', 'Initial Unstable', 'Initial Global',
|
| 39 |
'Initial Specific', 'Reassessment Internal', 'Reassessment External',
|
|
@@ -48,7 +48,7 @@ if not os.path.exists(excel_file):
|
|
| 48 |
if not os.path.exists(pickle_file):
|
| 49 |
pd.DataFrame(columns=[
|
| 50 |
'Prolific ID', 'Age', 'Profession', 'Interests', 'Topic Choice',
|
| 51 |
-
'Vignette Number', 'Vignette', 'Tone', 'Vignette Type',
|
| 52 |
'First Reveal', 'Initial Internal',
|
| 53 |
'Initial External', 'Initial Stable', 'Initial Unstable', 'Initial Global',
|
| 54 |
'Initial Specific', 'Reassessment Internal', 'Reassessment External',
|
|
@@ -205,7 +205,7 @@ def generate_custom_vignette(chat_history, topic_choice, desired_tone, user_stat
|
|
| 205 |
|
| 206 |
# Call the OpenAI API
|
| 207 |
try:
|
| 208 |
-
response = openai.
|
| 209 |
model="gpt-4o",
|
| 210 |
messages=[
|
| 211 |
{"role": "system", "content": "You are an assistant generating tailored psychological assessment scenarios."},
|
|
@@ -280,13 +280,13 @@ def generate_generic_vignette(topic_choice, desired_tone):
|
|
| 280 |
"""
|
| 281 |
|
| 282 |
# Call the OpenAI API
|
| 283 |
-
response = openai.
|
| 284 |
model="gpt-4",
|
| 285 |
messages=[
|
| 286 |
{"role": "system", "content": "You are an assistant generating generic psychological assessment scenarios."},
|
| 287 |
{"role": "user", "content": prompt},
|
| 288 |
],
|
| 289 |
-
temperature=0.
|
| 290 |
max_tokens=100
|
| 291 |
)
|
| 292 |
content = response.choices[0].message.content.strip()
|
|
@@ -447,79 +447,6 @@ def finalize_and_save_to_space(user_state):
|
|
| 447 |
print(f"Error in finalize_and_save_to_space: {e}")
|
| 448 |
raise
|
| 449 |
|
| 450 |
-
|
| 451 |
-
# [CONDITION 2] Added function to generate generic vignette
|
| 452 |
-
def generate_generic_vignette(topic_choice, desired_tone):
|
| 453 |
-
"""
|
| 454 |
-
Generates a generic vignette for psychological assessment, ignoring user profile details.
|
| 455 |
-
"""
|
| 456 |
-
try:
|
| 457 |
-
if topic_choice == "1":
|
| 458 |
-
topic_context = "a general professional scenario"
|
| 459 |
-
elif topic_choice == "2":
|
| 460 |
-
topic_context = "a general personal interest scenario"
|
| 461 |
-
elif topic_choice == "3":
|
| 462 |
-
topic_context = "a general scenario inspired by recent events or activities"
|
| 463 |
-
else:
|
| 464 |
-
topic_context = "a general scenario suitable for any context"
|
| 465 |
-
|
| 466 |
-
# Define tone context
|
| 467 |
-
if desired_tone == "Positive":
|
| 468 |
-
tone_context = "a pleasant and rewarding situation, highlighting success, collaboration, or enjoyable experiences."
|
| 469 |
-
elif desired_tone == "Negative":
|
| 470 |
-
tone_context = "an unpleasant or confrontational situation, involving conflict, challenges, or setbacks."
|
| 471 |
-
else:
|
| 472 |
-
tone_context = "a neutral situation."
|
| 473 |
-
|
| 474 |
-
# Build the prompt
|
| 475 |
-
prompt = f"""
|
| 476 |
-
You are tasked with generating short, generic vignettes for a psychological assessment.
|
| 477 |
-
User profile details (age, profession, interests) must be ignored.
|
| 478 |
-
|
| 479 |
-
Scenario Details:
|
| 480 |
-
- The topic of the vignette is {topic_context}.
|
| 481 |
-
- The tone of the vignette should reflect {tone_context}.
|
| 482 |
-
- The vignette should include:
|
| 483 |
-
- A clear context type (e.g., work, social, physical activity, educational, or creative).
|
| 484 |
-
- A specific interaction (e.g., feedback from authority, peer collaboration, or group dynamics).
|
| 485 |
-
- A defined event or action (e.g., presenting an idea, resolving a conflict, or completing a project).
|
| 486 |
-
|
| 487 |
-
Guidelines:
|
| 488 |
-
- The vignette must describe external events only, without referencing thoughts, emotions, or internal reflections.
|
| 489 |
-
- Use second-person language to immerse the user ("You...").
|
| 490 |
-
- Keep the vignette concise, no longer than 50 tokens.
|
| 491 |
-
- Use neutral and professional language suitable for a psychological assessment.
|
| 492 |
-
- Do not include personal identifying information or references to the user's actual profile.
|
| 493 |
-
|
| 494 |
-
Example:
|
| 495 |
-
Positive Example:
|
| 496 |
-
"You collaborate with a colleague on a shared report.
|
| 497 |
-
They praise your clear communication, and together you finalize the work ahead of schedule."
|
| 498 |
-
|
| 499 |
-
Negative Example:
|
| 500 |
-
"During a community gathering, you run into a disagreement over how to organize an activity.
|
| 501 |
-
The conversation becomes tense, and participants struggle to find common ground."
|
| 502 |
-
|
| 503 |
-
Now generate the vignette:
|
| 504 |
-
"""
|
| 505 |
-
|
| 506 |
-
# Call the OpenAI API
|
| 507 |
-
response = openai.ChatCompletion.create(
|
| 508 |
-
model="gpt-4",
|
| 509 |
-
messages=[
|
| 510 |
-
{"role": "system", "content": "You are an assistant generating generic psychological assessment scenarios."},
|
| 511 |
-
{"role": "user", "content": prompt},
|
| 512 |
-
],
|
| 513 |
-
temperature=0.5,
|
| 514 |
-
max_tokens=100
|
| 515 |
-
)
|
| 516 |
-
content = response.choices[0].message.content.strip()
|
| 517 |
-
return content, desired_tone
|
| 518 |
-
|
| 519 |
-
except Exception as e:
|
| 520 |
-
print(f"Error generating generic vignette: {e}")
|
| 521 |
-
return None, desired_tone
|
| 522 |
-
|
| 523 |
# [CONDITION 2] Function to pick vignette type
|
| 524 |
def pick_vignette_type():
|
| 525 |
return random.choice(["generic", "custom"])
|
|
@@ -738,6 +665,7 @@ def new_chat_input(user_input, chat_history, user_state, *args, **kwargs):
|
|
| 738 |
elif user_state["stage"] == "topic_choice":
|
| 739 |
print("topic_choice block successfully entered")
|
| 740 |
print(f"User entered input: {user_input}")
|
|
|
|
| 741 |
user_state['options_text'] = '1. A situation related to your profession.\n2. A situation related to your personal interests.\n3. Something else about me (Add another activity or interest that is important to you by saying "I also like...")'
|
| 742 |
user_state["user_responses"].append({"stage": "topic_choice", "response": user_input})
|
| 743 |
print(f"user input was: {user_input}, user responses after storing it are: {user_state['user_responses']}")
|
|
|
|
| 33 |
if not os.path.exists(excel_file):
|
| 34 |
pd.DataFrame(columns=[
|
| 35 |
'Prolific ID', 'Age', 'Profession', 'Interests', 'Topic Choice',
|
| 36 |
+
'Vignette Number', 'Vignette', 'Tone', 'Vignette Type',
|
| 37 |
'First Reveal', 'Initial Internal',
|
| 38 |
'Initial External', 'Initial Stable', 'Initial Unstable', 'Initial Global',
|
| 39 |
'Initial Specific', 'Reassessment Internal', 'Reassessment External',
|
|
|
|
| 48 |
if not os.path.exists(pickle_file):
|
| 49 |
pd.DataFrame(columns=[
|
| 50 |
'Prolific ID', 'Age', 'Profession', 'Interests', 'Topic Choice',
|
| 51 |
+
'Vignette Number', 'Vignette', 'Tone', 'Vignette Type',
|
| 52 |
'First Reveal', 'Initial Internal',
|
| 53 |
'Initial External', 'Initial Stable', 'Initial Unstable', 'Initial Global',
|
| 54 |
'Initial Specific', 'Reassessment Internal', 'Reassessment External',
|
|
|
|
| 205 |
|
| 206 |
# Call the OpenAI API
|
| 207 |
try:
|
| 208 |
+
response = openai.chat.completions.create(
|
| 209 |
model="gpt-4o",
|
| 210 |
messages=[
|
| 211 |
{"role": "system", "content": "You are an assistant generating tailored psychological assessment scenarios."},
|
|
|
|
| 280 |
"""
|
| 281 |
|
| 282 |
# Call the OpenAI API
|
| 283 |
+
response = openai.chat.completions.create(
|
| 284 |
model="gpt-4",
|
| 285 |
messages=[
|
| 286 |
{"role": "system", "content": "You are an assistant generating generic psychological assessment scenarios."},
|
| 287 |
{"role": "user", "content": prompt},
|
| 288 |
],
|
| 289 |
+
temperature=0.4,
|
| 290 |
max_tokens=100
|
| 291 |
)
|
| 292 |
content = response.choices[0].message.content.strip()
|
|
|
|
| 447 |
print(f"Error in finalize_and_save_to_space: {e}")
|
| 448 |
raise
|
| 449 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
# [CONDITION 2] Function to pick vignette type
|
| 451 |
def pick_vignette_type():
|
| 452 |
return random.choice(["generic", "custom"])
|
|
|
|
| 665 |
elif user_state["stage"] == "topic_choice":
|
| 666 |
print("topic_choice block successfully entered")
|
| 667 |
print(f"User entered input: {user_input}")
|
| 668 |
+
vignette_type = user_state["standardized_data"].get("Vignette Type", "custom")
|
| 669 |
user_state['options_text'] = '1. A situation related to your profession.\n2. A situation related to your personal interests.\n3. Something else about me (Add another activity or interest that is important to you by saying "I also like...")'
|
| 670 |
user_state["user_responses"].append({"stage": "topic_choice", "response": user_input})
|
| 671 |
print(f"user input was: {user_input}, user responses after storing it are: {user_state['user_responses']}")
|