File size: 20,529 Bytes
968c919 |
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 |
#!/usr/bin/env python3
"""
File Upload Interface
====================
Web-based file upload interface for high capacity input processing.
"""
from flask import Flask, request, jsonify, render_template_string, redirect, url_for
import os
import json
from pathlib import Path
from werkzeug.utils import secure_filename
from high_capacity_input_processor import HighCapacityInputProcessor
import threading
import time
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024 # 100MB max file size
# Initialize processor
processor = HighCapacityInputProcessor()
# HTML template for the upload interface
UPLOAD_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LiMp High Capacity Input Processor</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.header {
text-align: center;
margin-bottom: 30px;
}
.header h1 {
color: #333;
margin-bottom: 10px;
}
.header p {
color: #666;
font-size: 16px;
}
.upload-section {
border: 2px dashed #ddd;
border-radius: 10px;
padding: 40px;
text-align: center;
margin-bottom: 30px;
transition: border-color 0.3s;
}
.upload-section:hover {
border-color: #4CAF50;
}
.upload-section.dragover {
border-color: #4CAF50;
background-color: #f0f8f0;
}
.file-input {
margin: 20px 0;
}
.file-input input[type="file"] {
display: none;
}
.file-input label {
display: inline-block;
padding: 12px 24px;
background-color: #4CAF50;
color: white;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
.file-input label:hover {
background-color: #45a049;
}
.text-input {
margin: 20px 0;
}
.text-input textarea {
width: 100%;
height: 200px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
font-family: monospace;
font-size: 14px;
resize: vertical;
}
.submit-btn {
background-color: #2196F3;
color: white;
padding: 15px 30px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
margin: 10px 5px;
transition: background-color 0.3s;
}
.submit-btn:hover {
background-color: #1976D2;
}
.submit-btn:disabled {
background-color: #ccc;
cursor: not-allowed;
}
.progress {
width: 100%;
height: 20px;
background-color: #f0f0f0;
border-radius: 10px;
overflow: hidden;
margin: 20px 0;
display: none;
}
.progress-bar {
height: 100%;
background-color: #4CAF50;
width: 0%;
transition: width 0.3s;
}
.results {
margin-top: 30px;
padding: 20px;
background-color: #f9f9f9;
border-radius: 5px;
display: none;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin: 20px 0;
}
.stat-card {
background: white;
padding: 20px;
border-radius: 5px;
text-align: center;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.stat-number {
font-size: 24px;
font-weight: bold;
color: #4CAF50;
}
.stat-label {
color: #666;
margin-top: 5px;
}
.file-list {
margin-top: 20px;
}
.file-item {
background: white;
padding: 15px;
margin: 10px 0;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.file-name {
font-weight: bold;
color: #333;
}
.file-info {
color: #666;
font-size: 14px;
margin-top: 5px;
}
.chunk-info {
color: #888;
font-size: 12px;
margin-top: 5px;
}
.error {
color: #f44336;
background-color: #ffebee;
padding: 15px;
border-radius: 5px;
margin: 20px 0;
}
.success {
color: #4CAF50;
background-color: #e8f5e8;
padding: 15px;
border-radius: 5px;
margin: 20px 0;
}
.download-btn {
background-color: #FF9800;
color: white;
padding: 8px 16px;
border: none;
border-radius: 3px;
font-size: 12px;
cursor: pointer;
margin-top: 10px;
}
.download-btn:hover {
background-color: #F57C00;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>π§ LiMp High Capacity Input Processor</h1>
<p>Upload files or enter large text for intelligent chunking and training data generation</p>
</div>
<form id="uploadForm" enctype="multipart/form-data">
<div class="upload-section" id="uploadSection">
<h3>π File Upload</h3>
<p>Drag and drop files here or click to select</p>
<div class="file-input">
<label for="fileInput">Choose Files</label>
<input type="file" id="fileInput" name="files" multiple accept=".txt,.md,.py,.js,.html,.css,.json,.jsonl,.csv,.pdf,.doc,.docx,.xml,.yaml,.yml">
</div>
<p><small>Supported formats: TXT, MD, PY, JS, HTML, CSS, JSON, CSV, PDF, DOC, DOCX, XML, YAML</small></p>
</div>
<div class="text-input">
<h3>π Large Text Input</h3>
<textarea id="textInput" name="text" placeholder="Enter large text content here (up to 100MB)..."></textarea>
<p><small>Character count: <span id="charCount">0</span></small></p>
</div>
<div>
<button type="submit" class="submit-btn" id="submitBtn">Process Input</button>
<button type="button" class="submit-btn" onclick="generateTrainingData()">Generate Training Data</button>
<button type="button" class="submit-btn" onclick="clearAll()">Clear All</button>
</div>
<div class="progress" id="progress">
<div class="progress-bar" id="progressBar"></div>
</div>
</form>
<div class="results" id="results">
<h3>π Processing Results</h3>
<div id="resultsContent"></div>
</div>
</div>
<script>
let uploads = [];
// File input handling
const fileInput = document.getElementById('fileInput');
const uploadSection = document.getElementById('uploadSection');
const textInput = document.getElementById('textInput');
const charCount = document.getElementById('charCount');
const submitBtn = document.getElementById('submitBtn');
const progress = document.getElementById('progress');
const progressBar = document.getElementById('progressBar');
const results = document.getElementById('results');
const resultsContent = document.getElementById('resultsContent');
// Character count update
textInput.addEventListener('input', function() {
charCount.textContent = this.value.length.toLocaleString();
});
// Drag and drop handling
uploadSection.addEventListener('dragover', function(e) {
e.preventDefault();
this.classList.add('dragover');
});
uploadSection.addEventListener('dragleave', function(e) {
e.preventDefault();
this.classList.remove('dragover');
});
uploadSection.addEventListener('drop', function(e) {
e.preventDefault();
this.classList.remove('dragover');
fileInput.files = e.dataTransfer.files;
updateFileList();
});
fileInput.addEventListener('change', updateFileList);
function updateFileList() {
const files = fileInput.files;
if (files.length > 0) {
let fileList = '<h4>Selected Files:</h4>';
for (let file of files) {
fileList += `<div class="file-item">
<div class="file-name">${file.name}</div>
<div class="file-info">Size: ${(file.size / 1024 / 1024).toFixed(2)} MB, Type: ${file.type}</div>
</div>`;
}
uploadSection.innerHTML = fileList + uploadSection.innerHTML;
}
}
// Form submission
document.getElementById('uploadForm').addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData();
const files = fileInput.files;
const text = textInput.value.trim();
if (files.length === 0 && text === '') {
alert('Please select files or enter text content');
return;
}
// Add files
for (let file of files) {
formData.append('files', file);
}
// Add text
if (text) {
formData.append('text', text);
}
submitBtn.disabled = true;
progress.style.display = 'block';
progressBar.style.width = '0%';
try {
// Simulate progress
let progressValue = 0;
const progressInterval = setInterval(() => {
progressValue += Math.random() * 15;
if (progressValue > 90) progressValue = 90;
progressBar.style.width = progressValue + '%';
}, 200);
const response = await fetch('/upload', {
method: 'POST',
body: formData
});
clearInterval(progressInterval);
progressBar.style.width = '100%';
const result = await response.json();
if (result.success) {
uploads = result.uploads || [];
showResults(result);
} else {
showError(result.error);
}
} catch (error) {
showError('Upload failed: ' + error.message);
} finally {
submitBtn.disabled = false;
setTimeout(() => {
progress.style.display = 'none';
progressBar.style.width = '0%';
}, 1000);
}
});
async function generateTrainingData() {
if (uploads.length === 0) {
alert('No uploads available. Please upload files or enter text first.');
return;
}
try {
const response = await fetch('/generate_training_data', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ file_ids: uploads.map(u => u.file_id) })
});
const result = await response.json();
if (result.success) {
showSuccess(`Training data generated: ${result.training_data_file}`);
// Add download link
const downloadLink = document.createElement('a');
downloadLink.href = `/download/${result.training_data_file}`;
downloadLink.textContent = 'Download Training Data';
downloadLink.className = 'download-btn';
resultsContent.appendChild(downloadLink);
} else {
showError(result.error);
}
} catch (error) {
showError('Training data generation failed: ' + error.message);
}
}
function showResults(data) {
let html = '<div class="success">Processing completed successfully!</div>';
// Statistics
html += '<div class="stats">';
html += `<div class="stat-card">
<div class="stat-number">${data.stats.total_files}</div>
<div class="stat-label">Files Processed</div>
</div>`;
html += `<div class="stat-card">
<div class="stat-number">${data.stats.total_chunks}</div>
<div class="stat-label">Chunks Created</div>
</div>`;
html += `<div class="stat-card">
<div class="stat-number">${data.stats.total_size_mb.toFixed(2)}</div>
<div class="stat-label">Total Size (MB)</div>
</div>`;
html += '</div>';
// File list
if (data.uploads && data.uploads.length > 0) {
html += '<div class="file-list">';
html += '<h4>Processed Files:</h4>';
for (let upload of data.uploads) {
html += `<div class="file-item">
<div class="file-name">${upload.filename}</div>
<div class="file-info">Size: ${(upload.file_size / 1024 / 1024).toFixed(2)} MB, Type: ${upload.mime_type}</div>
<div class="chunk-info">Chunks: ${upload.chunks.length}</div>
</div>`;
}
html += '</div>';
}
resultsContent.innerHTML = html;
results.style.display = 'block';
}
function showError(message) {
resultsContent.innerHTML = `<div class="error">Error: ${message}</div>`;
results.style.display = 'block';
}
function showSuccess(message) {
resultsContent.innerHTML = `<div class="success">${message}</div>`;
results.style.display = 'block';
}
function clearAll() {
fileInput.value = '';
textInput.value = '';
charCount.textContent = '0';
uploads = [];
results.style.display = 'none';
uploadSection.innerHTML = `
<h3>π File Upload</h3>
<p>Drag and drop files here or click to select</p>
<div class="file-input">
<label for="fileInput">Choose Files</label>
<input type="file" id="fileInput" name="files" multiple accept=".txt,.md,.py,.js,.html,.css,.json,.jsonl,.csv,.pdf,.doc,.docx,.xml,.yaml,.yml">
</div>
<p><small>Supported formats: TXT, MD, PY, JS, HTML, CSS, JSON, CSV, PDF, DOC, DOCX, XML, YAML</small></p>
`;
}
</script>
</body>
</html>
"""
@app.route('/')
def index():
"""Main upload page."""
return render_template_string(UPLOAD_TEMPLATE)
@app.route('/upload', methods=['POST'])
def upload_files():
"""Handle file uploads and text input."""
try:
uploads = []
# Process uploaded files
if 'files' in request.files:
files = request.files.getlist('files')
for file in files:
if file.filename:
# Save uploaded file temporarily
filename = secure_filename(file.filename)
temp_path = Path(processor.upload_dir) / filename
file.save(str(temp_path))
# Process file
file_upload = processor.process_file_upload(temp_path)
uploads.append({
'file_id': file_upload.file_id,
'filename': file_upload.filename,
'file_size': file_upload.file_size,
'mime_type': file_upload.mime_type,
'chunks': len(file_upload.chunks)
})
# Process text input
text_content = request.form.get('text', '').strip()
if text_content:
chunks = processor.process_high_capacity_input(text_content)
uploads.append({
'file_id': 'text_input_' + str(int(time.time())),
'filename': 'text_input.txt',
'file_size': len(text_content),
'mime_type': 'text/plain',
'chunks': len(chunks)
})
# Get processing stats
stats = processor.get_processing_stats()
return jsonify({
'success': True,
'uploads': uploads,
'stats': stats
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 400
@app.route('/generate_training_data', methods=['POST'])
def generate_training_data():
"""Generate training data from processed uploads."""
try:
data = request.get_json()
file_ids = data.get('file_ids', [])
# Get all uploads
all_uploads = processor.get_all_uploads()
# Filter by file IDs if provided
if file_ids:
filtered_uploads = [upload for upload in all_uploads if upload.file_id in file_ids]
else:
filtered_uploads = all_uploads
# Generate training data
training_data_file = processor.create_training_data_from_chunks(
filtered_uploads,
output_format='jsonl',
include_metadata=True
)
return jsonify({
'success': True,
'training_data_file': Path(training_data_file).name,
'file_path': training_data_file,
'total_examples': sum(len(upload.chunks) for upload in filtered_uploads)
})
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 400
@app.route('/download/<filename>')
def download_file(filename):
"""Download generated training data file."""
file_path = processor.training_data_dir / filename
if file_path.exists():
return send_file(str(file_path), as_attachment=True)
else:
return "File not found", 404
@app.route('/stats')
def get_stats():
"""Get processing statistics."""
stats = processor.get_processing_stats()
return jsonify(stats)
@app.route('/uploads')
def list_uploads():
"""List all uploads."""
uploads = processor.get_all_uploads()
return jsonify([{
'file_id': upload.file_id,
'filename': upload.filename,
'file_size': upload.file_size,
'mime_type': upload.mime_type,
'upload_timestamp': upload.upload_timestamp,
'chunks': len(upload.chunks)
} for upload in uploads])
if __name__ == '__main__':
print("π Starting LiMp High Capacity Input Processor")
print("π Upload directory:", processor.upload_dir)
print("π Chunk directory:", processor.chunk_dir)
print("π Training data directory:", processor.training_data_dir)
print("π Web interface: http://localhost:5000")
app.run(debug=True, host='0.0.0.0', port=5000)
|