Spaces:
Running
Running
update
Browse files
app.py
CHANGED
|
@@ -3055,7 +3055,7 @@ with gr.Blocks(
|
|
| 3055 |
# Transformers.js logic
|
| 3056 |
elif sdk_name == "Transformers.js":
|
| 3057 |
try:
|
| 3058 |
-
#
|
| 3059 |
if not is_update:
|
| 3060 |
# Use duplicate_space to create a transformers.js template space
|
| 3061 |
from huggingface_hub import duplicate_space
|
|
@@ -3068,84 +3068,80 @@ with gr.Blocks(
|
|
| 3068 |
exist_ok=True
|
| 3069 |
)
|
| 3070 |
print("Duplicated repo result:", duplicated_repo, type(duplicated_repo))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3071 |
# Parse the transformers.js output to get the three files
|
| 3072 |
files = parse_transformers_js_output(code)
|
| 3073 |
|
| 3074 |
if not files['index.html'] or not files['index.js'] or not files['style.css']:
|
| 3075 |
return gr.update(value="Error: Could not parse transformers.js output. Please regenerate the code.", visible=True)
|
| 3076 |
|
| 3077 |
-
# Upload the three files to the
|
| 3078 |
import tempfile
|
|
|
|
| 3079 |
|
| 3080 |
-
#
|
| 3081 |
-
|
| 3082 |
-
|
| 3083 |
-
|
| 3084 |
-
|
| 3085 |
-
|
| 3086 |
-
api.upload_file(
|
| 3087 |
-
path_or_fileobj=temp_path,
|
| 3088 |
-
path_in_repo="index.html",
|
| 3089 |
-
repo_id=repo_id,
|
| 3090 |
-
repo_type="space"
|
| 3091 |
-
)
|
| 3092 |
-
except Exception as e:
|
| 3093 |
-
error_msg = str(e)
|
| 3094 |
-
if "403 Forbidden" in error_msg and "write token" in error_msg:
|
| 3095 |
-
return gr.update(value=f"Error: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)
|
| 3096 |
-
else:
|
| 3097 |
-
return gr.update(value=f"Error uploading index.html: {e}", visible=True)
|
| 3098 |
-
finally:
|
| 3099 |
-
import os
|
| 3100 |
-
os.unlink(temp_path)
|
| 3101 |
-
|
| 3102 |
-
# Upload index.js
|
| 3103 |
-
with tempfile.NamedTemporaryFile("w", suffix=".js", delete=False) as f:
|
| 3104 |
-
f.write(files['index.js'])
|
| 3105 |
-
temp_path = f.name
|
| 3106 |
-
|
| 3107 |
-
try:
|
| 3108 |
-
api.upload_file(
|
| 3109 |
-
path_or_fileobj=temp_path,
|
| 3110 |
-
path_in_repo="index.js",
|
| 3111 |
-
repo_id=repo_id,
|
| 3112 |
-
repo_type="space"
|
| 3113 |
-
)
|
| 3114 |
-
except Exception as e:
|
| 3115 |
-
error_msg = str(e)
|
| 3116 |
-
if "403 Forbidden" in error_msg and "write token" in error_msg:
|
| 3117 |
-
return gr.update(value=f"Error: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)
|
| 3118 |
-
else:
|
| 3119 |
-
return gr.update(value=f"Error uploading index.js: {e}", visible=True)
|
| 3120 |
-
finally:
|
| 3121 |
-
import os
|
| 3122 |
-
os.unlink(temp_path)
|
| 3123 |
-
|
| 3124 |
-
# Upload style.css
|
| 3125 |
-
with tempfile.NamedTemporaryFile("w", suffix=".css", delete=False) as f:
|
| 3126 |
-
f.write(files['style.css'])
|
| 3127 |
-
temp_path = f.name
|
| 3128 |
|
| 3129 |
-
|
| 3130 |
-
|
| 3131 |
-
|
| 3132 |
-
|
| 3133 |
-
|
| 3134 |
-
|
| 3135 |
-
)
|
| 3136 |
-
|
| 3137 |
-
|
| 3138 |
-
|
| 3139 |
-
|
| 3140 |
-
|
| 3141 |
-
|
| 3142 |
-
|
| 3143 |
-
|
| 3144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3145 |
|
| 3146 |
-
# Add anycoder tag to existing README
|
| 3147 |
add_anycoder_tag_to_readme(api, repo_id)
|
| 3148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3149 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
| 3150 |
action_text = "Updated" if is_update else "Deployed"
|
| 3151 |
return gr.update(value=f"β
{action_text}! [Open your Transformers.js Space here]({space_url})", visible=True)
|
|
|
|
| 3055 |
# Transformers.js logic
|
| 3056 |
elif sdk_name == "Transformers.js":
|
| 3057 |
try:
|
| 3058 |
+
# For new spaces, duplicate the template. For updates, just verify access.
|
| 3059 |
if not is_update:
|
| 3060 |
# Use duplicate_space to create a transformers.js template space
|
| 3061 |
from huggingface_hub import duplicate_space
|
|
|
|
| 3068 |
exist_ok=True
|
| 3069 |
)
|
| 3070 |
print("Duplicated repo result:", duplicated_repo, type(duplicated_repo))
|
| 3071 |
+
else:
|
| 3072 |
+
# For updates, verify we can access the existing space
|
| 3073 |
+
try:
|
| 3074 |
+
space_info = api.space_info(repo_id)
|
| 3075 |
+
if not space_info:
|
| 3076 |
+
return gr.update(value=f"Error: Could not access space {repo_id} for update.", visible=True)
|
| 3077 |
+
except Exception as e:
|
| 3078 |
+
return gr.update(value=f"Error: Cannot update space {repo_id}. {str(e)}", visible=True)
|
| 3079 |
# Parse the transformers.js output to get the three files
|
| 3080 |
files = parse_transformers_js_output(code)
|
| 3081 |
|
| 3082 |
if not files['index.html'] or not files['index.js'] or not files['style.css']:
|
| 3083 |
return gr.update(value="Error: Could not parse transformers.js output. Please regenerate the code.", visible=True)
|
| 3084 |
|
| 3085 |
+
# Upload the three files to the space (with retry logic for reliability)
|
| 3086 |
import tempfile
|
| 3087 |
+
import time
|
| 3088 |
|
| 3089 |
+
# Define files to upload
|
| 3090 |
+
files_to_upload = [
|
| 3091 |
+
("index.html", files['index.html']),
|
| 3092 |
+
("index.js", files['index.js']),
|
| 3093 |
+
("style.css", files['style.css'])
|
| 3094 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3095 |
|
| 3096 |
+
# Upload each file with retry logic (similar to static HTML pattern)
|
| 3097 |
+
max_attempts = 3
|
| 3098 |
+
for file_name, file_content in files_to_upload:
|
| 3099 |
+
success = False
|
| 3100 |
+
last_error = None
|
| 3101 |
+
|
| 3102 |
+
for attempt in range(max_attempts):
|
| 3103 |
+
try:
|
| 3104 |
+
with tempfile.NamedTemporaryFile("w", suffix=f".{file_name.split('.')[-1]}", delete=False) as f:
|
| 3105 |
+
f.write(file_content)
|
| 3106 |
+
temp_path = f.name
|
| 3107 |
+
|
| 3108 |
+
api.upload_file(
|
| 3109 |
+
path_or_fileobj=temp_path,
|
| 3110 |
+
path_in_repo=file_name,
|
| 3111 |
+
repo_id=repo_id,
|
| 3112 |
+
repo_type="space"
|
| 3113 |
+
)
|
| 3114 |
+
success = True
|
| 3115 |
+
break
|
| 3116 |
+
|
| 3117 |
+
except Exception as e:
|
| 3118 |
+
last_error = e
|
| 3119 |
+
error_msg = str(e)
|
| 3120 |
+
if "403 Forbidden" in error_msg and "write token" in error_msg:
|
| 3121 |
+
# Permission errors won't be fixed by retrying
|
| 3122 |
+
return gr.update(value=f"Error: Permission denied. Please ensure you have write access to {repo_id} and your token has the correct permissions.", visible=True)
|
| 3123 |
+
|
| 3124 |
+
if attempt < max_attempts - 1: # Not the last attempt
|
| 3125 |
+
time.sleep(2) # Wait before retrying
|
| 3126 |
+
finally:
|
| 3127 |
+
import os
|
| 3128 |
+
if 'temp_path' in locals():
|
| 3129 |
+
os.unlink(temp_path)
|
| 3130 |
+
|
| 3131 |
+
if not success:
|
| 3132 |
+
return gr.update(value=f"Error uploading {file_name}: {last_error}", visible=True)
|
| 3133 |
|
| 3134 |
+
# Add anycoder tag to existing README (for both new and update)
|
| 3135 |
add_anycoder_tag_to_readme(api, repo_id)
|
| 3136 |
|
| 3137 |
+
# For updates, trigger a space restart to ensure changes take effect
|
| 3138 |
+
if is_update:
|
| 3139 |
+
try:
|
| 3140 |
+
api.restart_space(repo_id=repo_id)
|
| 3141 |
+
except Exception as restart_error:
|
| 3142 |
+
# Don't fail the deployment if restart fails, just log it
|
| 3143 |
+
print(f"Note: Could not restart space after update: {restart_error}")
|
| 3144 |
+
|
| 3145 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
| 3146 |
action_text = "Updated" if is_update else "Deployed"
|
| 3147 |
return gr.update(value=f"β
{action_text}! [Open your Transformers.js Space here]({space_url})", visible=True)
|