Spaces:
Running
Running
| """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() | |