Spaces:
Running
Running
Yufan_Zhou commited on
Commit Β·
db7d8fe
1
Parent(s): d8c5622
Refactor: Match reference version parameter passing logic
Browse filesKey changes:
- web_api_bridge.py now builds profile dict directly in memory
- Pass profile to get_selected_attributes() instead of saving to file
- Remove generate_single_profile() dependency
- Use generate_final_summary() directly with profile
- Simplified flow: build profile β select attributes β generate summary
- Clear β/β indicators for user inputs vs generated fields
This matches the working reference version in experience_temp/
generate_user_profile_final/code/web_api_bridge.py
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
Web API bridge script for handling inputs from the web interface
|
| 6 |
-
and generating user personas
|
| 7 |
"""
|
| 8 |
|
| 9 |
import json
|
|
@@ -24,7 +24,8 @@ from based_data import (
|
|
| 24 |
generate_interests_and_hobbies
|
| 25 |
)
|
| 26 |
|
| 27 |
-
from generate_profile import
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
def parse_input_data(input_file: str) -> Dict[str, Any]:
|
|
@@ -48,7 +49,7 @@ def parse_input_data(input_file: str) -> Dict[str, Any]:
|
|
| 48 |
|
| 49 |
def generate_profile_from_input(input_data: Dict[str, Any], attribute_count: int = 200) -> Dict[str, Any]:
|
| 50 |
"""
|
| 51 |
-
Generate a complete user persona from input data
|
| 52 |
|
| 53 |
Args:
|
| 54 |
input_data: Input data containing basic information and optional custom values
|
|
@@ -67,117 +68,107 @@ def generate_profile_from_input(input_data: Dict[str, Any], attribute_count: int
|
|
| 67 |
# Use provided basic information or generate new basic information
|
| 68 |
age = basic_info.get('age')
|
| 69 |
if not age:
|
| 70 |
-
print("
|
| 71 |
age_info = generate_age_info()
|
| 72 |
age = age_info['age']
|
| 73 |
else:
|
| 74 |
-
print(f"Using provided age: {age}")
|
| 75 |
|
| 76 |
gender = basic_info.get('gender')
|
| 77 |
if not gender:
|
| 78 |
-
print("
|
| 79 |
gender = generate_gender()
|
| 80 |
else:
|
| 81 |
-
print(f"Using provided gender: {gender}")
|
| 82 |
|
| 83 |
occupation_info = basic_info.get('occupation', {})
|
| 84 |
occupation = occupation_info.get('status')
|
| 85 |
if not occupation:
|
| 86 |
-
print("
|
| 87 |
career_info = generate_career_info(age)
|
| 88 |
occupation = career_info['status']
|
| 89 |
else:
|
| 90 |
-
print(f"Using provided occupation: {occupation}")
|
| 91 |
|
| 92 |
location_info = basic_info.get('location', {})
|
| 93 |
if not location_info.get('city') or not location_info.get('country'):
|
| 94 |
-
print("
|
| 95 |
location_info = generate_location()
|
| 96 |
else:
|
| 97 |
-
print(f"Using provided location: {location_info.get('city')}, {location_info.get('country')}")
|
| 98 |
|
| 99 |
# Use custom personal values if provided, otherwise generate them
|
| 100 |
custom_personal_values = custom_values.get('personal_values')
|
| 101 |
if custom_personal_values and custom_personal_values.strip():
|
| 102 |
-
print("Using provided personal values...")
|
| 103 |
values_orientation = custom_personal_values.strip()
|
| 104 |
else:
|
| 105 |
-
print("
|
| 106 |
values_dict = generate_personal_values(age, gender, occupation, location_info)
|
| 107 |
values_orientation = values_dict.get("values_orientation", "") if isinstance(values_dict, dict) else str(values_dict)
|
| 108 |
|
| 109 |
# Use custom life attitude if provided, otherwise generate it
|
| 110 |
custom_life_attitude = custom_values.get('life_attitude')
|
| 111 |
if custom_life_attitude and custom_life_attitude.strip():
|
| 112 |
-
print("Using provided life attitude...")
|
| 113 |
life_attitude = {
|
| 114 |
"outlook": custom_life_attitude.strip(),
|
| 115 |
"coping_mechanism": "Custom life attitude provided by user"
|
| 116 |
}
|
| 117 |
else:
|
| 118 |
-
print("
|
| 119 |
life_attitude = generate_life_attitude(age, gender, occupation, location_info, values_orientation)
|
| 120 |
|
| 121 |
# Use custom life story if provided, otherwise generate it
|
| 122 |
custom_life_story = custom_values.get('life_story')
|
| 123 |
if custom_life_story and custom_life_story.strip():
|
| 124 |
-
print("Using provided life story...")
|
| 125 |
personal_story = {"personal_story": custom_life_story.strip()}
|
| 126 |
else:
|
| 127 |
-
print("
|
| 128 |
personal_story = generate_personal_story(age, gender, occupation, location_info, values_orientation, life_attitude)
|
| 129 |
|
| 130 |
# Use custom interests/hobbies if provided, otherwise generate them
|
| 131 |
custom_interests = custom_values.get('interests_hobbies')
|
| 132 |
if custom_interests and custom_interests.strip():
|
| 133 |
-
print("Using provided interests
|
| 134 |
# Split by comma and clean up
|
| 135 |
interests_list = [i.strip() for i in custom_interests.split(',') if i.strip()]
|
| 136 |
interests_and_hobbies = {"interests": interests_list}
|
| 137 |
else:
|
| 138 |
-
print("
|
| 139 |
interests_and_hobbies = generate_interests_and_hobbies(personal_story)
|
| 140 |
|
| 141 |
-
# Build
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
"personal_values": {
|
| 149 |
-
"values_orientation": values_orientation
|
| 150 |
},
|
| 151 |
-
"
|
| 152 |
-
"
|
| 153 |
-
"
|
|
|
|
| 154 |
}
|
| 155 |
|
| 156 |
-
#
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
# 2. Generate all sections using the improved prompts
|
| 171 |
-
# 3. Generate the final summary
|
| 172 |
-
print(f"Generating complete profile with {attribute_count} attributes...")
|
| 173 |
-
profile = generate_single_profile(template=None, profile_index=0, attribute_count=attribute_count)
|
| 174 |
-
|
| 175 |
-
if not profile:
|
| 176 |
-
print("Failed to generate profile")
|
| 177 |
-
return {}
|
| 178 |
|
| 179 |
return profile
|
| 180 |
-
|
| 181 |
except Exception as e:
|
| 182 |
print(f"Error generating user persona: {e}")
|
| 183 |
import traceback
|
|
|
|
| 3 |
|
| 4 |
"""
|
| 5 |
Web API bridge script for handling inputs from the web interface
|
| 6 |
+
and generating user personas following the logic in generate_profile.py.
|
| 7 |
"""
|
| 8 |
|
| 9 |
import json
|
|
|
|
| 24 |
generate_interests_and_hobbies
|
| 25 |
)
|
| 26 |
|
| 27 |
+
from generate_profile import generate_final_summary
|
| 28 |
+
from select_attributes import get_selected_attributes, save_results
|
| 29 |
|
| 30 |
|
| 31 |
def parse_input_data(input_file: str) -> Dict[str, Any]:
|
|
|
|
| 49 |
|
| 50 |
def generate_profile_from_input(input_data: Dict[str, Any], attribute_count: int = 200) -> Dict[str, Any]:
|
| 51 |
"""
|
| 52 |
+
Generate a complete user persona from input data
|
| 53 |
|
| 54 |
Args:
|
| 55 |
input_data: Input data containing basic information and optional custom values
|
|
|
|
| 68 |
# Use provided basic information or generate new basic information
|
| 69 |
age = basic_info.get('age')
|
| 70 |
if not age:
|
| 71 |
+
print("β Age not provided, generating...")
|
| 72 |
age_info = generate_age_info()
|
| 73 |
age = age_info['age']
|
| 74 |
else:
|
| 75 |
+
print(f"β Using provided age: {age}")
|
| 76 |
|
| 77 |
gender = basic_info.get('gender')
|
| 78 |
if not gender:
|
| 79 |
+
print("β Gender not provided, generating...")
|
| 80 |
gender = generate_gender()
|
| 81 |
else:
|
| 82 |
+
print(f"β Using provided gender: {gender}")
|
| 83 |
|
| 84 |
occupation_info = basic_info.get('occupation', {})
|
| 85 |
occupation = occupation_info.get('status')
|
| 86 |
if not occupation:
|
| 87 |
+
print("β Occupation not provided, generating...")
|
| 88 |
career_info = generate_career_info(age)
|
| 89 |
occupation = career_info['status']
|
| 90 |
else:
|
| 91 |
+
print(f"β Using provided occupation: {occupation}")
|
| 92 |
|
| 93 |
location_info = basic_info.get('location', {})
|
| 94 |
if not location_info.get('city') or not location_info.get('country'):
|
| 95 |
+
print("β Location not provided, generating...")
|
| 96 |
location_info = generate_location()
|
| 97 |
else:
|
| 98 |
+
print(f"β Using provided location: {location_info.get('city')}, {location_info.get('country')}")
|
| 99 |
|
| 100 |
# Use custom personal values if provided, otherwise generate them
|
| 101 |
custom_personal_values = custom_values.get('personal_values')
|
| 102 |
if custom_personal_values and custom_personal_values.strip():
|
| 103 |
+
print(f"β Using provided personal values: {custom_personal_values[:50]}...")
|
| 104 |
values_orientation = custom_personal_values.strip()
|
| 105 |
else:
|
| 106 |
+
print("β Personal values not provided, generating based on inputs...")
|
| 107 |
values_dict = generate_personal_values(age, gender, occupation, location_info)
|
| 108 |
values_orientation = values_dict.get("values_orientation", "") if isinstance(values_dict, dict) else str(values_dict)
|
| 109 |
|
| 110 |
# Use custom life attitude if provided, otherwise generate it
|
| 111 |
custom_life_attitude = custom_values.get('life_attitude')
|
| 112 |
if custom_life_attitude and custom_life_attitude.strip():
|
| 113 |
+
print(f"β Using provided life attitude: {custom_life_attitude[:50]}...")
|
| 114 |
life_attitude = {
|
| 115 |
"outlook": custom_life_attitude.strip(),
|
| 116 |
"coping_mechanism": "Custom life attitude provided by user"
|
| 117 |
}
|
| 118 |
else:
|
| 119 |
+
print("β Life attitude not provided, generating based on inputs...")
|
| 120 |
life_attitude = generate_life_attitude(age, gender, occupation, location_info, values_orientation)
|
| 121 |
|
| 122 |
# Use custom life story if provided, otherwise generate it
|
| 123 |
custom_life_story = custom_values.get('life_story')
|
| 124 |
if custom_life_story and custom_life_story.strip():
|
| 125 |
+
print(f"β Using provided life story: {custom_life_story[:50]}...")
|
| 126 |
personal_story = {"personal_story": custom_life_story.strip()}
|
| 127 |
else:
|
| 128 |
+
print("β Life story not provided, generating based on inputs...")
|
| 129 |
personal_story = generate_personal_story(age, gender, occupation, location_info, values_orientation, life_attitude)
|
| 130 |
|
| 131 |
# Use custom interests/hobbies if provided, otherwise generate them
|
| 132 |
custom_interests = custom_values.get('interests_hobbies')
|
| 133 |
if custom_interests and custom_interests.strip():
|
| 134 |
+
print(f"β Using provided interests: {custom_interests}")
|
| 135 |
# Split by comma and clean up
|
| 136 |
interests_list = [i.strip() for i in custom_interests.split(',') if i.strip()]
|
| 137 |
interests_and_hobbies = {"interests": interests_list}
|
| 138 |
else:
|
| 139 |
+
print("β Interests not provided, generating based on life story...")
|
| 140 |
interests_and_hobbies = generate_interests_and_hobbies(personal_story)
|
| 141 |
|
| 142 |
+
# Build complete user persona (matching reference version structure)
|
| 143 |
+
profile = {
|
| 144 |
+
"basic_info": {
|
| 145 |
+
"age": age,
|
| 146 |
+
"gender": gender,
|
| 147 |
+
"occupation": {"status": occupation},
|
| 148 |
+
"location": location_info
|
|
|
|
|
|
|
| 149 |
},
|
| 150 |
+
"values_orientation": values_orientation,
|
| 151 |
+
"life_attitude": life_attitude,
|
| 152 |
+
"personal_story": personal_story,
|
| 153 |
+
"interests_and_hobbies": interests_and_hobbies
|
| 154 |
}
|
| 155 |
|
| 156 |
+
# Select attributes
|
| 157 |
+
print(f"Selecting {attribute_count} attributes...")
|
| 158 |
+
# Pass the profile directly to get_selected_attributes
|
| 159 |
+
selected_paths = get_selected_attributes(profile, attribute_count)
|
| 160 |
+
profile["selected_attributes"] = selected_paths
|
| 161 |
|
| 162 |
+
# Save the selected attributes to output files
|
| 163 |
+
print("Saving selected attributes...")
|
| 164 |
+
save_results(profile, selected_paths)
|
| 165 |
|
| 166 |
+
# Generate final summary
|
| 167 |
+
print("Generating final summary...")
|
| 168 |
+
summary = generate_final_summary(profile)
|
| 169 |
+
profile["Summary"] = summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
return profile
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
print(f"Error generating user persona: {e}")
|
| 174 |
import traceback
|