-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
1471 lines (1241 loc) · 56.1 KB
/
app.py
File metadata and controls
1471 lines (1241 loc) · 56.1 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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
# Enable hf_transfer for maximum bandwidth utilization
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
from flask import Flask, render_template, request, jsonify, send_file, make_response, Response, stream_with_context, redirect, session
from functools import wraps
import re
import threading
import struct
import json
import csv
import time
import uuid
import shutil
import string
import gzip
import random
from io import BytesIO
from PIL import Image
import pandas as pd
import requests
import yaml
import markdown
from huggingface_hub import snapshot_download, hf_hub_download, list_repo_files
from hf_handler import HFHandler
# Import security utilities
try:
from security_utils import token_security
SECURITY_ENABLED = True
except ImportError:
SECURITY_ENABLED = False
print("Warning: security_utils not found, running without advanced security")
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY') or os.urandom(32)
hf_handler = HFHandler()
progress_status = {"status": "Idle", "percentage": 0}
# --- Helper Functions ---
def parse_size(size_str):
units = {"B": 1, "KB": 1024, "MB": 1024**2, "GB": 1024**3}
size_str = size_str.upper().strip()
match = re.match(r"^(\d+(?:\.\d+)?)([KMG]?B?)$", size_str)
if not match: return 1024**3
number, unit = match.groups()
unit = unit if unit else "B"
if len(unit) == 1 and unit != "B": unit += "B"
return int(float(number) * units.get(unit, 1))
def get_paste_filepath(paste_id):
paste_dir = os.path.join(os.getcwd(), 'pastes')
if not os.path.exists(paste_dir):
return None
for filename in os.listdir(paste_dir):
if filename.endswith(f"_{paste_id}.json"):
return os.path.join(paste_dir, filename)
return None
# --- Logic Functions (Threaded) ---
def split_logic(file_path, chunk_size_str, dest_path=None):
global progress_status
try:
if not os.path.exists(file_path):
progress_status = {"status": "Error: File not found", "percentage": 0}
return
chunk_size = parse_size(chunk_size_str)
file_size = os.path.getsize(file_path)
base_name = os.path.basename(file_path)
dir_name = dest_path if dest_path and os.path.exists(dest_path) else os.path.dirname(file_path)
if dest_path and not os.path.exists(dest_path):
os.makedirs(dest_path, exist_ok=True)
name_no_ext, ext = os.path.splitext(base_name)
import math
total_parts = math.ceil(file_size / chunk_size)
with open(file_path, 'rb') as f:
part_num = 1
bytes_processed = 0
while True:
chunk = f.read(chunk_size)
if not chunk: break
part_name = os.path.join(dir_name, f"{name_no_ext}-part{part_num:03d}{ext}")
with open(part_name, 'wb') as chunk_file:
chunk_file.write(chunk)
bytes_processed += len(chunk)
progress_status["percentage"] = int((bytes_processed / file_size) * 100)
progress_status["status"] = f"Splitting: Part {part_num} of {total_parts}..."
part_num += 1
progress_status = {"status": f"Success: Split into {total_parts} parts!", "percentage": 100}
except Exception as e:
progress_status = {"status": f"Error: {str(e)}", "percentage": 0}
def merge_logic(first_part_path):
global progress_status
try:
dir_name = os.path.dirname(first_part_path) or '.'
file_name_with_part = os.path.basename(first_part_path)
match = re.search(r'(.+?)[-.]part\d+(.*)', file_name_with_part)
if not match:
match = re.search(r'(.+?)\.[0-9]{3}$', file_name_with_part)
if not match:
progress_status = {"status": "Error: Invalid part format", "percentage": 0}
return
base_output_name = match.group(1)
else:
base_output_name = match.group(1) + match.group(2)
prefix = match.group(1)
suffix = match.group(2) if len(match.groups()) > 1 else ""
parts = sorted([os.path.join(dir_name, f) for f in os.listdir(dir_name)
if f.startswith(prefix) and (suffix in f) and (("-part" in f) or re.search(r'\.[0-9]{3}$', f))])
if not parts:
progress_status = {"status": "Error: Parts not found", "percentage": 0}
return
total_parts = len(parts)
output_full_path = os.path.join(dir_name, base_output_name)
with open(output_full_path, 'wb') as outfile:
for i, part in enumerate(parts):
progress_status["status"] = f"Merging: Part {i+1}/{total_parts}..."
with open(part, 'rb') as infile:
outfile.write(infile.read())
progress_status["percentage"] = int(((i+1) / total_parts) * 100)
progress_status = {"status": f"Success: Combined into {base_output_name}", "percentage": 100}
except Exception as e:
progress_status = {"status": f"Error: {str(e)}", "percentage": 0}
def convert_to_json_logic(file_path):
global progress_status
try:
if not os.path.exists(file_path):
progress_status = {"status": "Error: File not found", "percentage": 0}
return
file_size = os.path.getsize(file_path)
base_name = os.path.splitext(os.path.basename(file_path))[0]
dir_name = os.path.dirname(file_path)
output_path = os.path.join(dir_name, f"{base_name}_converted.json")
if file_path.endswith('.safetensors'):
progress_status = {"status": "Extracting Safetensors Header...", "percentage": 50}
with open(file_path, 'rb') as f:
header_size_bytes = f.read(8)
header_size = struct.unpack('<Q', header_size_bytes)[0]
header_json_bytes = f.read(header_size)
header_data = json.loads(header_json_bytes.decode('utf-8'))
with open(output_path, 'w', encoding='utf-8') as outfile:
json.dump(header_data, outfile, indent=2)
progress_status = {"status": "Success: Header extracted", "percentage": 100}
elif file_path.endswith('.csv'):
progress_status = {"status": "Converting CSV to JSON...", "percentage": 0}
with open(file_path, 'r', encoding='utf-8') as f_in, open(output_path, 'w', encoding='utf-8') as f_out:
reader = csv.DictReader(f_in)
f_out.write('[')
first = True
bytes_read = 0
for row in reader:
if not first: f_out.write(',\n')
f_out.write(json.dumps(row))
first = False
bytes_read += sum(len(str(v)) for v in row.values())
if bytes_read % 10000 == 0:
perc = min(int((bytes_read / file_size) * 100), 99)
progress_status = {"status": "Converting rows...", "percentage": perc}
f_out.write(']')
progress_status = {"status": "Success: CSV Converted", "percentage": 100}
else:
progress_status = {"status": "Error: Unsupported format", "percentage": 0}
except Exception as e:
progress_status = {"status": f"Error: {str(e)}", "percentage": 0}
# --- Routes ---
@app.route('/')
def index():
return render_template('index.html')
@app.route('/action', methods=['POST'])
def action():
global progress_status
data = request.json
mode = data.get('mode')
path = data.get('path')
dest = data.get('dest_path')
size = data.get('size', '1GB')
progress_status = {"status": "Starting...", "percentage": 0}
if mode == 'split':
threading.Thread(target=split_logic, args=(path, size, dest)).start()
elif mode == 'merge':
threading.Thread(target=merge_logic, args=(path,)).start()
elif mode == 'convert':
threading.Thread(target=convert_to_json_logic, args=(path,)).start()
return jsonify({"message": "Process started"})
@app.route('/progress')
def progress():
return jsonify(progress_status)
@app.route('/browse', methods=['POST'])
def browse_files():
try:
data = request.json
path = data.get('path')
if path == '::DRIVES::':
drives = []
for letter in string.ascii_uppercase:
drive_path = f"{letter}:\\"
if os.path.exists(drive_path):
drives.append(drive_path)
return jsonify({
'current_path': 'My PC',
'parent_path': '',
'dirs': drives,
'files': []
})
if not path:
path = os.path.expanduser("~")
path = os.path.abspath(path)
if not os.path.exists(path) or not os.path.isdir(path):
return jsonify({"error": f"Path not found: {path}"}), 404
try:
items = os.listdir(path)
except PermissionError:
return jsonify({"error": "Access Denied"}), 403
dirs = sorted([d for d in items if os.path.isdir(os.path.join(path, d))])
files = sorted([f for f in items if os.path.isfile(os.path.join(path, f))])
parent_dir = os.path.dirname(path)
if parent_dir == path: # Root
parent_dir = '::DRIVES::'
return jsonify({
'current_path': path,
'parent_path': parent_dir,
'dirs': dirs,
'files': files
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/pastebin')
def pastebin():
return render_template('pastebin.html')
@app.route('/save_paste', methods=['POST'])
def save_paste():
try:
data = request.json
if not data or 'content' not in data: return jsonify({"error": "No content"}), 400
paste_dir = os.path.join(os.getcwd(), 'pastes')
if not os.path.exists(paste_dir): os.makedirs(paste_dir)
paste_id = str(uuid.uuid4())[:8]
timestamp = int(time.time())
filename = f"paste_{timestamp}_{paste_id}.json"
with open(os.path.join(paste_dir, filename), 'w', encoding='utf-8') as f:
json.dump({"id": paste_id, "created_at": timestamp, **data}, f, indent=4)
return jsonify({"message": "Success", "id": paste_id})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/paste/<paste_id>')
def view_paste(paste_id):
filepath = get_paste_filepath(paste_id)
if not filepath: return "Paste not found", 404
with open(filepath, 'r', encoding='utf-8') as f:
return render_template('view_paste.html', paste=json.load(f))
@app.route('/download/<paste_id>')
def download_paste(paste_id):
filepath = get_paste_filepath(paste_id)
if not filepath: return "Paste not found", 404
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
filename = f"{re.sub(r'[^\w\s-]', '', data.get('title','untitled')).strip().replace(' ', '_')}.md"
response = make_response(data.get('content', ''))
response.headers['Content-Disposition'] = f'attachment; filename="{filename}"'
response.headers['Content-Type'] = 'text/plain; charset=utf-8'
return response
@app.route('/hf_downloader')
def hf_downloader_page():
return render_template('hf_downloader.html')
@app.route('/preview_jsonl')
def preview_jsonl_page():
return render_template('preview_jsonl.html')
@app.route('/api/hf_scan', methods=['POST'])
def hf_scan():
data = request.json
repo_id = data.get('repo_id', '').strip()
repo_type = data.get('repo_type', 'model')
token = data.get('token', '').strip() or None
if not repo_id: return jsonify({"error": "Repo ID is required"}), 400
result = hf_handler.scan_repo(repo_id, token, repo_type, options=data.get('options'))
if result.get('success'):
return jsonify(result)
else:
return jsonify(result), 400
@app.route('/api/hf_search', methods=['POST'])
def hf_search():
data = request.json
query = data.get('query', '')
limit = int(data.get('limit', 20))
sort = data.get('sort', 'downloads')
direction = int(data.get('direction', -1))
repo_type = data.get('repo_type', 'model')
result = hf_handler.search_repositories(query, limit, sort, direction, repo_type)
if result.get('success'):
return jsonify(result)
else:
return jsonify(result), 400
@app.route('/api/hf_download_links', methods=['POST'])
def hf_download_links():
data = request.json
repo_id = data.get('repo_id')
files = data.get('files', [])
repo_type = data.get('repo_type', 'model')
if not repo_id or not files:
return jsonify({"error": "Missing parameters"}), 400
links = hf_handler.generate_browser_links(repo_id, files, repo_type)
return jsonify({"links": links})
@app.route('/api/hf_download_server', methods=['POST'])
def hf_download_server():
data = request.json
repo_id = data.get('repo_id')
files = data.get('files', [])
repo_type = data.get('repo_type', 'model')
local_dir = data.get('local_dir')
token = data.get('token')
if not repo_id or not files or not local_dir:
return jsonify({"error": "Missing parameters (Repo ID, Files, or Local Path)"}), 400
return Response(stream_with_context(hf_handler.download_files_to_local(repo_id, files, local_dir, token, repo_type)), mimetype='application/json')
@app.route('/converter')
def converter_page():
return render_template('converter.html')
@app.route('/api/file_info', methods=['POST'])
def get_file_info():
"""Get file information for local disk selection"""
try:
data = request.json
file_path = data.get('path')
if not file_path or not os.path.exists(file_path):
return jsonify({"error": "File not found"}), 404
if not os.path.isfile(file_path):
return jsonify({"error": "Path is not a file"}), 400
# Get basic file stats
stat = os.stat(file_path)
file_size = stat.st_size
file_name = os.path.basename(file_path)
# Get extension
_, ext = os.path.splitext(file_name)
extension = ext.lstrip('.').lower() if ext else ''
# Estimate rows for certain file types
estimated_rows = None
try:
if extension == 'parquet':
# Read parquet metadata without loading full data
import pyarrow.parquet as pq
parquet_file = pq.ParquetFile(file_path)
estimated_rows = parquet_file.metadata.num_rows
elif extension in ['csv']:
# Rough estimate for CSV
with open(file_path, 'rb') as f:
# Read first 1MB to estimate
sample = f.read(1024 * 1024)
avg_line_length = len(sample) / sample.count(b'\n') if sample.count(b'\n') > 0 else 100
estimated_rows = int(file_size / avg_line_length)
elif extension in ['jsonl']:
# Count lines (limit to first 10000 for speed)
with open(file_path, 'rb') as f:
estimated_rows = sum(1 for _ in f)
elif extension in ['json']:
# Try to count array items
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
content = f.read(1024 * 1024) # Read first 1MB
estimated_rows = content.count('{') # Rough estimate
except Exception:
pass
return jsonify({
"name": file_name,
"path": file_path,
"size": file_size,
"extension": extension,
"estimated_rows": _formatNumber(estimated_rows) if estimated_rows else '-',
"modified_time": stat.st_mtime
})
except Exception as e:
return jsonify({"error": str(e)}), 500
def _formatNumber(num):
"""Format number to human readable"""
if num is None:
return '-'
if num >= 1000000:
return f"{num/1000000:.1f}M"
if num >= 1000:
return f"{num/1000:.1f}K"
return str(num)
@app.route('/api/convert', methods=['POST'])
def convert_api():
try:
data = request.json
mode = data.get('mode', 'url')
category = data.get('category')
target_format = data.get('target_format')
content_bytes = None
content_text = None
input_filename = "unknown"
if mode == 'url':
url = data.get('url')
input_filename = url.split('/')[-1]
resp = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, stream=True)
if resp.status_code != 200: return jsonify({"error": "Fetch failed"}), 400
content_bytes = resp.content
try: content_text = resp.text
except: pass
elif mode == 'local':
file_path = data.get('file_path')
if not os.path.exists(file_path): return jsonify({"error": "File not found"}), 404
input_filename = os.path.basename(file_path)
with open(file_path, 'rb') as f: content_bytes = f.read()
try:
with open(file_path, 'r', encoding='utf-8') as f: content_text = f.read()
except: pass
output_io = BytesIO()
filename = "converted"
mimetype = "application/octet-stream"
if category == 'image':
image = Image.open(BytesIO(content_bytes))
if target_format in ['jpeg', 'pdf'] and image.mode in ("RGBA", "P"):
image = image.convert("RGB")
save_fmt = 'JPEG' if target_format.upper() == 'JPG' else target_format.upper()
image.save(output_io, format=save_fmt)
mimetype = f"image/{target_format}" if target_format != 'pdf' else "application/pdf"
filename = f"image.{target_format}"
elif category == 'data':
parsed = None
ext = input_filename.split('.')[-1].lower() if '.' in input_filename else ''
if ext in ['xlsx', 'xls']:
parsed = pd.read_excel(BytesIO(content_bytes)).to_dict(orient='records')
elif ext == 'csv':
parsed = pd.read_csv(BytesIO(content_bytes)).to_dict(orient='records')
elif content_text:
try: parsed = json.loads(content_text)
except:
try: parsed = yaml.safe_load(content_text)
except: pass
if parsed is None: return jsonify({"error": "Parse error"}), 400
if target_format == 'json':
output_io.write(json.dumps(parsed, indent=2).encode('utf-8'))
mimetype = "application/json"
filename = "data.json"
elif target_format == 'yaml':
output_io.write(yaml.dump(parsed).encode('utf-8'))
mimetype = "application/x-yaml"
filename = "data.yaml"
elif target_format == 'excel':
pd.DataFrame(parsed).to_excel(output_io, index=False)
mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
filename = "data.xlsx"
elif target_format == 'csv':
output_io.write(pd.DataFrame(parsed).to_csv(index=False).encode('utf-8'))
mimetype = "text/csv"
filename = "data.csv"
elif category == 'document':
ext = input_filename.split('.')[-1].lower() if '.' in input_filename else ''
if ext in ['xlsx', 'xls', 'csv']:
df = pd.read_csv(BytesIO(content_bytes)) if ext == 'csv' else pd.read_excel(BytesIO(content_bytes))
if target_format == 'markdown':
output_io.write(df.to_markdown(index=False).encode('utf-8'))
filename = "table.md"
elif target_format == 'html':
html = f"<html><link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'><body class='p-4'>{df.to_html(classes='table table-bordered', index=False)}</body></html>"
output_io.write(html.encode('utf-8'))
filename = "table.html"
elif content_text and target_format == 'html':
html = markdown.markdown(content_text, extensions=['tables'])
output_io.write(f"<html><body>{html}</body></html>".encode('utf-8'))
filename = "doc.html"
elif content_text and target_format == 'markdown':
output_io.write(content_text.encode('utf-8'))
filename = "doc.md"
else: return jsonify({"error": "Unsupported doc conversion"}), 400
elif category == 'parquet':
ext = input_filename.split('.')[-1].lower() if '.' in input_filename else ''
if ext != 'parquet':
return jsonify({"error": "Invalid file format. Expected .parquet file"}), 400
try:
# Read parquet file using pandas
df = pd.read_parquet(BytesIO(content_bytes))
if target_format == 'jsonl':
# Convert DataFrame to JSON Lines format
jsonl_lines = []
for _, row in df.iterrows():
# Convert row to dict and handle non-serializable types
row_dict = row.to_dict()
# Convert numpy types to python native types
for key, value in row_dict.items():
if hasattr(value, 'item'): # numpy scalar
row_dict[key] = value.item()
elif isinstance(value, (pd.Timestamp, pd.NaT)):
row_dict[key] = str(value) if pd.notna(value) else None
elif pd.isna(value):
row_dict[key] = None
jsonl_lines.append(json.dumps(row_dict, ensure_ascii=False))
output_io.write('\n'.join(jsonl_lines).encode('utf-8'))
mimetype = "application/jsonlines"
filename = "data.jsonl"
else:
return jsonify({"error": "Unsupported parquet conversion format"}), 400
except Exception as e:
return jsonify({"error": f"Parquet processing failed: {str(e)}"}), 500
output_io.seek(0)
return send_file(output_io, mimetype=mimetype, as_attachment=True, download_name=filename)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/intelligence/<mode>')
def intelligence_page(mode):
if mode not in ['dataset', 'model']:
return redirect('/intelligence/dataset')
return render_template('intelligence.html', active_mode=mode)
@app.route('/api/intel/analyze_dataset', methods=['POST'])
def analyze_dataset():
try:
data = request.json
file_path = data.get('path')
if not file_path or not os.path.exists(file_path):
return jsonify({"error": "File not found"}), 404
file_path_lower = file_path.lower()
is_gz = file_path_lower.endswith('.gz')
df = None
total_rows = 0
# Format detection
if file_path_lower.endswith('.csv'):
df = pd.read_csv(file_path)
total_rows = len(df)
elif file_path_lower.endswith(('.xlsx', '.xls')):
df = pd.read_excel(file_path)
total_rows = len(df)
elif file_path_lower.endswith('.json') and not file_path_lower.endswith('.jsonl'):
df = pd.read_json(file_path)
total_rows = len(df)
elif file_path_lower.endswith('.jsonl') or file_path_lower.endswith('.jsonl.gz'):
import gzip
open_func = gzip.open if is_gz else open
mode = 'rt' if is_gz else 'r'
records = []
with open_func(file_path, mode, encoding='utf-8', errors='replace') as f:
for i, line in enumerate(f):
if i < 1000:
try:
line_data = json.loads(line)
if line_data: records.append(line_data)
except: pass
else: break
if records:
df = pd.DataFrame(records)
# Count total lines efficiently
with open_func(file_path, mode, encoding='utf-8', errors='replace') as f:
total_rows = sum(1 for _ in f)
else:
return jsonify({"error": "Could not parse any valid JSON objects from file"}), 400
if df is None:
return jsonify({"error": "Unsupported or unrecognized dataset format"}), 400
# Safe duplicate detection (JSONL often has dicts/lists which are unhashable)
duplicates = 0
try:
duplicates = int(df.duplicated().sum())
except TypeError:
try:
duplicates = int(df.astype(str).duplicated().sum())
except:
duplicates = "Unsupported (Nested Data)"
analysis = {
"total_rows": total_rows,
"columns": list(df.columns),
"duplicates": duplicates,
"null_values": int(df.isnull().sum().sum()),
"memory_usage": f"{os.path.getsize(file_path) / 1024**2:.2f} MB",
"sample": df.head(5).to_dict(orient='records')
}
return jsonify(analysis)
except Exception as e:
import traceback
print(traceback.format_exc())
return jsonify({"error": f"Analysis failed: {str(e)}"}), 500
@app.route('/api/intel/clean_dataset', methods=['POST'])
def clean_dataset():
try:
data = request.json
file_path = data.get('path')
if not os.path.exists(file_path): return jsonify({"error": "File not found"}), 404
ext = file_path.split('.')[-1].lower()
if ext == 'csv': df = pd.read_csv(file_path)
elif ext == 'json': df = pd.read_json(file_path)
else: return jsonify({"error": "Only CSV/JSON supported for direct cleaning"}), 400
initial_rows = len(df)
df = df.drop_duplicates()
cleaned_rows = len(df)
output_path = file_path.replace(f".{ext}", f"_cleaned.{ext}")
# Ensure target directory exists for the cleaned file
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True)
try:
if ext == 'csv': df.to_csv(output_path, index=False)
else: df.to_json(output_path, orient='records', indent=2)
except Exception as save_err:
return jsonify({"error": f"Failed to write cleaned file to disk: {str(save_err)}"}), 500
return jsonify({
"success": True,
"initial_rows": initial_rows,
"cleaned_rows": cleaned_rows,
"saved_to": output_path
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/compute/vram_calc', methods=['POST'])
def vram_calc():
try:
data = request.json
params = float(data.get('params', 0)) # in Billions
bits = float(data.get('bits', 16))
context = float(data.get('context', 2048))
batch_size = float(data.get('batch_size', 1))
# Weights: (Params * 10^9) * (Bits / 8) / (1024**3) GB
weight_vram = (params * 10**9) * (bits / 8) / (1024**3)
# KV Cache: 2 * layers * hidden_size * context * batch_size * dtype_size
# Simplification: context * params * 0.5 (rough estimate for cache)
kv_cache = (context * params * 10**9 * (bits / 8) * 0.0000001) / (1024**3) # Placeholder for complex calc
# System Overhead: ~10%
total = (weight_vram + kv_cache) * 1.1
return jsonify({
"weight_vram": round(weight_vram, 2),
"kv_cache": round(kv_cache, 2),
"total_estimated": round(total, 2)
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/intelligence/rag')
def rag_page():
return render_template('rag.html')
@app.route('/compute/vram')
def compute_vram_page():
return render_template('compute.html')
@app.route('/data_preparation')
def data_preparation_page():
return render_template('data_preparation.html')
@app.route('/api/intel/rag_chunk', methods=['POST'])
def rag_chunk_logic():
try:
data = request.json
text = data.get('text', '')
chunk_size = int(data.get('chunk_size', 500))
overlap = int(data.get('overlap', 50))
if not text: return jsonify({"error": "No text provided"}), 400
chunks = []
start = 0
while start < len(text):
end = start + chunk_size
chunk = text[start:end]
chunks.append({
"index": len(chunks) + 1,
"content": chunk,
"length": len(chunk),
"tokens_est": int(len(chunk) / 4) # Rough estimation
})
if end >= len(text): break
start += (chunk_size - overlap)
return jsonify({
"success": True,
"total_chunks": len(chunks),
"chunks": chunks
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/intel/preview_jsonl', methods=['POST'])
def preview_jsonl():
try:
data = request.json
file_path = data.get('path')
lines_to_read = int(data.get('limit', 20))
max_line_length = 50000 # Safeguard against massive lines
if not file_path:
return jsonify({"error": "Path is required"}), 400
if not os.path.exists(file_path) or not os.path.isfile(file_path):
return jsonify({"error": "File not found or is not a valid file"}), 404
preview_data = []
is_gz = file_path.lower().endswith('.gz')
try:
import gzip
# Open with gzip if .gz, otherwise normal open
open_func = gzip.open if is_gz else open
mode = 'rt' if is_gz else 'r' # 'rt' for text mode in gzip
with open_func(file_path, mode, encoding='utf-8', errors='replace') as f:
for i, line in enumerate(f):
if i >= lines_to_read:
break
# Safeguard: skip or truncate extremely long lines
if len(line) > max_line_length:
preview_data.append({
"_error": "Line too large to preview",
"length": len(line),
"preview": line[:1000] + "..."
})
continue
clean_line = line.strip()
if not clean_line: continue
try:
preview_data.append(json.loads(clean_line))
except json.JSONDecodeError:
preview_data.append({"_error": "Invalid JSON on this line", "raw": clean_line[:200] + "..."})
except Exception as e:
return jsonify({"error": f"Failed to read file: {str(e)}"}), 500
return jsonify({
"success": True,
"filename": os.path.basename(file_path),
"preview": preview_data,
"total_previewed": len(preview_data),
"file_size": os.path.getsize(file_path),
"is_compressed": is_gz
})
except Exception as e:
return jsonify({"error": str(e)}), 500
# Data Preparation API Endpoints
@app.route('/api/prep/clean', methods=['POST'])
def prep_clean():
"""Data Cleaning: membersihkan data dari noise dan inkonsistensi"""
try:
data = request.json
input_path = data.get('input_path')
output_path = data.get('output_path')
options = data.get('options', {})
if not input_path or not os.path.exists(input_path):
return jsonify({"error": "Input file not found"}), 404
is_gz = input_path.lower().endswith('.gz')
open_func = gzip.open if is_gz else open
mode = 'rt' if is_gz else 'r'
records = []
errors = []
# Read all records
with open_func(input_path, mode, encoding='utf-8', errors='replace') as f:
for i, line in enumerate(f):
try:
line = line.strip()
if not line:
continue
record = json.loads(line)
records.append(record)
except json.JSONDecodeError as e:
errors.append({"line": i + 1, "error": str(e)})
initial_count = len(records)
# Apply cleaning operations
cleaned_records = records.copy()
# 1. Remove duplicates (convert to string for comparison)
if options.get('remove_duplicates', True):
seen = set()
unique_records = []
for record in cleaned_records:
record_str = json.dumps(record, sort_keys=True)
if record_str not in seen:
seen.add(record_str)
unique_records.append(record)
cleaned_records = unique_records
# 2. Handle nulls - filter out records with all null values
if options.get('handle_nulls', True):
def has_valid_data(record):
if not record:
return False
for v in record.values():
if v is not None and v != '' and v != [] and v != {}:
return True
return False
cleaned_records = [r for r in cleaned_records if has_valid_data(r)]
# 3. Normalize text fields
if options.get('normalize_text', True):
for record in cleaned_records:
for key, value in record.items():
if isinstance(value, str):
record[key] = value.strip()
# 4. Remove special characters
if options.get('remove_special_chars', False):
import re
for record in cleaned_records:
for key, value in record.items():
if isinstance(value, str):
record[key] = re.sub(r'[^\w\s-]', '', value)
# 5. Filter by minimum length
if options.get('filter_min_length', False):
min_length = options.get('min_length', 10)
cleaned_records = [
r for r in cleaned_records
if any(len(str(v)) >= min_length for v in r.values() if isinstance(v, str))
]
final_count = len(cleaned_records)
removed_count = initial_count - final_count
reduction_percent = round((removed_count / initial_count * 100), 2) if initial_count > 0 else 0
# Determine output path
if not output_path:
base, ext = os.path.splitext(input_path)
if is_gz:
base = base.replace('.jsonl', '')
output_path = f"{base}_cleaned.jsonl.gz"
else:
output_path = f"{base}_cleaned.jsonl"
# Write cleaned data
os.makedirs(os.path.dirname(os.path.abspath(output_path)) or '.', exist_ok=True)
if output_path.lower().endswith('.gz'):
with gzip.open(output_path, 'wt', encoding='utf-8') as f:
for record in cleaned_records:
f.write(json.dumps(record, ensure_ascii=False) + '\n')
else:
with open(output_path, 'w', encoding='utf-8') as f:
for record in cleaned_records:
f.write(json.dumps(record, ensure_ascii=False) + '\n')
return jsonify({
"success": True,
"initial_count": initial_count,
"final_count": final_count,
"removed_count": removed_count,
"reduction_percent": reduction_percent,
"output_path": output_path,
"parse_errors": len(errors)
})
except Exception as e:
import traceback
print(traceback.format_exc())
return jsonify({"error": str(e)}), 500
@app.route('/api/prep/split', methods=['POST'])
def prep_split():
"""Data Splitting: membagi data menjadi training dan validation set"""
try:
data = request.json
input_path = data.get('input_path')
output_dir = data.get('output_dir')
train_ratio = data.get('train_ratio', 0.8)
random_seed = data.get('random_seed', 42)
shuffle = data.get('shuffle', True)
if not input_path or not os.path.exists(input_path):
return jsonify({"error": "Input file not found"}), 404
is_gz = input_path.lower().endswith('.gz')
open_func = gzip.open if is_gz else open
mode = 'rt' if is_gz else 'r'
# Read all records
records = []
with open_func(input_path, mode, encoding='utf-8', errors='replace') as f:
for line in f:
line = line.strip()
if not line:
continue
try:
records.append(json.loads(line))
except:
pass
total_count = len(records)
if total_count == 0:
return jsonify({"error": "No valid records found in file"}), 400
# Shuffle if requested
if shuffle:
import random
random.seed(random_seed)
random.shuffle(records)
# Calculate split
train_count = int(total_count * train_ratio)
val_count = total_count - train_count
train_records = records[:train_count]
val_records = records[train_count:]
# Determine output directory and filenames