Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,11 @@ import json
|
|
4 |
from huggingface_hub import HfApi, create_repo, upload_file, login
|
5 |
import random
|
6 |
import string
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Import documentation from assembler_docs.py
|
9 |
from assembler_docs import DOCUMENTATION
|
@@ -18,6 +23,7 @@ if not HF_TOKEN:
|
|
18 |
# Log in to Hugging Face Hub with the token
|
19 |
try:
|
20 |
login(token=HF_TOKEN)
|
|
|
21 |
except Exception as e:
|
22 |
raise ValueError(f"Failed to log in to Hugging Face Hub: {str(e)}")
|
23 |
|
@@ -28,6 +34,17 @@ def generate_space_name():
|
|
28 |
random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))
|
29 |
return f"GeneratedSpace-{random_suffix}"
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
@app.route('/create-space', methods=['POST'])
|
32 |
def create_hf_space():
|
33 |
try:
|
@@ -49,21 +66,27 @@ def create_hf_space():
|
|
49 |
if space_type not in valid_space_types:
|
50 |
return jsonify({"error": f"Invalid space_type. Must be one of {valid_space_types}"}), 400
|
51 |
|
52 |
-
#
|
|
|
53 |
space_name = generate_space_name()
|
54 |
-
full_repo_id = f"
|
|
|
|
|
|
|
|
|
55 |
|
56 |
try:
|
57 |
-
# Attempt to create the repository with explicit
|
58 |
repo_info = create_repo(
|
59 |
repo_id=space_name,
|
|
|
60 |
repo_type="space",
|
61 |
space_sdk=space_type,
|
62 |
token=HF_TOKEN,
|
63 |
private=False,
|
64 |
exist_ok=True # Allow creation even if the repo might exist
|
65 |
)
|
66 |
-
|
67 |
except Exception as e:
|
68 |
return jsonify({"error": f"Failed to create repository {full_repo_id}: {str(e)}"}), 500
|
69 |
|
@@ -72,6 +95,7 @@ def create_hf_space():
|
|
72 |
repo_exists = hf_api.repo_exists(repo_id=full_repo_id, repo_type="space")
|
73 |
if not repo_exists:
|
74 |
return jsonify({"error": f"Repository {full_repo_id} does not exist after creation attempt."}), 500
|
|
|
75 |
except Exception as e:
|
76 |
return jsonify({"error": f"Failed to verify repository {full_repo_id}: {str(e)}"}), 500
|
77 |
|
@@ -93,7 +117,7 @@ def create_hf_space():
|
|
93 |
repo_type="space",
|
94 |
token=HF_TOKEN
|
95 |
)
|
96 |
-
|
97 |
except Exception as e:
|
98 |
os.remove(f"temp_{filename}")
|
99 |
return jsonify({"error": f"Failed to upload file {filename}: {str(e)}"}), 500
|
@@ -118,7 +142,7 @@ def create_hf_space():
|
|
118 |
repo_type="space",
|
119 |
token=HF_TOKEN
|
120 |
)
|
121 |
-
|
122 |
except Exception as e:
|
123 |
os.remove("temp_requirements.txt")
|
124 |
return jsonify({"error": f"Failed to upload requirements.txt: {str(e)}"}), 500
|
@@ -144,13 +168,13 @@ CMD ["python", "app.py"]
|
|
144 |
repo_type="space",
|
145 |
token=HF_TOKEN
|
146 |
)
|
147 |
-
|
148 |
except Exception as e:
|
149 |
os.remove("temp_Dockerfile")
|
150 |
return jsonify({"error": f"Failed to upload Dockerfile: {str(e)}"}), 500
|
151 |
os.remove("temp_Dockerfile")
|
152 |
|
153 |
-
space_url = f"https://huggingface.co/spaces/
|
154 |
return jsonify({
|
155 |
"message": "New Space created",
|
156 |
"url": space_url,
|
|
|
4 |
from huggingface_hub import HfApi, create_repo, upload_file, login
|
5 |
import random
|
6 |
import string
|
7 |
+
import logging
|
8 |
+
|
9 |
+
# Configure logging
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
logger = logging.getLogger(__name__)
|
12 |
|
13 |
# Import documentation from assembler_docs.py
|
14 |
from assembler_docs import DOCUMENTATION
|
|
|
23 |
# Log in to Hugging Face Hub with the token
|
24 |
try:
|
25 |
login(token=HF_TOKEN)
|
26 |
+
logger.info("Successfully logged in to Hugging Face Hub")
|
27 |
except Exception as e:
|
28 |
raise ValueError(f"Failed to log in to Hugging Face Hub: {str(e)}")
|
29 |
|
|
|
34 |
random_suffix = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6))
|
35 |
return f"GeneratedSpace-{random_suffix}"
|
36 |
|
37 |
+
def verify_organization_access(organization: str, token: str) -> bool:
|
38 |
+
"""Verify if the token has write access to the organization."""
|
39 |
+
try:
|
40 |
+
# Try to list repositories or check organization membership
|
41 |
+
repos = hf_api.list_repos(author=organization, token=token)
|
42 |
+
logger.info(f"Verified access to organization {organization}")
|
43 |
+
return True
|
44 |
+
except Exception as e:
|
45 |
+
logger.error(f"No write access to organization {organization}: {str(e)}")
|
46 |
+
return False
|
47 |
+
|
48 |
@app.route('/create-space', methods=['POST'])
|
49 |
def create_hf_space():
|
50 |
try:
|
|
|
66 |
if space_type not in valid_space_types:
|
67 |
return jsonify({"error": f"Invalid space_type. Must be one of {valid_space_types}"}), 400
|
68 |
|
69 |
+
# Organization namespace for new Spaces
|
70 |
+
ORGANIZATION = "Space-Share"
|
71 |
space_name = generate_space_name()
|
72 |
+
full_repo_id = f"{ORGANIZATION}/{space_name}"
|
73 |
+
|
74 |
+
# Verify token has access to Space-Share organization
|
75 |
+
if not verify_organization_access(ORGANIZATION, HF_TOKEN):
|
76 |
+
return jsonify({"error": f"No write access to organization {ORGANIZATION}. Check your HF_TOKEN permissions."}), 403
|
77 |
|
78 |
try:
|
79 |
+
# Attempt to create the repository with explicit organization context
|
80 |
repo_info = create_repo(
|
81 |
repo_id=space_name,
|
82 |
+
organization=ORGANIZATION, # Explicitly specify the organization
|
83 |
repo_type="space",
|
84 |
space_sdk=space_type,
|
85 |
token=HF_TOKEN,
|
86 |
private=False,
|
87 |
exist_ok=True # Allow creation even if the repo might exist
|
88 |
)
|
89 |
+
logger.info(f"Repository created: {full_repo_id}")
|
90 |
except Exception as e:
|
91 |
return jsonify({"error": f"Failed to create repository {full_repo_id}: {str(e)}"}), 500
|
92 |
|
|
|
95 |
repo_exists = hf_api.repo_exists(repo_id=full_repo_id, repo_type="space")
|
96 |
if not repo_exists:
|
97 |
return jsonify({"error": f"Repository {full_repo_id} does not exist after creation attempt."}), 500
|
98 |
+
logger.info(f"Verified repository exists: {full_repo_id}")
|
99 |
except Exception as e:
|
100 |
return jsonify({"error": f"Failed to verify repository {full_repo_id}: {str(e)}"}), 500
|
101 |
|
|
|
117 |
repo_type="space",
|
118 |
token=HF_TOKEN
|
119 |
)
|
120 |
+
logger.info(f"Uploaded file: {filename}")
|
121 |
except Exception as e:
|
122 |
os.remove(f"temp_{filename}")
|
123 |
return jsonify({"error": f"Failed to upload file {filename}: {str(e)}"}), 500
|
|
|
142 |
repo_type="space",
|
143 |
token=HF_TOKEN
|
144 |
)
|
145 |
+
logger.info("Uploaded requirements.txt")
|
146 |
except Exception as e:
|
147 |
os.remove("temp_requirements.txt")
|
148 |
return jsonify({"error": f"Failed to upload requirements.txt: {str(e)}"}), 500
|
|
|
168 |
repo_type="space",
|
169 |
token=HF_TOKEN
|
170 |
)
|
171 |
+
logger.info("Uploaded Dockerfile")
|
172 |
except Exception as e:
|
173 |
os.remove("temp_Dockerfile")
|
174 |
return jsonify({"error": f"Failed to upload Dockerfile: {str(e)}"}), 500
|
175 |
os.remove("temp_Dockerfile")
|
176 |
|
177 |
+
space_url = f"https://huggingface.co/spaces/{full_repo_id}"
|
178 |
return jsonify({
|
179 |
"message": "New Space created",
|
180 |
"url": space_url,
|