Spaces:
Running
Running
Add debug information and local file reading for chat history retrieval
Browse files- app.py +22 -0
- src/analytics/chat_evaluator.py +27 -4
app.py
CHANGED
|
@@ -755,6 +755,28 @@ def initialize_app():
|
|
| 755 |
print(f"App initialized with model: {ACTIVE_MODEL['name']}")
|
| 756 |
return selected_model
|
| 757 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 758 |
# Initialize HF client with token at startup
|
| 759 |
selected_model = initialize_app()
|
| 760 |
|
|
|
|
| 755 |
print(f"App initialized with model: {ACTIVE_MODEL['name']}")
|
| 756 |
return selected_model
|
| 757 |
|
| 758 |
+
def initialize_chat_evaluator():
|
| 759 |
+
"""Initialize chat evaluator with proper paths"""
|
| 760 |
+
try:
|
| 761 |
+
evaluator = ChatEvaluator(
|
| 762 |
+
hf_token=HF_TOKEN,
|
| 763 |
+
dataset_id=DATASET_ID,
|
| 764 |
+
chat_history_path=CHAT_HISTORY_PATH,
|
| 765 |
+
annotations_dir=os.path.join(CHAT_HISTORY_PATH, 'evaluations')
|
| 766 |
+
)
|
| 767 |
+
|
| 768 |
+
# Проверим наличие директорий
|
| 769 |
+
os.makedirs(CHAT_HISTORY_PATH, exist_ok=True)
|
| 770 |
+
os.makedirs(os.path.join(CHAT_HISTORY_PATH, 'evaluations'), exist_ok=True)
|
| 771 |
+
|
| 772 |
+
print(f"Debug - Chat history path: {CHAT_HISTORY_PATH}")
|
| 773 |
+
print(f"Debug - Number of chat files: {len(os.listdir(CHAT_HISTORY_PATH))}")
|
| 774 |
+
|
| 775 |
+
return evaluator
|
| 776 |
+
except Exception as e:
|
| 777 |
+
print(f"Error initializing chat evaluator: {str(e)}")
|
| 778 |
+
raise
|
| 779 |
+
|
| 780 |
# Initialize HF client with token at startup
|
| 781 |
selected_model = initialize_app()
|
| 782 |
|
src/analytics/chat_evaluator.py
CHANGED
|
@@ -38,13 +38,35 @@ class ChatEvaluator:
|
|
| 38 |
def get_chat_history(self) -> List[Dict[str, Any]]:
|
| 39 |
"""
|
| 40 |
Get all chat history data from local files and dataset
|
| 41 |
-
|
| 42 |
-
Returns:
|
| 43 |
-
List of chat histories
|
| 44 |
"""
|
| 45 |
success, chat_data = self.dataset_manager.get_chat_history()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
if not success or not chat_data:
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return chat_data
|
| 49 |
|
| 50 |
def get_qa_pairs_for_evaluation(self, limit: int = 50) -> List[Dict[str, Any]]:
|
|
@@ -318,3 +340,4 @@ class ChatEvaluator:
|
|
| 318 |
metrics["improvement_rate"] = (improved_count / len(annotations)) * 100
|
| 319 |
|
| 320 |
return metrics
|
|
|
|
|
|
| 38 |
def get_chat_history(self) -> List[Dict[str, Any]]:
|
| 39 |
"""
|
| 40 |
Get all chat history data from local files and dataset
|
|
|
|
|
|
|
|
|
|
| 41 |
"""
|
| 42 |
success, chat_data = self.dataset_manager.get_chat_history()
|
| 43 |
+
|
| 44 |
+
# Добавим отладочную информацию
|
| 45 |
+
print(f"Debug - Chat history fetch success: {success}")
|
| 46 |
+
print(f"Debug - Number of chat records: {len(chat_data) if chat_data else 0}")
|
| 47 |
+
|
| 48 |
if not success or not chat_data:
|
| 49 |
+
# Попробуем прочитать локальные файлы
|
| 50 |
+
local_data = self._read_local_chat_history()
|
| 51 |
+
print(f"Debug - Local chat records found: {len(local_data)}")
|
| 52 |
+
return local_data
|
| 53 |
+
return chat_data
|
| 54 |
+
|
| 55 |
+
def _read_local_chat_history(self) -> List[Dict[str, Any]]:
|
| 56 |
+
"""
|
| 57 |
+
Read chat history from local files
|
| 58 |
+
"""
|
| 59 |
+
chat_data = []
|
| 60 |
+
if os.path.exists(self.chat_history_path):
|
| 61 |
+
for filename in os.listdir(self.chat_history_path):
|
| 62 |
+
if filename.endswith('.json'):
|
| 63 |
+
try:
|
| 64 |
+
filepath = os.path.join(self.chat_history_path, filename)
|
| 65 |
+
with open(filepath, 'r', encoding='utf-8') as f:
|
| 66 |
+
data = json.load(f)
|
| 67 |
+
chat_data.append(data)
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Error reading chat file {filename}: {str(e)}")
|
| 70 |
return chat_data
|
| 71 |
|
| 72 |
def get_qa_pairs_for_evaluation(self, limit: int = 50) -> List[Dict[str, Any]]:
|
|
|
|
| 340 |
metrics["improvement_rate"] = (improved_count / len(annotations)) * 100
|
| 341 |
|
| 342 |
return metrics
|
| 343 |
+
|