-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
351 lines (296 loc) · 12.8 KB
/
Copy pathapp.py
File metadata and controls
351 lines (296 loc) · 12.8 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
import os
import hashlib
import json
from flask import Flask, render_template, request, jsonify, send_file
from werkzeug.utils import secure_filename
import torch
import torchvision.transforms as transforms
from torchvision import models
from PIL import Image
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
from datetime import datetime
import shutil
from pathlib import Path
import send2trash # For Recycle Bin support
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['MAX_CONTENT_LENGTH'] = 500 * 1024 * 1024 # 500MB max
app.config['THUMBNAIL_FOLDER'] = 'static/thumbnails'
# Create necessary directories
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['THUMBNAIL_FOLDER'], exist_ok=True)
# Initialize AI model for image analysis
class ImageFeatureExtractor:
def __init__(self):
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
self.model = models.mobilenet_v2(pretrained=True)
self.model = torch.nn.Sequential(*list(self.model.children())[:-1])
self.model = self.model.to(self.device)
self.model.eval()
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
def extract_features(self, image_path):
try:
image = Image.open(image_path).convert('RGB')
image_tensor = self.transform(image).unsqueeze(0).to(self.device)
with torch.no_grad():
features = self.model(image_tensor)
return features.cpu().numpy().flatten()
except Exception as e:
print(f"Error extracting features from {image_path}: {e}")
return None
# Initialize text analyzer
class TextDuplicateAnalyzer:
def __init__(self):
self.vectorizer = TfidfVectorizer(max_features=1000, stop_words='english')
def extract_features(self, file_path):
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
text = f.read()
return text
except Exception as e:
print(f"Error reading text file {file_path}: {e}")
return None
# Initialize components
image_extractor = ImageFeatureExtractor()
text_analyzer = TextDuplicateAnalyzer()
def get_file_hash(filepath):
"""Calculate MD5 hash of file"""
hash_md5 = hashlib.md5()
with open(filepath, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def get_file_info(filepath):
"""Get file information"""
stat = os.stat(filepath)
return {
'name': os.path.basename(filepath),
'path': filepath,
'size': stat.st_size,
'modified': datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M:%S'),
'extension': os.path.splitext(filepath)[1].lower()
}
def is_image_file(filename):
"""Check if file is an image"""
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'}
return os.path.splitext(filename)[1].lower() in image_extensions
def is_text_file(filename):
"""Check if file is a text document"""
text_extensions = {'.txt', '.doc', '.docx', '.pdf', '.rtf', '.md'}
return os.path.splitext(filename)[1].lower() in text_extensions
@app.route('/')
def index():
return render_template('index.html')
@app.route('/scan', methods=['POST'])
def scan_duplicates():
"""Scan for duplicates using AI"""
folder_path = request.json.get('folder_path', app.config['UPLOAD_FOLDER'])
print(f"\n{'='*50}")
print(f"🔍 SCANNING FOLDER: {folder_path}")
print(f"{'='*50}")
if not os.path.exists(folder_path):
print(f"❌ ERROR: Folder does not exist: {folder_path}")
return jsonify({'error': 'Folder does not exist'}), 400
# Get all files
files = []
for root, dirs, filenames in os.walk(folder_path):
for filename in filenames:
filepath = os.path.join(root, filename)
files.append(filepath)
if not files:
print("❌ No files found in folder")
return jsonify({'error': 'No files found'}), 400
# Separate files by type
image_files = [f for f in files if is_image_file(f)]
text_files = [f for f in files if is_text_file(f)]
other_files = [f for f in files if not is_image_file(f) and not is_text_file(f)]
results = {
'exact_duplicates': [],
'near_duplicates': [],
'similar_images': [],
'stats': {
'total_files': len(files),
'total_size': sum(os.path.getsize(f) for f in files),
'image_files': len(image_files),
'text_files': len(text_files),
'other_files': len(other_files)
}
}
# Find exact duplicates using hash
hash_map = {}
for filepath in files:
file_hash = get_file_hash(filepath)
if file_hash in hash_map:
results['exact_duplicates'].append({
'original': get_file_info(hash_map[file_hash]),
'duplicate': get_file_info(filepath)
})
else:
hash_map[file_hash] = filepath
# Find near-duplicate images using AI
if image_files:
image_features = []
valid_images = []
for img_path in image_files:
features = image_extractor.extract_features(img_path)
if features is not None:
image_features.append(features)
valid_images.append(img_path)
if len(valid_images) > 1:
similarity_matrix = cosine_similarity(image_features)
for i in range(len(valid_images)):
for j in range(i+1, len(valid_images)):
similarity = similarity_matrix[i][j]
if 0.80 <= similarity < 0.95:
results['similar_images'].append({
'file1': get_file_info(valid_images[i]),
'file2': get_file_info(valid_images[j]),
'similarity': float(similarity)
})
# Calculate potential savings
duplicate_size = sum(d['duplicate']['size'] for d in results['exact_duplicates'])
similar_size = sum(d['file2']['size'] for d in results['similar_images'])
results['stats']['potential_savings'] = duplicate_size + similar_size
results['stats']['savings_percentage'] = round(
(results['stats']['potential_savings'] / results['stats']['total_size']) * 100, 2
) if results['stats']['total_size'] > 0 else 0
return jsonify(results)
# ============== FIXED DELETE FUNCTION WITH RECYCLE BIN ==============
@app.route('/delete', methods=['POST'])
def delete_files():
"""Move selected duplicate files to Recycle Bin"""
files_to_delete = request.json.get('files', [])
deleted = []
errors = []
for filepath in files_to_delete:
try:
# Clean up the path - fix missing backslashes
if ':' in filepath and not filepath.startswith('E:\\'):
# Extract just the filename if path is corrupted
filename = os.path.basename(filepath)
correct_path = os.path.join('E:\\AI_DRIVEN_DUPLICATE_FINDER\\uploads', filename)
if os.path.exists(correct_path):
filepath = correct_path
else:
# Try to find the file in uploads folder
uploads_path = os.path.join('uploads', filename)
if os.path.exists(uploads_path):
filepath = uploads_path
if os.path.exists(filepath):
# Send to Recycle Bin
send2trash.send2trash(filepath)
deleted.append(filepath)
print(f"✅ Moved to Recycle Bin: {filepath}")
else:
errors.append(f"File not found: {filepath}")
except Exception as e:
errors.append(str(e))
print(f"❌ Error: {e}")
return jsonify({
'deleted': deleted,
'errors': errors,
'message': f"Successfully moved {len(deleted)} files to Recycle Bin"
})
# ============== FIXED KEEP BEST FUNCTION WITH RECYCLE BIN ==============
@app.route('/keep-best', methods=['POST'])
def keep_best_files():
"""Keep the best version of duplicate files"""
duplicate_groups = request.json.get('groups', [])
kept = []
deleted = []
errors = []
for group in duplicate_groups:
try:
# Sort files by criteria (keep the largest file)
files = sorted(group['files'],
key=lambda x: (
-os.path.getsize(x['path']) if os.path.exists(x['path']) else 0
))
# Keep the first file, delete the rest
keeper = files[0]
kept.append(keeper['path'])
for duplicate in files[1:]:
try:
dup_path = duplicate['path']
# Fix path if needed
if not os.path.exists(dup_path):
filename = os.path.basename(dup_path)
dup_path = os.path.join('uploads', filename)
if os.path.exists(dup_path):
send2trash.send2trash(dup_path)
deleted.append(dup_path)
print(f"✅ Moved duplicate to Recycle Bin: {dup_path}")
else:
errors.append(f"Duplicate not found: {dup_path}")
except Exception as e:
errors.append(f"Error deleting {duplicate['path']}: {str(e)}")
except Exception as e:
errors.append(f"Error processing group: {str(e)}")
return jsonify({
'kept': kept,
'deleted': deleted,
'errors': errors,
'message': f"Kept {len(kept)} files, moved {len(deleted)} duplicates to Recycle Bin"
})
@app.route('/export-report', methods=['POST'])
def export_report():
"""Export scan results to CSV"""
results = request.json.get('results', {})
report_path = f'reports/duplicate_report_{datetime.now().strftime("%Y%m%d_%H%M%S")}.csv'
os.makedirs('reports', exist_ok=True)
# Prepare data for CSV
rows = []
# Add exact duplicates
for dup in results.get('exact_duplicates', []):
rows.append({
'Type': 'Exact Duplicate',
'Original File': dup['original']['name'],
'Duplicate File': dup['duplicate']['name'],
'Original Path': dup['original']['path'],
'Duplicate Path': dup['duplicate']['path'],
'Size (bytes)': dup['duplicate']['size'],
'Similarity': '100%'
})
# Add near duplicates
for sim in results.get('similar_images', []):
rows.append({
'Type': 'Near Duplicate',
'Original File': sim['file1']['name'],
'Duplicate File': sim['file2']['name'],
'Original Path': sim['file1']['path'],
'Duplicate Path': sim['file2']['path'],
'Size (bytes)': sim['file2']['size'],
'Similarity': f"{sim['similarity']*100:.1f}%"
})
df = pd.DataFrame(rows)
df.to_csv(report_path, index=False)
return send_file(report_path, as_attachment=True, download_name='duplicate_report.csv')
if __name__ == '__main__':
print("="*50)
print("SanDisk AI Smart Duplicate Finder")
print("="*50)
print(f"PyTorch version: {torch.__version__}")
print(f"Device: {'CUDA' if torch.cuda.is_available() else 'CPU'}")
print(f"Upload folder: {app.config['UPLOAD_FOLDER']}")
print("="*50)
print("\n🚀 Server starting...")
print("📱 Open http://localhost:5000 in your browser")
print("="*50)
# Clean up any existing .deleted files on startup
try:
uploads_dir = app.config['UPLOAD_FOLDER']
for file in os.listdir(uploads_dir):
if file.endswith('.deleted'):
file_path = os.path.join(uploads_dir, file)
send2trash.send2trash(file_path)
print(f"🧹 Cleaned up: {file}")
except:
pass
app.run(debug=True, port=5000)