update
Browse files915 unmodified lines916917918919920921922923924925926927928929930931932216 unmodified lines11491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183915 unmodified linesprint(f"[Deploy] {error_msg}")return False, error_msg, None# Write transformers.js files to temp directoryfor filename, content in files.items():file_path = temp_path / filenameprint(f"[Deploy] Writing {filename} ({len(content)} chars) to {file_path}")# Use binary mode with UTF-8 encoding for better emoji/special character handlingtry:file_path.write_bytes(content.encode('utf-8'))except UnicodeEncodeError as e:print(f"[Deploy] Encoding error in {filename}: {e}, using fallback encoding")# Fallback: ignore problematic charactersfile_path.write_bytes(content.encode('utf-8', errors='ignore'))# Verify the write was successfulwritten_size = file_path.stat().st_sizeprint(f"[Deploy] Verified {filename}: {written_size} bytes on disk")216 unmodified linesif not file_content:return False, f"Missing content for {file_name}", None# Ensure content is properly encoded (handle emojis safely)try:# Test encoding - this will catch any encoding issues earlyfile_content.encode('utf-8', errors='strict')except UnicodeEncodeError as e:print(f"[Deploy] Encoding warning for {file_name}: {e}")# Replace problematic characters with safe equivalentsfile_content = file_content.encode('utf-8', errors='ignore').decode('utf-8')success = Falselast_error = Nonefor attempt in range(max_attempts):temp_file_path = Nonetry:# Create a NEW temp file for this upload (key difference from old approach)print(f"[Deploy] Creating temp file for {file_name} with {len(file_content)} chars")# Use binary mode with explicit UTF-8 encoding for better emoji handlingwith tempfile.NamedTemporaryFile("wb", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:f.write(file_content.encode('utf-8'))f.flush() # Ensure all content is written to disk before closingtemp_file_path = f.name# File is now closed and flushed, safe to upload# Verify temp file size before uploadimport os as _ostemp_size = _os.path.getsize(temp_file_path)print(f"[Deploy] Temp file {file_name} size on disk: {temp_size} bytes (expected ~{len(file_content)} chars)")# Upload the file without commit_message (HF handles this for spaces)api.upload_file(path_or_fileobj=temp_file_path,915 unmodified lines916917918919920921922923924925926927216 unmodified lines11441145114611471148114911501151115211531154115511561157115811591160116111621163915 unmodified linesprint(f"[Deploy] {error_msg}")return False, error_msg, None# Write transformers.js files to temp directoryfor filename, content in files.items():file_path = temp_path / filenameprint(f"[Deploy] Writing {filename} ({len(content)} chars) to {file_path}")# Use text mode - Python handles encoding automaticallyfile_path.write_text(content, encoding='utf-8')# Verify the write was successfulwritten_size = file_path.stat().st_sizeprint(f"[Deploy] Verified {filename}: {written_size} bytes on disk")216 unmodified linesif not file_content:return False, f"Missing content for {file_name}", Nonesuccess = Falselast_error = Nonefor attempt in range(max_attempts):temp_file_path = Nonetry:# Create a NEW temp file for this upload (matches Gradio version approach)print(f"[Deploy] Creating temp file for {file_name} with {len(file_content)} chars")# Use text mode "w" - lets Python handle encoding automatically (better emoji support)with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:f.write(file_content)temp_file_path = f.name# File is now closed and flushed, safe to upload# Upload the file without commit_message (HF handles this for spaces)api.upload_file(path_or_fileobj=temp_file_path,
backend_deploy.py+7-27