-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
630 lines (508 loc) · 20.5 KB
/
Copy pathapi.py
File metadata and controls
630 lines (508 loc) · 20.5 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
"""
AI Chat Log Converter - FastAPI Backend API
License: MIT
A RESTful API service for converting AI chat logs to various formats.
Supports file upload, automatic column detection, batch processing, and file download.
Features:
- Single file processing (extract, classify, finetune, context, openai)
- Batch file processing with auto-detection
- Agent preview and matching
- Multi-language support (Chinese/English)
- Temporary file management
- CORS enabled for frontend integration
Usage:
uvicorn api:app --reload --host 127.0.0.1 --port 8010
API Endpoints:
GET /api/ - API information
POST /api/upload - Upload and parse file
POST /api/preview - Preview agent matching
POST /api/process - Process single file
POST /api/batch - Process multiple files
GET /api/download/{filename} - Download processed file
DELETE /api/cleanup - Clean temporary files
"""
import os
import tempfile
import shutil
from typing import List, Optional
from pathlib import Path
from datetime import datetime
from contextlib import asynccontextmanager
import logging
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
# Import core logic module
from core import (
parse_file, auto_detect_columns, extract_agent, classify_agents,
convert_to_json, batch_process, batch_process_auto, preview_match,
ProcessResult, set_language, t
)
# Configure logger
logger = logging.getLogger(__name__)
# ============ Application Lifecycle Management ============
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Manage application lifecycle with proper startup and shutdown.
This replaces the deprecated on_event decorators with modern lifespan context manager.
"""
# Startup
TEMP_DIR.mkdir(exist_ok=True)
OUTPUT_DIR.mkdir(exist_ok=True)
# Clean up old temporary files on startup
cleanup_old_files(max_age_hours=24)
print(f"✓ AI Chat Log Converter API started")
print(f"✓ Temp directory: {TEMP_DIR}")
print(f"✓ Output directory: {OUTPUT_DIR}")
yield
# Shutdown
# Clean up temporary files on shutdown
cleanup_old_files(max_age_hours=1)
print("✓ AI Chat Log Converter API shutting down")
# ============ Application Initialization ============
app = FastAPI(
title="AI Chat Log Converter API",
description="Process and transform AI chat logs - Group by Agent · Local Processing · Cross-platform",
version="1.0.0",
docs_url="/api/docs",
redoc_url="/api/redoc",
lifespan=lifespan
)
# CORS Configuration - Allow all origins for development
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=True,
)
# ============ Temporary File Management ============
TEMP_DIR = Path(tempfile.gettempdir()) / "ai_chat_converter"
TEMP_DIR.mkdir(exist_ok=True)
# Output directory: use current working directory's output folder for easy access
OUTPUT_DIR = Path.cwd() / "output"
OUTPUT_DIR.mkdir(exist_ok=True)
def cleanup_old_files(max_age_hours: int = 24):
"""
Clean up temporary files older than specified hours.
Args:
max_age_hours: Maximum age in hours before files are cleaned
"""
if not TEMP_DIR.exists():
return
current_time = datetime.now()
for item in TEMP_DIR.rglob("*"):
if item.is_file():
file_age = current_time - datetime.fromtimestamp(item.stat().st_mtime)
if file_age.total_seconds() > max_age_hours * 3600:
try:
item.unlink()
except Exception:
pass
# ============ Helper Functions ============
def result_to_dict(result: ProcessResult) -> dict:
"""
Convert ProcessResult to dictionary for JSON response.
Args:
result: ProcessResult object from processing
Returns:
Dictionary representation of the result
"""
# Exclude only filenames from full paths for security and consistency
safe_files = [os.path.basename(f) if os.path.isabs(f) else f for f in (result.files or [])]
return {
"success": result.success,
"message": result.message,
"files": safe_files,
"data": result.data
}
def save_upload(file: UploadFile) -> Path:
"""
Save uploaded file to temporary directory with unique name.
Args:
file: Uploaded file from request
Returns:
Path to saved temporary file
"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
safe_filename = f"{timestamp}_{file.filename.replace('/', '_').replace('\\', '_')}"
file_path = TEMP_DIR / safe_filename
with open(file_path, "wb") as f:
shutil.copyfileobj(file.file, f)
return file_path
# ============ Validation Functions ============
def validate_file_format(filename: str) -> bool:
"""
Validate that the file has a supported format.
Args:
filename: Name of the file to validate
Returns:
True if format is supported (.csv or .txt)
"""
return filename.lower().endswith(('.csv', '.txt'))
def validate_processing_mode(mode: str, target: Optional[str] = None,
role_col: Optional[str] = None,
content_col: Optional[str] = None) -> None:
"""
Validate processing mode and required parameters.
Centralized validation logic for both single and batch processing endpoints.
Raises HTTPException if validation fails.
Args:
mode: Processing mode (extract, classify, finetune, context, openai)
target: Target agent name (required for extract mode)
role_col: Role column name (required for JSON modes)
content_col: Content column name (required for JSON modes)
Raises:
HTTPException: If validation fails with appropriate error message
"""
# Check valid mode
if mode not in ["extract", "classify", "finetune", "context", "openai"]:
raise HTTPException(status_code=400, detail=t('error_unknown_mode').format(mode))
# Extract mode requires target
if mode == "extract" and not target:
raise HTTPException(status_code=400, detail=t('input_target'))
# JSON modes require content_col and role_col
if mode in ["finetune", "context", "openai"]:
if not content_col:
raise HTTPException(status_code=400, detail=t('select_content_col'))
if not role_col:
raise HTTPException(status_code=400, detail=t('select_required'))
# ============ API Endpoints ============
@app.get("/api/")
async def root():
"""
Get API information and available modes.
Returns:
API metadata including name, version, and supported processing modes
"""
return {
"name": "AI Chat Log Converter API",
"version": "1.0.0",
"description": "Process and transform AI chat logs",
"modes": {
"extract": "Extract records for a specific agent",
"classify": "Split records by agent into separate files",
"finetune": "Convert to JSON with full metadata (message_id, turn_id, token_count)",
"context": "Convert to simplified JSON format (role, content, timestamp)",
"openai": "Convert to OpenAI fine-tuning format (JSONL, pure messages)"
},
"endpoints": {
"upload": "POST /api/upload - Upload and parse file",
"preview": "POST /api/preview - Preview agent matching",
"process": "POST /api/process - Process single file",
"batch": "POST /api/batch - Process multiple files",
"download": "GET /api/download/{filename} - Download processed file"
}
}
@app.post("/api/upload")
async def upload_file(
file: UploadFile = File(..., description="CSV or TXT file to upload"),
lang: str = Form("zh", description="Language code (zh or en)")
):
"""
Upload a file and automatically detect column mappings.
Parses the uploaded file, detects delimiter, and attempts to identify
key columns (agent, role, content, time) automatically.
Args:
file: Uploaded CSV/TXT file
lang: Language for messages (default: zh)
Returns:
Parsed file information including headers, detected columns, and preview
Raises:
HTTPException: If file format is invalid or parsing fails
"""
try:
# Validate file format
if not validate_file_format(file.filename or ""):
raise HTTPException(
status_code=400,
detail=t('error_load') + ": Unsupported file format. Only .csv and .txt are supported."
)
set_language(lang)
file_path = save_upload(file)
# Parse file and detect columns
result = parse_file(str(file_path))
detected = auto_detect_columns(result)
# Build response
response_data = {
"success": True,
"message": t('auto_detect_ok'),
"file_info": {
"filename": file.filename or "",
"total_rows": len(result.rows),
"total_cols": len(result.headers),
"delimiter": repr(result.delimiter)
},
"detected": {
"agent_col": detected.agent_col,
"role_col": detected.role_col,
"content_col": detected.content_col,
"time_col": detected.time_col
},
"headers": result.headers,
"preview": result.rows[:5]
}
return response_data
except ValueError as e:
logger.error(f"ValueError in upload: {e}", exc_info=True)
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error(f"Upload failed: {type(e).__name__}: {e}", exc_info=True)
import traceback
logger.error(f"Traceback: {traceback.format_exc()}")
raise HTTPException(status_code=500, detail=f"{t('error_load')}: {str(e)}")
@app.post("/api/preview")
async def preview_agents(
file: UploadFile = File(..., description="CSV or TXT file to preview"),
agent_col: str = Form(..., description="Agent column name"),
target: str = Form(..., description="Target agent name to match"),
lang: str = Form("zh", description="Language code (zh or en)")
):
"""
Preview agents matching a target string.
Performs case-insensitive substring matching to show which agent
names will be matched before actual extraction.
Args:
file: Uploaded CSV/TXT file
agent_col: Name of the agent column
target: Target string to match against agent names
lang: Language for messages (default: zh)
Returns:
List of matched agent names and count
Raises:
HTTPException: If parsing or matching fails
"""
try:
set_language(lang)
file_path = save_upload(file)
result = parse_file(str(file_path))
matched = preview_match(result, agent_col, target)
return {
"success": True,
"matched": matched,
"count": len(matched),
"target": target,
"message": t('matched') + str(len(matched)) if matched else t('not_found')
}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/api/process")
async def process_file(
file: UploadFile = File(..., description="CSV or TXT file to process"),
mode: str = Form(..., description="Processing mode: extract, classify, finetune, context, or openai"),
agent_col: str = Form(..., description="Agent column name"),
role_col: Optional[str] = Form(None, description="Role column name (required for finetune/context/openai)"),
content_col: Optional[str] = Form(None, description="Content column name (required for finetune/context/openai)"),
time_col: Optional[str] = Form(None, description="Timestamp column name (optional)"),
target: Optional[str] = Form(None, description="Target agent name (required for extract mode)"),
reverse: bool = Form(False, description="Reverse message order (for JSON modes)"),
lang: str = Form("zh", description="Language code (zh or en)")
):
"""
Process a single file with specified mode and column mappings.
Supported modes:
- extract: Extract records for a specific agent (requires target)
- classify: Split records by agent into separate CSV files
- finetune: Convert to JSON with full metadata
- context: Convert to simplifiedJSON format
- openai: Convert to OpenAI fine-tuning format (JSONL)
Args:
file: Uploaded CSV/TXT file
mode: Processing mode
agent_col: Agent column name
role_col: Role column name (for JSON modes)
content_col: Content column name (for JSON modes)
time_col: Timestamp column name (optional)
target: Target agent name (for extract mode)
reverse: Reverse message order (for JSON modes)
lang: Language for messages (default: zh)
Returns:
Processing result with output file information
Raises:
HTTPException: If validation fails or processing encounters errors
"""
try:
# Validate inputs using centralized validation
validate_processing_mode(
mode=mode,
target=target,
role_col=role_col,
content_col=content_col
)
set_language(lang)
file_path = save_upload(file)
result = parse_file(str(file_path))
base = Path(file_path).stem
out_dir = OUTPUT_DIR
out_dir.mkdir(exist_ok=True)
# Process based on mode
if mode == "extract":
# Include agent name in filename for better identification
safe_target = "".join(c if c.isalnum() or c in "_-" else "_" for c in target)
save_path = out_dir / f"{base}_{safe_target}.csv"
proc_result = extract_agent(result, agent_col, target, str(save_path))
elif mode == "classify":
proc_result = classify_agents(result, agent_col, str(out_dir))
elif mode in ["finetune", "context", "openai"]:
# Determine file extension based on mode
ext = "jsonl" if mode == "openai" else "json"
save_path = out_dir / f"{base}_{mode}.{ext}"
proc_result = convert_to_json(
result=result, agent_col=agent_col,
role_col=role_col or "", content_col=content_col,
time_col=time_col, mode=mode,
save_path=str(save_path), reverse=reverse
)
return result_to_dict(proc_result)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{t('error')}: {str(e)}")
@app.post("/api/batch")
async def batch_process_api(
files: List[UploadFile] = File(..., description="Multiple CSV or TXT files to process"),
mode: str = Form(..., description="Processing mode: extract, classify, finetune, context, or openai"),
agent_col: Optional[str] = Form(None,
description="Agent column name (optional, auto-detected if not provided)"),
role_col: Optional[str] = Form(None, description="Role column name (for JSON modes)"),
content_col: Optional[str] = Form(None, description="Content column name (for JSON modes)"),
time_col: Optional[str] = Form(None, description="Timestamp column name (optional)"),
target: Optional[str] = Form(None, description="Target agent name (for extract mode)"),
reverse: bool = Form(False, description="Reverse message order (for JSON modes)"),
auto_detect: bool = Form(True, description="Use automatic column detection"),
lang: str = Form("zh", description="Language code (zh or en)")
):
"""
Process multiple files with automatic or manual column detection.
When auto_detect is True, detects columns from the first file and applies
to all files. When False, uses manually specified column names.
Args:
files: List of uploaded CSV/TXT files
mode: Processing mode
agent_col: Agent column name (for manual mode)
role_col: Role column name (for manual mode)
content_col: Content column name (for manual mode)
time_col: Timestamp column name (optional)
target: Target agent name (for extract mode)
reverse: Reverse message order (for JSON modes)
auto_detect: Use automatic detection (default: True)
lang: Language for messages (default: zh)
Returns:
Batch processing results with individual file results
Raises:
HTTPException: If validation fails or processing encounters errors
"""
try:
# Validate inputs
validate_processing_mode(
mode=mode,
target=target,
role_col=role_col,
content_col=content_col
)
set_language(lang)
# Save all uploaded files
file_paths = []
for f in files:
if not validate_file_format(f.filename or ""):
continue
file_paths.append(str(save_upload(f)))
if not file_paths:
raise HTTPException(status_code=400, detail="No valid files provided")
out_dir = str(OUTPUT_DIR)
# Process files
if auto_detect:
results = batch_process_auto(
file_paths, mode, target=target,
out_dir=out_dir, reverse=reverse
)
else:
results = batch_process(
file_paths, mode,
agent_col=agent_col, role_col=role_col,
content_col=content_col, time_col=time_col,
target=target, out_dir=out_dir, reverse=reverse
)
success_count = sum(1 for r in results if r.success)
return {
"success": True,
"total": len(results),
"success_count": success_count,
"failed_count": len(results) - success_count,
"results": [result_to_dict(r) for r in results]
}
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=f"{t('error')}: {str(e)}")
@app.get("/api/download/{filename}")
async def download_file(filename: str):
"""
Download a processed file from the output directory.
Args:
filename: Name of the file to download (from output directory)
Returns:
File response for download
Raises:
HTTPException: If file does not exist or path is invalid
"""
# Security: Only allow files from output directory
file_path = OUTPUT_DIR / filename
# Prevent directory traversal attacks
if not file_path.exists() or not file_path.is_file():
raise HTTPException(status_code=404, detail="File not found")
# Verify file is within output directory
try:
file_path.resolve().relative_to(OUTPUT_DIR.resolve())
except ValueError:
raise HTTPException(status_code=403, detail="Access denied")
return FileResponse(
file_path,
filename=filename,
media_type="application/octet-stream"
)
@app.delete("/api/cleanup")
async def cleanup_files(max_age_hours: int = 24):
"""
Clean up temporary files older than specified hours.
Args:
max_age_hours: Maximum age in hours before cleanup (default: 24)
Returns:
Cleanup status message
"""
try:
cleanup_old_files(max_age_hours)
return {
"success": True,
"message": f"Cleaned up files older than {max_age_hours} hours"
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/health")
async def health_check():
"""
Health check endpoint for monitoring.
Returns:
Service health status
"""
return {
"status": "healthy",
"temp_dir": str(TEMP_DIR),
"output_dir": str(OUTPUT_DIR),
"timestamp": datetime.now().isoformat()
}
# ============ Static Files (Web Frontend) ============
static_path = Path(__file__).parent / "static"
if static_path.exists():
app.mount("/", StaticFiles(directory=str(static_path), html=True), name="static")
# ============ Main Entry Point ============
if __name__ == "__main__":
import uvicorn
uvicorn.run(
app,
host="127.0.0.1",
port=8010,
log_level="info"
)