File size: 4,546 Bytes
7bd4e6e
8e0b6ea
32e0f9e
7bd4e6e
 
 
8e0b6ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32e0f9e
 
7bd4e6e
8e0b6ea
 
 
 
 
 
 
7bd4e6e
8e0b6ea
 
 
 
 
 
 
 
 
 
32e0f9e
8e0b6ea
a7cd005
32e0f9e
a2966f7
 
a7cd005
a2966f7
 
 
 
 
 
 
a7cd005
a2966f7
 
 
 
 
a7cd005
a2966f7
 
 
 
a7cd005
a2966f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32e0f9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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}"}