Spaces:
Runtime error
Runtime error
Update tools/space_builder.py
Browse files- tools/space_builder.py +68 -40
tools/space_builder.py
CHANGED
@@ -1,49 +1,77 @@
|
|
1 |
-
import requests
|
2 |
-
import logging
|
3 |
import os
|
|
|
|
|
4 |
|
5 |
logger = logging.getLogger(__name__)
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
A dictionary containing the result or an error message.
|
18 |
-
"""
|
19 |
-
logger.info(f"SPACE_BUILDER: Calling API with prompt: '{build_prompt[:100]}...'")
|
20 |
-
payload = {"data": [build_prompt]}
|
21 |
-
|
22 |
try:
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
return {"prompt": build_prompt, "error": error_message}
|
41 |
-
except requests.exceptions.Timeout:
|
42 |
-
logger.error("SPACE_BUILDER: API call timed out.")
|
43 |
-
return {"prompt": build_prompt, "error": "The request timed out."}
|
44 |
-
except requests.exceptions.RequestException as e:
|
45 |
-
logger.error(f"SPACE_BUILDER: Request failed: {e}")
|
46 |
-
return {"prompt": build_prompt, "error": f"Request failed: {e}"}
|
47 |
except Exception as e:
|
48 |
-
logger.error(f"SPACE_BUILDER: An unexpected error occurred: {e}", exc_info=True)
|
49 |
-
return {"
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import logging
|
3 |
+
from gradio_client import Client, GradioClientError
|
4 |
|
5 |
logger = logging.getLogger(__name__)
|
6 |
|
7 |
+
client = None
|
8 |
+
|
9 |
+
def _initialize_client():
|
10 |
+
global client
|
11 |
+
if client is None:
|
12 |
+
try:
|
13 |
+
logger.info("Initializing Gradio client for broadfield-dev/build-space...")
|
14 |
+
client = Client("broadfield-dev/build-space")
|
15 |
+
logger.info("Gradio client initialized successfully.")
|
16 |
+
except Exception as e:
|
17 |
+
logger.error(f"Failed to initialize Gradio client: {e}")
|
18 |
+
client = None
|
19 |
+
return client
|
20 |
+
|
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(
|
32 |
+
ui_api_token_from_textbox=hf_token,
|
33 |
+
space_name_ui=space_name,
|
34 |
+
owner_ui=owner,
|
35 |
+
sdk_ui=sdk,
|
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 GradioClientError as e:
|
42 |
+
logger.error(f"SPACE_BUILDER: API Client Error during space creation: {e}")
|
43 |
+
return {"error": f"API Client Error: {e}"}
|
44 |
+
except Exception as e:
|
45 |
+
logger.error(f"SPACE_BUILDER: An unexpected error occurred during space creation: {e}", exc_info=True)
|
46 |
+
return {"error": f"An unexpected error occurred: {e}"}
|
47 |
|
48 |
+
def update_huggingface_space_file(owner: str, space_name: str, file_path: str, new_content: str, commit_message: str):
|
49 |
+
if not _initialize_client():
|
50 |
+
return {"error": "Space builder client could not be initialized."}
|
51 |
|
52 |
+
hf_token = os.getenv("HF_TOKEN")
|
53 |
+
if not hf_token:
|
54 |
+
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
|
55 |
|
56 |
+
logger.info(f"SPACE_BUILDER: Calling /update_space_file for {owner}/{space_name}/{file_path}")
|
|
|
|
|
|
|
|
|
|
|
57 |
try:
|
58 |
+
result = client.predict(
|
59 |
+
ui_api_token_from_textbox=hf_token,
|
60 |
+
space_name_ui=space_name,
|
61 |
+
owner_ui=owner,
|
62 |
+
file_path_in_repo=file_path,
|
63 |
+
file_content=new_content,
|
64 |
+
commit_message_ui=commit_message or f"Update {file_path}",
|
65 |
+
api_name="/update_space_file"
|
66 |
+
)
|
67 |
+
logger.info(f"SPACE_BUILDER: API response for file update: {result}")
|
68 |
+
return {"result": result}
|
69 |
+
except GradioClientError as e:
|
70 |
+
if "is not in the list of choices" in str(e):
|
71 |
+
logger.error(f"SPACE_BUILDER: The file '{file_path}' may not exist or is not selectable in the target space.")
|
72 |
+
return {"error": f"The file '{file_path}' could not be selected for update. It may not exist in the space."}
|
73 |
+
logger.error(f"SPACE_BUILDER: API Client Error during file update: {e}")
|
74 |
+
return {"error": f"API Client Error: {e}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
except Exception as e:
|
76 |
+
logger.error(f"SPACE_BUILDER: An unexpected error occurred during file update: {e}", exc_info=True)
|
77 |
+
return {"error": f"An unexpected error occurred: {e}"}
|