Node-Brain-BU3 / tools /space_builder.py
broadfield-dev's picture
Update tools/space_builder.py
a7cd005 verified
import os
import logging
from gradio_client import Client
logger = logging.getLogger(__name__)
client = None
def _initialize_client():
global client
if client is None:
try:
logger.info("Initializing Gradio client for broadfield-dev/build-space...")
client = Client("broadfield-dev/build-space")
logger.info("Gradio client initialized successfully.")
except Exception as e:
logger.error(f"Failed to initialize Gradio client: {e}")
client = None
return client
def create_huggingface_space(owner: str, space_name: str, sdk: str, markdown_content: str):
if not _initialize_client():
return {"error": "Space builder client could not be initialized."}
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
logger.info(f"SPACE_BUILDER: Calling /create_space for {owner}/{space_name}")
try:
result = client.predict(
ui_api_token_from_textbox=hf_token,
space_name_ui=space_name,
owner_ui=owner,
sdk_ui=sdk,
markdown_input=markdown_content,
api_name="/create_space"
)
return {"result": result}
except Exception as e:
logger.error(f"SPACE_BUILDER: An error occurred during space creation: {e}", exc_info=True)
return {"error": f"An API or client error occurred: {e}"}
def update_huggingface_space_file(owner: str, space_name: str, file_path: str, new_content: str, commit_message: str):
if not _initialize_client():
return {"error": "Space builder client could not be initialized."}
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
logger.info(f"SPACE_BUILDER: Calling /update_space_file for {owner}/{space_name}/{file_path}")
try:
result = client.predict(
ui_api_token_from_textbox=hf_token,
space_name_ui=space_name,
owner_ui=owner,
file_path_in_repo=file_path,
file_content=new_content,
commit_message_ui=commit_message or f"Update {file_path}",
api_name="/update_space_file"
)
return {"result": result}
except Exception as e:
if "is not in the list of choices" in str(e):
return {"error": f"The file '{file_path}' could not be selected for update. It may not exist."}
logger.error(f"SPACE_BUILDER: An error occurred during file update: {e}", exc_info=True)
return {"error": f"An API or client error occurred: {e}"}
def list_huggingface_space_files(owner: str, space_name: str):
if not _initialize_client():
return {"error": "Space builder client could not be initialized."}
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
logger.info(f"SPACE_BUILDER: Calling /handle_load_space_files_list for {owner}/{space_name}")
try:
status_msg, files_list, _ = client.predict(
token_from_ui=hf_token,
space_name=space_name,
owner_name=owner,
api_name="/handle_load_space_files_list"
)
return {"status": status_msg, "files": files_list}
except Exception as e:
logger.error(f"SPACE_BUILDER: An error occurred while listing files: {e}", exc_info=True)
return {"error": f"An API or client error occurred: {e}"}
def get_huggingface_space_file_content(owner: str, space_name: str, file_path: str):
if not _initialize_client():
return {"error": "Space builder client could not be initialized."}
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
return {"error": "Hugging Face token (HF_TOKEN) is not set in environment variables."}
logger.info(f"SPACE_BUILDER: Calling /handle_file_selected_for_editing for {owner}/{space_name}/{file_path}")
try:
content, status_msg = client.predict(
token_from_ui=hf_token,
space_name=space_name,
owner_name=owner,
api_name="/handle_file_selected_for_editing"
)
return {"status": status_msg, "content": content}
except Exception as e:
logger.error(f"SPACE_BUILDER: An error occurred while getting file content: {e}", exc_info=True)
return {"error": f"An API or client error occurred: {e}"}