File size: 12,123 Bytes
8f66a5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
File processor class for handling dataset file operations
"""
import os
import json
import time
import asyncio
from pathlib import Path
from typing import Dict, List, Optional, Tuple
from datetime import datetime
import aiohttp
from sqlalchemy.orm import Session
from models import ProcessingState, ProcessingStatusEnum
import logging

logger = logging.getLogger(__name__)


class FileProcessor:
    """Object-oriented file processor for dataset integration"""

    def __init__(self, processed_files_dir: str = "processed_files"):
        """
        Initialize file processor

        Args:
            processed_files_dir: Directory to store processed files
        """
        self.processed_files_dir = Path(processed_files_dir)
        self.all_raw_dir = self.processed_files_dir / "all_raw"
        self.ato_raw_dir = self.processed_files_dir / "ato_raw"

        # Create directories
        self.processed_files_dir.mkdir(parents=True, exist_ok=True)
        self.all_raw_dir.mkdir(parents=True, exist_ok=True)
        self.ato_raw_dir.mkdir(parents=True, exist_ok=True)

    async def download_file(
        self,
        repo_id: str,
        filename: str,
        local_dir: Path,
        token: Optional[str] = None,
    ) -> Optional[str]:
        """
        Download a single file from Hugging Face

        Args:
            repo_id: Repository ID (e.g., "samfred2/ALL")
            filename: File to download
            local_dir: Local directory to save file
            token: Optional HF token for authentication

        Returns:
            Path to downloaded file or None if failed
        """
        try:
            logger.info(f"Downloading {filename} from {repo_id}...")
            await asyncio.sleep(1)  # Rate limiting

            local_path = local_dir / filename
            local_path.parent.mkdir(parents=True, exist_ok=True)

            url = f"https://huggingface.co/api/datasets/{repo_id}/resolve/main/{filename}"
            headers = {"Authorization": f"Bearer {token}"} if token else {}

            async with aiohttp.ClientSession() as session:
                async with session.get(url, headers=headers) as response:
                    if response.status != 200:
                        logger.error(
                            f"Failed to download {filename}: HTTP {response.status}"
                        )
                        return None

                    content = await response.read()
                    local_path.write_bytes(content)
                    logger.info(f"Downloaded to {local_path}")
                    return str(local_path)

        except Exception as e:
            logger.error(f"Error downloading {filename}: {e}")
            return None

    def load_json_file(self, file_path: str) -> Optional[Dict]:
        """
        Load and parse JSON file

        Args:
            file_path: Path to JSON file

        Returns:
            Parsed JSON data or None if failed
        """
        try:
            with open(file_path, "r") as f:
                return json.load(f)
        except Exception as e:
            logger.error(f"Error loading JSON from {file_path}: {e}")
            return None

    def find_matching_all_file(
        self, ato_filename: str, all_filenames: List[str]
    ) -> Optional[str]:
        """
        Find matching ALL file for ATO file using suffix matching

        Args:
            ato_filename: ATO filename to match
            all_filenames: List of ALL filenames

        Returns:
            Matching ALL filename or None
        """
        for all_name in all_filenames:
            if all_name.endswith(ato_filename):
                return all_name
        return None

    async def list_json_files(
        self, repo_id: str, token: Optional[str] = None
    ) -> List[str]:
        """
        List all JSON files in a repository

        Args:
            repo_id: Repository ID
            token: Optional HF token

        Returns:
            List of JSON filenames
        """
        try:
            url = f"https://huggingface.co/api/datasets/{repo_id}"
            headers = {"Authorization": f"Bearer {token}"} if token else {}

            async with aiohttp.ClientSession() as session:
                async with session.get(url, headers=headers) as response:
                    if response.status != 200:
                        logger.error(f"Failed to list files from {repo_id}")
                        return []

                    data = await response.json()
                    siblings = data.get("siblings", [])

                    return [
                        f["rfilename"]
                        for f in siblings
                        if f["rfilename"].endswith(".json")
                    ]

        except Exception as e:
            logger.error(f"Error listing files from {repo_id}: {e}")
            return []

    async def process_datasets(
        self,
        all_repo_id: str,
        ato_repo_id: str,
        hf_token: Optional[str] = None,
        max_files: int = 0,
        db: Optional[Session] = None,
    ) -> Dict:
        """
        Process datasets: download, match, integrate, and save

        Args:
            all_repo_id: Source ALL repository ID
            ato_repo_id: Source ATO repository ID
            hf_token: Optional HF token
            max_files: Maximum files to process (0 = all)
            db: Database session for state tracking

        Returns:
            Processing result dictionary
        """
        try:
            # Update state to downloading
            if db:
                state = db.query(ProcessingState).first()
                if not state:
                    state = ProcessingState(status=ProcessingStatusEnum.DOWNLOADING)
                    db.add(state)
                else:
                    state.status = ProcessingStatusEnum.DOWNLOADING
                    state.started_at = datetime.utcnow()
                db.commit()

            logger.info("Listing repository files...")
            all_files = await self.list_json_files(all_repo_id, hf_token)
            ato_files = await self.list_json_files(ato_repo_id, hf_token)

            logger.info(f"Found {len(all_files)} files in {all_repo_id}")
            logger.info(f"Found {len(ato_files)} files in {ato_repo_id}")

            # Match files
            logger.info("Matching ATO to ALL files...")
            match_map: Dict[str, str] = {}
            for ato_file in ato_files:
                matching_all = self.find_matching_all_file(ato_file, all_files)
                if matching_all:
                    match_map[ato_file] = matching_all

            matched_count = len(match_map)
            logger.info(f"Found {matched_count} matching pairs")

            if db:
                state = db.query(ProcessingState).first()
                if state:
                    state.status = ProcessingStatusEnum.MATCHING
                    state.total_files = len(ato_files)
                    state.matched_pairs = matched_count
                    db.commit()

            # Process matched files
            logger.info("Processing matched files...")
            processed_count = 0

            for ato_filename, all_filename in match_map.items():
                if max_files > 0 and processed_count >= max_files:
                    logger.info(f"Reached limit of {max_files} files")
                    break

                logger.info(f"Processing: {ato_filename} <-> {all_filename}")

                # Download ATO file
                ato_path = await self.download_file(
                    ato_repo_id, ato_filename, self.ato_raw_dir, hf_token
                )
                if not ato_path:
                    continue

                ato_data = self.load_json_file(ato_path)
                if not ato_data:
                    continue

                # Download ALL file
                all_path = await self.download_file(
                    all_repo_id, all_filename, self.all_raw_dir, hf_token
                )
                if not all_path:
                    continue

                all_data = self.load_json_file(all_path)
                if not all_data:
                    continue

                # Integrate transcription
                logger.info("Integrating transcription...")
                all_data["transcription_content"] = ato_data
                all_data["transcription_content"]["full_course_name"] = all_filename

                # Save integrated file
                output_path = self.processed_files_dir / all_filename
                output_path.parent.mkdir(parents=True, exist_ok=True)
                with open(output_path, "w") as f:
                    json.dump(all_data, f, indent=4)

                logger.info(f"Saved integrated file to {output_path}")
                processed_count += 1

                if db:
                    state = db.query(ProcessingState).first()
                    if state:
                        state.status = ProcessingStatusEnum.INTEGRATING
                        state.processed_files = processed_count
                        db.commit()

            logger.info("Processing complete")

            if db:
                state = db.query(ProcessingState).first()
                if state:
                    state.status = ProcessingStatusEnum.COMPLETED
                    state.completed_at = datetime.utcnow()
                    db.commit()

            return {
                "success": True,
                "total_files": len(ato_files),
                "matched_pairs": matched_count,
                "processed_files": processed_count,
            }

        except Exception as e:
            logger.error(f"Processing error: {e}")

            if db:
                state = db.query(ProcessingState).first()
                if state:
                    state.status = ProcessingStatusEnum.ERROR
                    state.error_message = str(e)
                    state.completed_at = datetime.utcnow()
                    db.commit()

            return {
                "success": False,
                "total_files": 0,
                "matched_pairs": 0,
                "processed_files": 0,
                "error": str(e),
            }

    def get_processed_files(self) -> List[str]:
        """
        Get list of processed files ready for upload

        Returns:
            List of relative file paths
        """
        files = []

        def walk_dir(directory: Path, prefix: str = ""):
            for item in directory.iterdir():
                relative_path = f"{prefix}/{item.name}" if prefix else item.name

                if item.is_dir():
                    walk_dir(item, relative_path)
                elif item.suffix == ".json":
                    files.append(relative_path)

        walk_dir(self.processed_files_dir)
        return files

    def get_file_content(self, filename: str) -> Optional[Dict]:
        """
        Get file content for preview

        Args:
            filename: Relative filename

        Returns:
            File content or None
        """
        file_path = self.processed_files_dir / filename

        # Security: prevent directory traversal
        try:
            file_path.resolve().relative_to(self.processed_files_dir.resolve())
        except ValueError:
            logger.warning(f"Directory traversal attempt: {filename}")
            return None

        if not file_path.exists():
            return None

        return self.load_json_file(str(file_path))

    def get_file_size(self, filename: str) -> Optional[int]:
        """
        Get file size in bytes

        Args:
            filename: Relative filename

        Returns:
            File size or None
        """
        file_path = self.processed_files_dir / filename

        try:
            file_path.resolve().relative_to(self.processed_files_dir.resolve())
        except ValueError:
            return None

        if not file_path.exists():
            return None

        return file_path.stat().st_size