akhaliq HF Staff commited on
Commit
857cdba
·
1 Parent(s): 0055506
Files changed (1) hide show
  1. backend_deploy.py +7 -27
backend_deploy.py CHANGED
@@ -916,17 +916,12 @@ def deploy_to_huggingface_space(
916
  print(f"[Deploy] {error_msg}")
917
  return False, error_msg, None
918
 
919
- # Write transformers.js files to temp directory
920
  for filename, content in files.items():
921
  file_path = temp_path / filename
922
  print(f"[Deploy] Writing {filename} ({len(content)} chars) to {file_path}")
923
- # Use binary mode with UTF-8 encoding for better emoji/special character handling
924
- try:
925
- file_path.write_bytes(content.encode('utf-8'))
926
- except UnicodeEncodeError as e:
927
- print(f"[Deploy] Encoding error in {filename}: {e}, using fallback encoding")
928
- # Fallback: ignore problematic characters
929
- file_path.write_bytes(content.encode('utf-8', errors='ignore'))
930
  # Verify the write was successful
931
  written_size = file_path.stat().st_size
932
  print(f"[Deploy] Verified {filename}: {written_size} bytes on disk")
@@ -1149,35 +1144,20 @@ def deploy_to_huggingface_space(
1149
  if not file_content:
1150
  return False, f"Missing content for {file_name}", None
1151
 
1152
- # Ensure content is properly encoded (handle emojis safely)
1153
- try:
1154
- # Test encoding - this will catch any encoding issues early
1155
- file_content.encode('utf-8', errors='strict')
1156
- except UnicodeEncodeError as e:
1157
- print(f"[Deploy] Encoding warning for {file_name}: {e}")
1158
- # Replace problematic characters with safe equivalents
1159
- file_content = file_content.encode('utf-8', errors='ignore').decode('utf-8')
1160
-
1161
  success = False
1162
  last_error = None
1163
 
1164
  for attempt in range(max_attempts):
1165
  temp_file_path = None
1166
  try:
1167
- # Create a NEW temp file for this upload (key difference from old approach)
1168
  print(f"[Deploy] Creating temp file for {file_name} with {len(file_content)} chars")
1169
- # Use binary mode with explicit UTF-8 encoding for better emoji handling
1170
- with tempfile.NamedTemporaryFile("wb", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
1171
- f.write(file_content.encode('utf-8'))
1172
- f.flush() # Ensure all content is written to disk before closing
1173
  temp_file_path = f.name
1174
  # File is now closed and flushed, safe to upload
1175
 
1176
- # Verify temp file size before upload
1177
- import os as _os
1178
- temp_size = _os.path.getsize(temp_file_path)
1179
- print(f"[Deploy] Temp file {file_name} size on disk: {temp_size} bytes (expected ~{len(file_content)} chars)")
1180
-
1181
  # Upload the file without commit_message (HF handles this for spaces)
1182
  api.upload_file(
1183
  path_or_fileobj=temp_file_path,
 
916
  print(f"[Deploy] {error_msg}")
917
  return False, error_msg, None
918
 
919
+ # Write transformers.js files to temp directory
920
  for filename, content in files.items():
921
  file_path = temp_path / filename
922
  print(f"[Deploy] Writing {filename} ({len(content)} chars) to {file_path}")
923
+ # Use text mode - Python handles encoding automatically
924
+ file_path.write_text(content, encoding='utf-8')
 
 
 
 
 
925
  # Verify the write was successful
926
  written_size = file_path.stat().st_size
927
  print(f"[Deploy] Verified {filename}: {written_size} bytes on disk")
 
1144
  if not file_content:
1145
  return False, f"Missing content for {file_name}", None
1146
 
 
 
 
 
 
 
 
 
 
1147
  success = False
1148
  last_error = None
1149
 
1150
  for attempt in range(max_attempts):
1151
  temp_file_path = None
1152
  try:
1153
+ # Create a NEW temp file for this upload (matches Gradio version approach)
1154
  print(f"[Deploy] Creating temp file for {file_name} with {len(file_content)} chars")
1155
+ # Use text mode "w" - lets Python handle encoding automatically (better emoji support)
1156
+ with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
1157
+ f.write(file_content)
 
1158
  temp_file_path = f.name
1159
  # File is now closed and flushed, safe to upload
1160
 
 
 
 
 
 
1161
  # Upload the file without commit_message (HF handles this for spaces)
1162
  api.upload_file(
1163
  path_or_fileobj=temp_file_path,