Spaces:
Running
on
Zero
Running
on
Zero
File size: 3,533 Bytes
6ea41ec |
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 |
"""File validation utilities for the UI components."""
import gradio as gr
import logging
from pathlib import Path
from src.core.config import config
from src.core.logging_config import get_logger
logger = get_logger(__name__)
def update_ui_for_file_count(files):
"""Update UI components based on the number of files uploaded."""
if not files or len(files) == 0:
return (
gr.update(visible=False), # processing_type_selector
"<div style='color: #666; font-style: italic;'>Upload documents to begin</div>" # file_status_text
)
if len(files) == 1:
file_name = files[0].name if hasattr(files[0], 'name') else str(files[0])
return (
gr.update(visible=False), # processing_type_selector (hidden for single file)
f"<div style='color: #2563eb; font-weight: 500;'>π Single document: {file_name}</div>"
)
else:
# Calculate total size for validation display
total_size = 0
try:
for file in files:
if hasattr(file, 'size'):
total_size += file.size
elif hasattr(file, 'name'):
# For file paths, get size from filesystem
total_size += Path(file.name).stat().st_size
except:
pass # Size calculation is optional for display
size_display = f" ({total_size / (1024*1024):.1f}MB)" if total_size > 0 else ""
# Check if within limits
if len(files) > 5:
status_color = "#dc2626" # red
status_text = f"β οΈ Too many files: {len(files)}/5 (max 5 files allowed)"
elif total_size > 20 * 1024 * 1024: # 20MB
status_color = "#dc2626" # red
status_text = f"β οΈ Files too large{size_display} (max 20MB combined)"
else:
status_color = "#059669" # green
status_text = f"π Batch mode: {len(files)} files{size_display}"
return (
gr.update(visible=True), # processing_type_selector (visible for multiple files)
f"<div style='color: {status_color}; font-weight: 500;'>{status_text}</div>"
)
def validate_file_for_parser(file_path, parser_name):
"""Validate if the file type is supported by the selected parser."""
if not file_path:
return True, "" # No file selected yet
try:
file_path_obj = Path(file_path)
file_ext = file_path_obj.suffix.lower()
# Check file size
if file_path_obj.exists():
file_size = file_path_obj.stat().st_size
if file_size > config.app.max_file_size:
size_mb = file_size / (1024 * 1024)
max_mb = config.app.max_file_size / (1024 * 1024)
return False, f"File size ({size_mb:.1f}MB) exceeds maximum allowed size ({max_mb:.1f}MB)"
# Check file extension
if file_ext not in config.app.allowed_extensions:
return False, f"File type '{file_ext}' is not supported. Allowed types: {', '.join(config.app.allowed_extensions)}"
# Parser-specific validation
if "GOT-OCR" in parser_name:
if file_ext not in ['.jpg', '.jpeg', '.png']:
return False, "GOT-OCR only supports JPG and PNG formats."
return True, ""
except Exception as e:
logger.error(f"Error validating file: {e}")
return False, f"Error validating file: {e}" |