Local-Voice/debug_cache.py
2025-09-23 13:40:57 +08:00

178 lines
5.7 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
缓存调试脚本
用于调试greeting缓存问题
"""
import os
import sys
import json
from pathlib import Path
from greeting_cache_manager import GreetingCacheManager
def debug_cache_issues():
"""调试缓存问题"""
print("🔍 调试greeting缓存问题")
print("=" * 50)
# 检查缓存目录
cache_dir = Path("greeting_cache")
if not cache_dir.exists():
print("❌ 缓存目录不存在")
return
print(f"📁 缓存目录: {cache_dir.absolute()}")
# 列出所有文件
print("\n📋 缓存文件列表:")
cache_files = list(cache_dir.glob("*.wav"))
for i, file in enumerate(cache_files):
print(f" {i+1}. {file.name}")
print(f" 大小: {file.stat().st_size} 字节")
print(f" 路径: {file}")
# 检查索引文件
index_file = cache_dir / "cache_index.json"
print(f"\n📋 索引文件: {index_file}")
if index_file.exists():
try:
with open(index_file, 'r', encoding='utf-8') as f:
cache_index = json.load(f)
print(f" 索引记录数: {len(cache_index)}")
for key, path in cache_index.items():
print(f" {key} -> {path}")
except Exception as e:
print(f" ❌ 读取索引失败: {e}")
else:
print(" ❌ 索引文件不存在")
# 创建缓存管理器实例
cache_manager = GreetingCacheManager()
# 检查缓存完整性
print(f"\n🔍 缓存验证:")
valid_count, invalid_count = cache_manager.validate_cache()
print(f" 有效缓存: {valid_count}")
print(f" 无效缓存: {invalid_count}")
# 模拟缓存检查
print(f"\n🧪 模拟缓存检查:")
# 假设的角色配置
test_characters = {
"libai": "吾乃李白,字太白,号青莲居士。今天有幸与君相会,让我们畅谈诗词人生吧!",
"zhubajie": "俺老猪来也!想吃点好的,睡个好觉,过神仙日子!"
}
for char_name, greeting in test_characters.items():
print(f"\n 测试角色: {char_name}")
# 检查缓存状态
is_cached = cache_manager.is_cached(char_name, greeting)
print(f" 缓存状态: {'✅ 已缓存' if is_cached else '❌ 未缓存'}")
# 获取缓存路径
cache_path = cache_manager.get_cache_path(char_name, greeting)
print(f" 生成路径: {cache_path}")
print(f" 文件存在: {cache_path.exists()}")
# 获取hash
greeting_hash = cache_manager._get_greeting_hash(greeting)
print(f" 文本hash: {greeting_hash}")
# 检查索引键
cache_key = f"{char_name}_{greeting_hash}"
print(f" 索引键: {cache_key}")
print(f" 索引存在: {cache_key in cache_manager.cache_index}")
if cache_key in cache_manager.cache_index:
indexed_path = cache_manager.cache_index[cache_key]
print(f" 索引路径: {indexed_path}")
print(f" 路径匹配: {str(cache_path) == indexed_path}")
def fix_cache_filenames():
"""修复缓存文件名中的冒号问题"""
print("\n🔧 修复缓存文件名问题")
print("=" * 50)
cache_dir = Path("greeting_cache")
if not cache_dir.exists():
print("❌ 缓存目录不存在")
return
# 查找有问题的文件名(包含冒号的)
problem_files = list(cache_dir.glob(":*.wav"))
if problem_files:
print(f"🔍 发现 {len(problem_files)} 个有问题的文件名:")
for file in problem_files:
print(f" {file.name}")
# 修复文件名
new_name = file.name[1:] # 移除开头的冒号
new_path = file.parent / new_name
try:
file.rename(new_path)
print(f" ✅ 重命名为: {new_name}")
except Exception as e:
print(f" ❌ 重命名失败: {e}")
else:
print("✅ 没有发现文件名问题")
def rebuild_cache_index():
"""重建缓存索引"""
print("\n🔄 重建缓存索引")
print("=" * 50)
cache_dir = Path("greeting_cache")
if not cache_dir.exists():
print("❌ 缓存目录不存在")
return
cache_manager = GreetingCacheManager()
# 清空当前索引
cache_manager.cache_index = {}
# 扫描所有wav文件
wav_files = list(cache_dir.glob("*.wav"))
print(f"🔍 扫描到 {len(wav_files)} 个wav文件")
for wav_file in wav_files:
# 尝试从文件名解析角色名和hash
name_parts = wav_file.stem.split('_')
if len(name_parts) >= 2:
character_name = name_parts[0]
greeting_hash = name_parts[-1] # 最后一个部分是hash
cache_key = f"{character_name}_{greeting_hash}"
cache_manager.cache_index[cache_key] = str(wav_file)
print(f" ✅ 添加到索引: {cache_key}")
else:
print(f" ❌ 无法解析文件名: {wav_file.name}")
# 保存索引
cache_manager._save_cache_index()
print(f"✅ 索引重建完成,共 {len(cache_manager.cache_index)} 条记录")
if __name__ == "__main__":
try:
# 调试缓存问题
debug_cache_issues()
# 修复文件名问题
fix_cache_filenames()
# 重建索引
rebuild_cache_index()
print("\n🎉 缓存调试和修复完成!")
except Exception as e:
print(f"\n❌ 调试失败: {e}")
import traceback
traceback.print_exc()
sys.exit(1)