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
 
 
 
8ef4162
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
Модуль для управления датасетом на Hugging Face Hub
"""

import os
import json
import tempfile
from typing import Tuple, List, Dict, Any, Optional
from datetime import datetime
from huggingface_hub import HfApi, HfFolder
from config.settings import VECTOR_STORE_PATH

class DatasetManager:
    def __init__(self, dataset_name="Rulga/status-law-knowledge-base", token: Optional[str] = None):
        """
        Инициализация менеджера датасетов
        
        Args:
            dataset_name: Имя датасета на Hugging Face Hub
            token: Токен доступа к Hugging Face Hub (если не задан, берется из ~/.huggingface/token)
        """
        self.token = token if token else HfFolder.get_token()
        if not self.token:
            raise ValueError("Не найден токен Hugging Face. Установите переменную окружения HUGGINGFACE_TOKEN")
            
        self.api = HfApi(token=self.token)
        self.dataset_name = dataset_name
        
        # Проверяем/создаем репозиторий при инициализации
        try:
            self.api.repo_info(repo_id=self.dataset_name, repo_type="dataset")
        except Exception:
            print(f"Создаем новый репозиторий датасета: {self.dataset_name}")
            self.api.create_repo(
                repo_id=self.dataset_name,
                repo_type="dataset",
                private=True
            )

    def init_dataset_structure(self) -> Tuple[bool, str]:
        """
        Инициализация структуры датасета на Hugging Face
        
        Returns:
            (успех, сообщение)
        """
        try:
            # Проверяем существование репозитория
            try:
                self.api.repo_info(repo_id=self.dataset_name, repo_type="dataset")
            except Exception:
                # Если репозиторий не существует, создаем его
                self.api.create_repo(repo_id=self.dataset_name, repo_type="dataset", private=True)
            
            # Создаем пустые .gitkeep файлы для поддержания структуры
            directories = ["vector_store", "chat_history", "documents"]
            
            for directory in directories:
                with tempfile.NamedTemporaryFile(delete=False) as temp:
                    temp_path = temp.name
                
                try:
                    self.api.upload_file(
                        path_or_fileobj=temp_path,
                        path_in_repo=f"{directory}/.gitkeep",
                        repo_id=self.dataset_name,
                        repo_type="dataset"
                    )
                finally:
                    if os.path.exists(temp_path):
                        os.remove(temp_path)
            
            return True, "Структура датасета успешно создана"
        except Exception as e:
            return False, f"Ошибка при создании структуры датасета: {str(e)}"

    def upload_vector_store(self) -> Tuple[bool, str]:
        """
        Загрузка векторного хранилища в датасет
        
        Returns:
            (успех, сообщение)
        """
        try:
            # Проверяем наличие файлов
            index_path = os.path.join(VECTOR_STORE_PATH, "index.faiss")
            config_path = os.path.join(VECTOR_STORE_PATH, "index.pkl")
            
            if not os.path.exists(index_path):
                return False, f"Файл векторного хранилища не найден: {index_path}"
                
            if not os.path.exists(config_path):
                return False, f"Файл конфигурации не найден: {config_path}"

            # Загружаем файлы
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            
            # Сначала сохраняем старые версии файлов в архивной директории, если они существуют
            try:
                # Проверяем наличие старых файлов
                self.api.hf_hub_download(
                    repo_id=self.dataset_name,
                    filename="vector_store/index.faiss",
                    repo_type="dataset"
                )
                
                # Если файл существует, создаем архивную копию
                self.api.upload_file(
                    path_or_fileobj=index_path,
                    path_in_repo=f"vector_store/archive/index_{timestamp}.faiss",
                    repo_id=self.dataset_name,
                    repo_type="dataset"
                )
                
                self.api.upload_file(
                    path_or_fileobj=config_path,
                    path_in_repo=f"vector_store/archive/index_{timestamp}.pkl",
                    repo_id=self.dataset_name,
                    repo_type="dataset"
                )
            except Exception:
                # Если файлов нет, создаем директорию для архива
                with tempfile.NamedTemporaryFile(delete=False) as temp:
                    temp_path = temp.name
                
                try:
                    self.api.upload_file(
                        path_or_fileobj=temp_path,
                        path_in_repo="vector_store/archive/.gitkeep",
                        repo_id=self.dataset_name,
                        repo_type="dataset"
                    )
                finally:
                    if os.path.exists(temp_path):
                        os.remove(temp_path)
            
            # Загружаем текущие файлы
            self.api.upload_file(
                path_or_fileobj=index_path,
                path_in_repo="vector_store/index.faiss",
                repo_id=self.dataset_name,
                repo_type="dataset"
            )
            
            self.api.upload_file(
                path_or_fileobj=config_path,
                path_in_repo="vector_store/index.pkl",
                repo_id=self.dataset_name,
                repo_type="dataset"
            )
            
            # Обновляем метаданные о последнем обновлении
            metadata = {
                "last_update": timestamp,
                "version": "1.0"
            }
            
            with tempfile.NamedTemporaryFile(mode="w+", suffix=".json", delete=False) as temp:
                json.dump(metadata, temp, ensure_ascii=False, indent=2)
                temp_name = temp.name
            
            try:
                self.api.upload_file(
                    path_or_fileobj=temp_name,
                    path_in_repo="vector_store/metadata.json",
                    repo_id=self.dataset_name,
                    repo_type="dataset"
                )
            finally:
                if os.path.exists(temp_name):
                    os.remove(temp_name)
            
            return True, "Векторное хранилище успешно загружено"
        except Exception as e:
            return False, f"Ошибка при загрузке векторного хранилища: {str(e)}"

    def download_vector_store(self, force: bool = False) -> Tuple[bool, str]:
        """
        Загрузка векторного хранилища из датасета
        
        Args:
            force: Принудительная загрузка даже если локальные файлы существуют
            
        Returns:
            (успех, сообщение)
        """
        try:
            # Создаем директорию если её нет
            os.makedirs(VECTOR_STORE_PATH, exist_ok=True)
            
            # Проверяем наличие локальных файлов
            index_path = os.path.join(VECTOR_STORE_PATH, "index.faiss")
            config_path = os.path.join(VECTOR_STORE_PATH, "index.pkl")
            
            if not force and os.path.exists(index_path) and os.path.exists(config_path):
                return True, "Локальные файлы векторного хранилища уже существуют"
            
            # Загружаем файлы
            try:
                # Пробуем получить метаданные для проверки существования файлов
                self.api.hf_hub_download(
                    repo_id=self.dataset_name,
                    filename="vector_store/metadata.json",
                    repo_type="dataset",
                    local_dir=VECTOR_STORE_PATH
                )
                
                # Загружаем файлы векторного хранилища
                self.api.hf_hub_download(
                    repo_id=self.dataset_name,
                    filename="vector_store/index.faiss",
                    repo_type="dataset",
                    local_dir=VECTOR_STORE_PATH
                )
                
                self.api.hf_hub_download(
                    repo_id=self.dataset_name,
                    filename="vector_store/index.pkl",
                    repo_type="dataset",
                    local_dir=VECTOR_STORE_PATH
                )
                
                return True, "Векторное хранилище успешно загружено"
            except Exception as download_error:
                return False, f"Ошибка при загрузке файлов: {str(download_error)}"
        except Exception as e:
            return False, f"Ошибка при загрузке векторного хранилища: {str(e)}"

    def save_chat_history(self, conversation_id: str, messages: List[Dict[str, Any]]) -> Tuple[bool, str]:
        """
        Сохранение истории чата в датасет
        
        Args:
            conversation_id: Идентификатор беседы
            messages: Список сообщений в формате [{role: str, content: str}]
            
        Returns:
            (успех, сообщение)
        """
        try:
            # Формируем имя файла с временной меткой
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"chat_history/{conversation_id}_{timestamp}.json"
            
            # Создаем данные для сохранения
            chat_data = {
                "conversation_id": conversation_id,
                "timestamp": timestamp,
                "messages": messages
            }
            
            # Используем безопасный временный файл
            with tempfile.NamedTemporaryFile(mode="w+", suffix=".json", delete=False, encoding="utf-8") as temp:
                json.dump(chat_data, temp, ensure_ascii=False, indent=2)
                temp_name = temp.name
            
            try:
                # Загружаем файл в датасет
                self.api.upload_file(
                    path_or_fileobj=temp_name,
                    path_in_repo=filename,
                    repo_id=self.dataset_name,
                    repo_type="dataset"
                )
            finally:
                # Удаляем временный файл в любом случае
                if os.path.exists(temp_name):
                    os.remove(temp_name)
            
            return True, "История чата сохранена"
        except Exception as e:
            return False, f"Ошибка при сохранении истории чата: {str(e)}"

    def get_chat_history(self, conversation_id: Optional[str] = None) -> Tuple[bool, Any]:
        """
        Получение истории чатов из датасета
        
        Args:
            conversation_id: Идентификатор беседы (если None, возвращает все чаты)
            
        Returns:
            (успех, история чатов или сообщение об ошибке)
        """
        try:
            # Получаем список файлов в директории chat_history
            files = self.api.list_repo_files(
                repo_id=self.dataset_name,
                repo_type="dataset",
                path="chat_history"
            )
            
            # Фильтруем файлы по conversation_id, если он указан
            if conversation_id:
                files = [f for f in files if f.startswith(f"chat_history/{conversation_id}_")]
            
            # Если файлов нет, возвращаем пустой список
            if not files or all(f.endswith(".gitkeep") for f in files):
                return True, []
            
            # Создаем временную директорию для загрузки файлов
            with tempfile.TemporaryDirectory() as temp_dir:
                chat_histories = []
                
                for file in files:
                    if file.endswith(".gitkeep"):
                        continue
                        
                    # Загружаем файл
                    local_file = self.api.hf_hub_download(
                        repo_id=self.dataset_name,
                        filename=file,
                        repo_type="dataset",
                        local_dir=temp_dir
                    )
                    
                    # Читаем содержимое файла
                    with open(local_file, "r", encoding="utf-8") as f:
                        chat_data = json.load(f)
                        chat_histories.append(chat_data)
                
                # Сортируем по временной метке
                chat_histories.sort(key=lambda x: x.get("timestamp", ""), reverse=True)
                
                return True, chat_histories
        except Exception as e:
            return False, f"Ошибка при получении истории чатов: {str(e)}"

    def upload_document(self, file_path: str, document_id: Optional[str] = None) -> Tuple[bool, str]:
        """
        Загрузка документа в датасет
        
        Args:
            file_path: Путь к файлу документа
            document_id: Идентификатор документа (если None, используется имя файла)
            
        Returns:
            (успех, сообщение)
        """
        try:
            if not os.path.exists(file_path):
                return False, f"Файл не найден: {file_path}"
                
            # Если document_id не указан, используем имя файла
            if document_id is None:
                document_id = os.path.basename(file_path)
                
            # Добавляем временную метку к имени файла
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"documents/{document_id}_{timestamp}{os.path.splitext(file_path)[1]}"
            
            # Загружаем файл
            self.api.upload_file(
                path_or_fileobj=file_path,
                path_in_repo=filename,
                repo_id=self.dataset_name,
                repo_type="dataset"
            )
            
            return True, f"Документ успешно загружен: {filename}"
        except Exception as e:
            return False, f"Ошибка при загрузке документа: {str(e)}"

def test_dataset_connection(token: Optional[str] = None) -> Tuple[bool, str]:
    """
    Тестовая функция для проверки подключения к датасету
    
    Args:
        token: Токен доступа к Hugging Face Hub
        
    Returns:
        (успех, сообщение)
    """
    try:
        manager = DatasetManager(token=token)
        success, message = manager.init_dataset_structure()
        
        if not success:
            return False, message
            
        print(f"Тест инициализации: {message}")
        
        return True, "Подключение к датасету работает"
    except Exception as e:
        return False, f"Ошибка подключения к датасету: {str(e)}"

if __name__ == "__main__":
    # Тестируем подключение
    success, message = test_dataset_connection()
    print(message)