Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -177,154 +177,120 @@ EXCLUSION_THRESHOLD = 20
|
|
| 177 |
# Storing / Uploading Functions
|
| 178 |
##############################################################################
|
| 179 |
|
| 180 |
-
def generate_custom_vignette(
|
| 181 |
-
conversation_history,
|
| 182 |
-
topic_choice,
|
| 183 |
-
desired_tone,
|
| 184 |
-
app_state=None,
|
| 185 |
-
context_types=None,
|
| 186 |
-
interaction_types=None,
|
| 187 |
-
actions_events=None
|
| 188 |
-
):
|
| 189 |
"""
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
- Maintain a clearly Positive or Negative atmosphere (desired_tone).
|
| 202 |
-
- Follow the user's topic choice:
|
| 203 |
-
1 => profession-related
|
| 204 |
-
2 => personal interests
|
| 205 |
-
3 => something referencing the last user input in conversation_history
|
| 206 |
"""
|
| 207 |
-
|
| 208 |
if app_state is None:
|
| 209 |
raise ValueError("app_state must be passed to generate_custom_vignette().")
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
# Extract user profile
|
| 214 |
user_profile = app_state.get("standardized_data", {})
|
| 215 |
user_age = user_profile.get("Age", "Unknown")
|
| 216 |
user_profession = user_profile.get("Profession", "Unknown")
|
| 217 |
user_interests = user_profile.get("Interests", "Unknown")
|
| 218 |
|
| 219 |
-
#
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
#
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
| 237 |
|
| 238 |
-
#
|
| 239 |
-
|
| 240 |
|
|
|
|
| 241 |
prompt = f"""
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
{
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
{
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
-
|
| 254 |
-
-
|
| 255 |
-
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
- Do not add any extra lines like "Tone: ...".
|
| 273 |
-
- Do not say "Here is your vignette."
|
| 274 |
-
- Keep it neutral and professional, describing only outward actions or occurrences.
|
| 275 |
-
|
| 276 |
-
Below is the conversation history, which may include clarifications or references from the user:
|
| 277 |
-
{conversation_history}
|
| 278 |
-
|
| 279 |
-
Now:
|
| 280 |
-
1) Select the best modules from each dictionary (one context, one interaction, one action).
|
| 281 |
-
2) Ensure the final text is concise (<50 tokens).
|
| 282 |
-
3) Remain in second-person, external focus, matching the chosen tone.
|
| 283 |
-
4) Output only the short scenario text, nothing else.
|
| 284 |
-
"""
|
| 285 |
|
| 286 |
-
# Prepare the messages for the
|
| 287 |
messages = [
|
| 288 |
-
{
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
"avoiding internal states or feelings."
|
| 294 |
-
),
|
| 295 |
-
},
|
| 296 |
-
{
|
| 297 |
-
"role": "user",
|
| 298 |
-
"content": prompt,
|
| 299 |
-
},
|
| 300 |
]
|
| 301 |
|
| 302 |
-
#
|
| 303 |
-
# if you want the LLM to read them as distinct user/assistant messages:
|
| 304 |
-
for msg in conversation_history:
|
| 305 |
-
messages.append(msg)
|
| 306 |
-
|
| 307 |
-
# Make the OpenAI API call
|
| 308 |
try:
|
| 309 |
-
response = openai.
|
| 310 |
-
model="gpt-
|
| 311 |
messages=messages,
|
| 312 |
temperature=0.3,
|
| 313 |
-
max_tokens=
|
| 314 |
)
|
| 315 |
content = response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
except Exception as e:
|
| 317 |
-
print(f"Error
|
| 318 |
return None, None
|
| 319 |
|
| 320 |
-
# Final text is the entire content; the tone we used is desired_tone (or fallback)
|
| 321 |
-
# If desired_tone is not strictly "Positive"/"Negative," you might default to "Positive."
|
| 322 |
-
final_tone = desired_tone
|
| 323 |
-
if final_tone not in ["Positive", "Negative"]:
|
| 324 |
-
final_tone = "Positive"
|
| 325 |
-
|
| 326 |
-
# Return the raw content + final_tone
|
| 327 |
-
return content, final_tone
|
| 328 |
|
| 329 |
|
| 330 |
def upload_file_to_space(local_file_path, repo_id, hf_token, path_in_repo=None):
|
|
@@ -650,10 +616,7 @@ def new_chat_input(message, history, app_state=None, *args, **kwargs):
|
|
| 650 |
conversation_history=app_state["conversation_history"],
|
| 651 |
topic_choice=app_state["topic_choice"],
|
| 652 |
desired_tone=app_state["desired_tone"],
|
| 653 |
-
app_state=app_state
|
| 654 |
-
context_types=context_types,
|
| 655 |
-
interaction_types=interaction_types,
|
| 656 |
-
actions_events=actions_events
|
| 657 |
)
|
| 658 |
app_state["vignette"] = vignette
|
| 659 |
app_state["tone"] = tone
|
|
@@ -767,7 +730,7 @@ def new_chat_input(message, history, app_state=None, *args, **kwargs):
|
|
| 767 |
app_state["user_responses"].append({'app_state["stage"]': 'additional_info', 'response': additional_info})
|
| 768 |
app_state["conversation_history"].append({"role": "user", "content": additional_info})
|
| 769 |
# Use the additional info in the conversation history to regenerate the vignette
|
| 770 |
-
app_state["vignette"], app_state["tone"] = generate_custom_vignette(conversation_history=app_state["conversation_history"], topic_choice=app_state["topic_choice"], desired_tone=app_state["desired_tone"], app_state=app_state
|
| 771 |
app_state["conversation_history"].append({"role": "assistant", "content": app_state["vignette"]})
|
| 772 |
app_state["user_responses"].append({'app_state["stage"]': 'vignette_regenerated', 'content': app_state["vignette"], 'tone': app_state["tone"]})
|
| 773 |
# Temporarily store the new vignette and tone
|
|
|
|
| 177 |
# Storing / Uploading Functions
|
| 178 |
##############################################################################
|
| 179 |
|
| 180 |
+
def generate_custom_vignette(conversation_history, topic_choice, desired_tone, app_state=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
"""
|
| 182 |
+
Generates a personalized vignette based on user preferences and modular seeds for a psychological assessment.
|
| 183 |
+
|
| 184 |
+
Args:
|
| 185 |
+
conversation_history (list): The user's conversation history for context.
|
| 186 |
+
topic_choice (str): The user's selected topic ("1" = profession, "2" = interests, "3" = most recent input).
|
| 187 |
+
desired_tone (str): The desired tone for the vignette ("Positive" or "Negative").
|
| 188 |
+
app_state (dict): The application state containing user data and standardized data.
|
| 189 |
+
|
| 190 |
+
Returns:
|
| 191 |
+
vignette (str): The generated vignette text.
|
| 192 |
+
tone (str): The tone used for the vignette.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
"""
|
|
|
|
| 194 |
if app_state is None:
|
| 195 |
raise ValueError("app_state must be passed to generate_custom_vignette().")
|
| 196 |
+
|
| 197 |
+
# Extract user profile from app_state
|
|
|
|
|
|
|
| 198 |
user_profile = app_state.get("standardized_data", {})
|
| 199 |
user_age = user_profile.get("Age", "Unknown")
|
| 200 |
user_profession = user_profile.get("Profession", "Unknown")
|
| 201 |
user_interests = user_profile.get("Interests", "Unknown")
|
| 202 |
|
| 203 |
+
# Dynamically extract modular seeds
|
| 204 |
+
context_types_str = ", ".join(context_types.keys())
|
| 205 |
+
interaction_types_str = ", ".join(interaction_types.keys())
|
| 206 |
+
actions_events_str = ", ".join(actions_events.keys())
|
| 207 |
+
|
| 208 |
+
# Determine topic context
|
| 209 |
+
if topic_choice == "1":
|
| 210 |
+
topic_context = "Work/Professional"
|
| 211 |
+
elif topic_choice == "2":
|
| 212 |
+
topic_context = "Personal Interests"
|
| 213 |
+
elif topic_choice == "3":
|
| 214 |
+
# Use the most recent entry in the conversation history
|
| 215 |
+
recent_context = (
|
| 216 |
+
conversation_history[-1]["content"]
|
| 217 |
+
if conversation_history and "content" in conversation_history[-1]
|
| 218 |
+
else "general activities"
|
| 219 |
+
)
|
| 220 |
+
topic_context = f"Recent input: {recent_context}"
|
| 221 |
+
else:
|
| 222 |
+
topic_context = "General context"
|
| 223 |
|
| 224 |
+
# Validate the desired tone
|
| 225 |
+
final_tone = "Positive" if desired_tone not in ["Positive", "Negative"] else desired_tone
|
| 226 |
|
| 227 |
+
# Build the OpenAI API prompt
|
| 228 |
prompt = f"""
|
| 229 |
+
Using the following modular seeds, create a personalized vignette for a psychological assessment:
|
| 230 |
+
- Context Types: {context_types_str}
|
| 231 |
+
- Interaction Types: {interaction_types_str}
|
| 232 |
+
- Actions/Events: {actions_events_str}
|
| 233 |
+
|
| 234 |
+
Guidelines:
|
| 235 |
+
- Analyze the conversation history to understand the user's profile:
|
| 236 |
+
- Age: {user_age}
|
| 237 |
+
- Profession: {user_profession}
|
| 238 |
+
- Interests: {user_interests}
|
| 239 |
+
- The user has chosen topic {topic_choice}:
|
| 240 |
+
- **If the user chose '1', focus on a situation related to their profession ({user_profession}).**
|
| 241 |
+
- **If the user chose '2', focus on a situation related to their personal interests ({user_interests}).**
|
| 242 |
+
- **If the user chose '3', focus on the most recent input ({topic_context}).**
|
| 243 |
+
|
| 244 |
+
Instructions:
|
| 245 |
+
- Select one module from each category (Context, Interaction, Action/Event) to build the vignette.
|
| 246 |
+
- Match the tone to the user's preference: {final_tone}.
|
| 247 |
+
- Ensure the vignette is directly related to the user's profile and context.
|
| 248 |
+
- Do not include the module names or categories in the vignette.
|
| 249 |
+
- Describe the scenario in the second person, focusing on external events without inner reflections, thoughts, or feelings.
|
| 250 |
+
- Keep the vignette concise (under 50 tokens).
|
| 251 |
+
- Use neutral, professional language suitable for a psychological assessment.
|
| 252 |
+
- Do not include any personal identifying information.
|
| 253 |
+
|
| 254 |
+
Output Format:
|
| 255 |
+
Tone: [Positive or Negative]
|
| 256 |
+
|
| 257 |
+
[Vignette Text]
|
| 258 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
+
# Prepare the messages for the API call
|
| 261 |
messages = [
|
| 262 |
+
{"role": "system", "content": """
|
| 263 |
+
You are tasked with selecting appropriate modules and generating a standardized vignette for a psychological assessment.
|
| 264 |
+
The vignette should assess attribution styles across three dimensions: Internal-External, Stable-Unstable, and Global-Specific.
|
| 265 |
+
"""},
|
| 266 |
+
{"role": "user", "content": prompt}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
]
|
| 268 |
|
| 269 |
+
# Call the OpenAI API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
try:
|
| 271 |
+
response = openai.ChatCompletion.create(
|
| 272 |
+
model="gpt-4",
|
| 273 |
messages=messages,
|
| 274 |
temperature=0.3,
|
| 275 |
+
max_tokens=100
|
| 276 |
)
|
| 277 |
content = response.choices[0].message.content.strip()
|
| 278 |
+
|
| 279 |
+
# Parse the response to separate tone and vignette
|
| 280 |
+
if content.startswith("Tone:"):
|
| 281 |
+
tone, vignette = content.split("\n", 1)
|
| 282 |
+
tone = tone.replace("Tone:", "").strip()
|
| 283 |
+
vignette = vignette.strip()
|
| 284 |
+
else:
|
| 285 |
+
tone = final_tone
|
| 286 |
+
vignette = content.strip()
|
| 287 |
+
|
| 288 |
+
return vignette, tone
|
| 289 |
+
|
| 290 |
except Exception as e:
|
| 291 |
+
print(f"Error generating vignette: {e}")
|
| 292 |
return None, None
|
| 293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
|
| 295 |
|
| 296 |
def upload_file_to_space(local_file_path, repo_id, hf_token, path_in_repo=None):
|
|
|
|
| 616 |
conversation_history=app_state["conversation_history"],
|
| 617 |
topic_choice=app_state["topic_choice"],
|
| 618 |
desired_tone=app_state["desired_tone"],
|
| 619 |
+
app_state=app_state
|
|
|
|
|
|
|
|
|
|
| 620 |
)
|
| 621 |
app_state["vignette"] = vignette
|
| 622 |
app_state["tone"] = tone
|
|
|
|
| 730 |
app_state["user_responses"].append({'app_state["stage"]': 'additional_info', 'response': additional_info})
|
| 731 |
app_state["conversation_history"].append({"role": "user", "content": additional_info})
|
| 732 |
# Use the additional info in the conversation history to regenerate the vignette
|
| 733 |
+
app_state["vignette"], app_state["tone"] = generate_custom_vignette(conversation_history=app_state["conversation_history"], topic_choice=app_state["topic_choice"], desired_tone=app_state["desired_tone"], app_state=app_state)
|
| 734 |
app_state["conversation_history"].append({"role": "assistant", "content": app_state["vignette"]})
|
| 735 |
app_state["user_responses"].append({'app_state["stage"]': 'vignette_regenerated', 'content': app_state["vignette"], 'tone': app_state["tone"]})
|
| 736 |
# Temporarily store the new vignette and tone
|