File size: 2,861 Bytes
21c88c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
839ccab
21c88c7
 
839ccab
21c88c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f0f104
 
21c88c7
9f0f104
21c88c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Patch ChatInput.tsx: allow uploading PDF, images, Excel, Word, PPT,
OpenDocument and text files in addition to CSV/JSON/JSONL, and surface the
extracted-text fields returned by the backend.
"""

import os

FILE = "/source/frontend/src/components/Chat/ChatInput.tsx"

ACCEPT = (
    ".csv,.json,.jsonl,.pdf,"
    ".png,.jpg,.jpeg,.webp,.bmp,.tiff,.gif,"
    ".xlsx,.xlsm,.xls,"
    ".docx,.doc,"
    ".pptx,"
    ".odt,.ods,.odp,"
    ".txt,.md,.rtf,.log"
)

EXTENSIONS_SET = (
    "['csv', 'json', 'jsonl', 'pdf', "
    "'png', 'jpg', 'jpeg', 'webp', 'bmp', 'tiff', 'tif', 'gif', "
    "'xlsx', 'xlsm', 'xls', 'docx', 'doc', 'pptx', "
    "'odt', 'ods', 'odp', 'txt', 'md', 'rtf', 'log']"
)


def patch():
    if not os.path.exists(FILE):
        print(f"SKIP: {FILE} not found")
        return

    with open(FILE, "r", encoding="utf-8") as f:
        content = f.read()

    # 1. Widen the accept string
    old_accept = "const DATASET_UPLOAD_ACCEPT = '.csv,.json,.jsonl';"
    if old_accept in content:
        content = content.replace(old_accept, f"const DATASET_UPLOAD_ACCEPT = `{ACCEPT}`;")
        print("OK: widened DATASET_UPLOAD_ACCEPT")

    # 2. Widen the allowed extension set
    old_ext = "const DATASET_UPLOAD_EXTENSIONS = new Set(['csv', 'json', 'jsonl']);"
    if old_ext in content:
        content = content.replace(old_ext, f"const DATASET_UPLOAD_EXTENSIONS = new Set({EXTENSIONS_SET});")
        print("OK: widened DATASET_UPLOAD_EXTENSIONS")

    # 3. Update the error message
    old_err = "'Only CSV, JSON, and JSONL dataset files are supported.'"
    new_err = (
        "'Không hỗ trợ định dạng này. Các định dạng được hỗ trợ: CSV, JSON, JSONL, "
        "PDF, ảnh (PNG/JPG/JPEG/WebP/BMP/TIFF/GIF), Excel (XLSX/XLSM/XLS), "
        "Word (DOCX/DOC), PowerPoint (PPTX), OpenDocument (ODT/ODS/ODP), "
        "và văn bản (TXT/MD/RTF/LOG).'"
    )
    if old_err in content:
        content = content.replace(old_err, new_err)
        print("OK: updated unsupported-format error message")

    # 4. Widen the DatasetUploadResponse interface format + add extracted fields
    old_fmt = "  format: 'csv' | 'json' | 'jsonl';"
    if old_fmt in content:
        content = content.replace(old_fmt, "  format: string;")
        print("OK: widened interface format type")

    if "extracted_text?: string" not in content:
        content = content.replace(
            "  load_dataset_snippet: string;\n",
            "  load_dataset_snippet: string;\n"
            "  extracted_text?: string;\n"
            "  extraction_warning?: string;\n",
        )
        print("OK: added extracted_text/extraction_warning to interface")

    with open(FILE, "w", encoding="utf-8") as f:
        f.write(content)
    print("OK: ChatInput.tsx patched for multi-format uploads")


if __name__ == "__main__":
    patch()