Spaces:
Runtime error
Runtime error
Update tools/space_builder.py
Browse files- tools/space_builder.py +38 -7
tools/space_builder.py
CHANGED
@@ -21,11 +21,9 @@ def _initialize_client():
|
|
21 |
def create_huggingface_space(owner: str, space_name: str, sdk: str, markdown_content: str):
|
22 |
if not _initialize_client():
|
23 |
return {"error": "Space builder client could not be initialized."}
|
24 |
-
|
25 |
hf_token = os.getenv("HF_TOKEN")
|
26 |
if not hf_token:
|
27 |
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
|
28 |
-
|
29 |
logger.info(f"SPACE_BUILDER: Calling /create_space for {owner}/{space_name}")
|
30 |
try:
|
31 |
result = client.predict(
|
@@ -36,7 +34,6 @@ def create_huggingface_space(owner: str, space_name: str, sdk: str, markdown_con
|
|
36 |
markdown_input=markdown_content,
|
37 |
api_name="/create_space"
|
38 |
)
|
39 |
-
logger.info(f"SPACE_BUILDER: API response for creation: {result}")
|
40 |
return {"result": result}
|
41 |
except Exception as e:
|
42 |
logger.error(f"SPACE_BUILDER: An error occurred during space creation: {e}", exc_info=True)
|
@@ -45,11 +42,9 @@ def create_huggingface_space(owner: str, space_name: str, sdk: str, markdown_con
|
|
45 |
def update_huggingface_space_file(owner: str, space_name: str, file_path: str, new_content: str, commit_message: str):
|
46 |
if not _initialize_client():
|
47 |
return {"error": "Space builder client could not be initialized."}
|
48 |
-
|
49 |
hf_token = os.getenv("HF_TOKEN")
|
50 |
if not hf_token:
|
51 |
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
|
52 |
-
|
53 |
logger.info(f"SPACE_BUILDER: Calling /update_space_file for {owner}/{space_name}/{file_path}")
|
54 |
try:
|
55 |
result = client.predict(
|
@@ -61,11 +56,47 @@ def update_huggingface_space_file(owner: str, space_name: str, file_path: str, n
|
|
61 |
commit_message_ui=commit_message or f"Update {file_path}",
|
62 |
api_name="/update_space_file"
|
63 |
)
|
64 |
-
logger.info(f"SPACE_BUILDER: API response for file update: {result}")
|
65 |
return {"result": result}
|
66 |
except Exception as e:
|
67 |
if "is not in the list of choices" in str(e):
|
68 |
-
logger.error(f"SPACE_BUILDER: The file '{file_path}' may not exist or is not selectable in the target space.")
|
69 |
return {"error": f"The file '{file_path}' could not be selected for update. It may not exist in the space."}
|
70 |
logger.error(f"SPACE_BUILDER: An error occurred during file update: {e}", exc_info=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
return {"error": f"An API or client error occurred: {e}"}
|
|
|
21 |
def create_huggingface_space(owner: str, space_name: str, sdk: str, markdown_content: str):
|
22 |
if not _initialize_client():
|
23 |
return {"error": "Space builder client could not be initialized."}
|
|
|
24 |
hf_token = os.getenv("HF_TOKEN")
|
25 |
if not hf_token:
|
26 |
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
|
|
|
27 |
logger.info(f"SPACE_BUILDER: Calling /create_space for {owner}/{space_name}")
|
28 |
try:
|
29 |
result = client.predict(
|
|
|
34 |
markdown_input=markdown_content,
|
35 |
api_name="/create_space"
|
36 |
)
|
|
|
37 |
return {"result": result}
|
38 |
except Exception as e:
|
39 |
logger.error(f"SPACE_BUILDER: An error occurred during space creation: {e}", exc_info=True)
|
|
|
42 |
def update_huggingface_space_file(owner: str, space_name: str, file_path: str, new_content: str, commit_message: str):
|
43 |
if not _initialize_client():
|
44 |
return {"error": "Space builder client could not be initialized."}
|
|
|
45 |
hf_token = os.getenv("HF_TOKEN")
|
46 |
if not hf_token:
|
47 |
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
|
|
|
48 |
logger.info(f"SPACE_BUILDER: Calling /update_space_file for {owner}/{space_name}/{file_path}")
|
49 |
try:
|
50 |
result = client.predict(
|
|
|
56 |
commit_message_ui=commit_message or f"Update {file_path}",
|
57 |
api_name="/update_space_file"
|
58 |
)
|
|
|
59 |
return {"result": result}
|
60 |
except Exception as e:
|
61 |
if "is not in the list of choices" in str(e):
|
|
|
62 |
return {"error": f"The file '{file_path}' could not be selected for update. It may not exist in the space."}
|
63 |
logger.error(f"SPACE_BUILDER: An error occurred during file update: {e}", exc_info=True)
|
64 |
+
return {"error": f"An API or client error occurred: {e}"}
|
65 |
+
|
66 |
+
def list_space_files(owner: str, space_name: str):
|
67 |
+
if not _initialize_client():
|
68 |
+
return {"error": "Space builder client could not be initialized."}
|
69 |
+
hf_token = os.getenv("HF_TOKEN")
|
70 |
+
if not hf_token:
|
71 |
+
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
|
72 |
+
logger.info(f"SPACE_BUILDER: Calling /handle_load_space_files_list for {owner}/{space_name}")
|
73 |
+
try:
|
74 |
+
status_msg, file_list, _ = client.predict(
|
75 |
+
token_from_ui=hf_token,
|
76 |
+
space_name=space_name,
|
77 |
+
owner_name=owner,
|
78 |
+
api_name="/handle_load_space_files_list"
|
79 |
+
)
|
80 |
+
return {"status": status_msg, "files": file_list}
|
81 |
+
except Exception as e:
|
82 |
+
logger.error(f"SPACE_BUILDER: An error occurred while listing files: {e}", exc_info=True)
|
83 |
+
return {"error": f"An API or client error occurred: {e}"}
|
84 |
+
|
85 |
+
def get_space_file_content(owner: str, space_name: str, file_path: str):
|
86 |
+
if not _initialize_client():
|
87 |
+
return {"error": "Space builder client could not be initialized."}
|
88 |
+
hf_token = os.getenv("HF_TOKEN")
|
89 |
+
if not hf_token:
|
90 |
+
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
|
91 |
+
logger.info(f"SPACE_BUILDER: Calling /handle_file_selected_for_editing for {owner}/{space_name}/{file_path}")
|
92 |
+
try:
|
93 |
+
content, status_msg = client.predict(
|
94 |
+
token_from_ui=hf_token,
|
95 |
+
space_name=space_name,
|
96 |
+
owner_name=owner,
|
97 |
+
api_name="/handle_file_selected_for_editing"
|
98 |
+
)
|
99 |
+
return {"status": status_msg, "content": content}
|
100 |
+
except Exception as e:
|
101 |
+
logger.error(f"SPACE_BUILDER: An error occurred while getting file content: {e}", exc_info=True)
|
102 |
return {"error": f"An API or client error occurred: {e}"}
|