Spaces:
Runtime error
Runtime error
import os | |
import logging | |
from gradio_client import Client, GradioClientError | |
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" | |
) | |
logger.info(f"SPACE_BUILDER: API response for creation: {result}") | |
return {"result": result} | |
except GradioClientError as e: | |
logger.error(f"SPACE_BUILDER: API Client Error during space creation: {e}") | |
return {"error": f"API Client Error: {e}"} | |
except Exception as e: | |
logger.error(f"SPACE_BUILDER: An unexpected error occurred during space creation: {e}", exc_info=True) | |
return {"error": f"An unexpected 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" | |
) | |
logger.info(f"SPACE_BUILDER: API response for file update: {result}") | |
return {"result": result} | |
except GradioClientError as e: | |
if "is not in the list of choices" in str(e): | |
logger.error(f"SPACE_BUILDER: The file '{file_path}' may not exist or is not selectable in the target space.") | |
return {"error": f"The file '{file_path}' could not be selected for update. It may not exist in the space."} | |
logger.error(f"SPACE_BUILDER: API Client Error during file update: {e}") | |
return {"error": f"API Client Error: {e}"} | |
except Exception as e: | |
logger.error(f"SPACE_BUILDER: An unexpected error occurred during file update: {e}", exc_info=True) | |
return {"error": f"An unexpected error occurred: {e}"} |