Spaces:
Running
Running
A newer version of the Gradio SDK is available:
6.5.1
重构说明 - 匹配参考版本的参数传递逻辑
问题根源
之前的实现与参考版本(experience_temp/generation_user_profile)的逻辑不同:
❌ 旧版本的问题流程
web_api_bridge.py
↓ 保存 user_profile.json 到文件
↓ 调用 generate_single_profile()
↓ generate_single_profile() 读取文件
↓ 调用 select_attributes.generate_user_profile() 重新生成
↓ 用户输入被覆盖!
✅ 新版本的正确流程(匹配参考版本)
web_api_bridge.py
↓ 在内存中构建 profile 字典
↓ 直接传递 profile 给 get_selected_attributes(profile)
↓ 调用 generate_final_summary(profile)
↓ 用户输入被正确使用!
关键修改
1. web_api_bridge.py - 完全重构
旧逻辑:
# 保存到文件
base_profile = {...}
with open('user_profile.json', 'w') as f:
json.dump(base_profile, f)
# 调用复杂的 generate_single_profile
profile = generate_single_profile(attribute_count=200)
新逻辑(匹配参考版本):
# 直接在内存中构建 profile
profile = {
"basic_info": {
"age": age,
"gender": gender,
"occupation": {"status": occupation},
"location": location_info
},
"values_orientation": values_orientation,
"life_attitude": life_attitude,
"personal_story": personal_story,
"interests_and_hobbies": interests_and_hobbies
}
# 直接传递给后续函数
selected_paths = get_selected_attributes(profile, attribute_count)
summary = generate_final_summary(profile)
2. 数据结构对齐
参考版本的 profile 结构:
{
"basic_info": {
"age": 25,
"gender": "Male",
"occupation": {"status": "Engineer"},
"location": {"city": "Tokyo", "country": "Japan"}
},
"values_orientation": "...",
"life_attitude": {...},
"personal_story": {...},
"interests_and_hobbies": {...}
}
现在的实现完全匹配这个结构。
3. 清晰的输出指示器
# 用户提供的输入
print(f"✓ Using provided age: {age}")
# 自动生成的内容
print("✗ Age not provided, generating...")
参考版本的关键特征
- 不依赖文件系统: 所有数据在内存中传递
- 简单的函数调用链:
build profile → select attributes → generate summary - 直接参数传递:
get_selected_attributes(profile)而不是从文件读取 - 用户输入优先: 有输入就用,没有才生成
测试验证
测试场景 1: 部分输入
输入: age=25, gender=Male, occupation=Engineer
输出应显示:
✓ Using provided age: 25
✓ Using provided gender: Male
✓ Using provided occupation: Engineer
✗ Location not provided, generating...
✗ Personal values not provided, generating based on inputs...
测试场景 2: 完整输入
输入: 所有字段都填写
输出应显示:
✓ Using provided age: 30
✓ Using provided gender: Female
✓ Using provided occupation: Teacher
✓ Using provided location: Paris, France
✓ Using provided personal values: ...
✓ Using provided life attitude: ...
✓ Using provided life story: ...
✓ Using provided interests: ...
与参考版本的对比
| 特性 | 参考版本 | 旧实现 | 新实现 |
|---|---|---|---|
| 参数传递方式 | 内存 | 文件 | ✅ 内存 |
| profile 结构 | basic_info + values | age + Occupations | ✅ basic_info + values |
| 函数调用 | 直接调用 | 通过文件中转 | ✅ 直接调用 |
| 用户输入处理 | 直接使用 | 被覆盖 | ✅ 直接使用 |
| 代码复杂度 | 简单 | 复杂 | ✅ 简单 |
移除的依赖
- ❌ 不再需要
generate_single_profile() - ❌ 不再需要
convert_web_profile_to_selector_format() - ❌ 不再需要保存/读取
user_profile.json - ✅ 直接使用
get_selected_attributes(profile, attribute_count) - ✅ 直接使用
generate_final_summary(profile)
结论
通过匹配参考版本的实现方式,我们:
- ✅ 简化了代码逻辑
- ✅ 消除了文件系统依赖
- ✅ 确保用户输入被正确使用
- ✅ 提高了代码可维护性
现在的实现与 GitHub 上正常工作的参考版本完全一致!