File size: 5,240 Bytes
8bd1285
6063cc9
 
 
 
8bd1285
 
 
6063cc9
 
 
 
 
 
 
 
8bd1285
6063cc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8bd1285
 
 
 
6063cc9
 
8bd1285
 
 
6063cc9
 
8bd1285
 
 
 
6063cc9
 
 
 
 
 
8bd1285
6063cc9
8bd1285
 
 
 
 
6063cc9
8bd1285
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import gradio as gr
from app_logic import (
    create_space,
    view_space_files,
    update_space_file,
)

# Gradio interface
def main_ui():
    with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary_hue=gr.themes.colors.sky), title="Hugging Face Space Builder") as demo:
        gr.Markdown(
            """
            # ๐Ÿ› ๏ธ Hugging Face Space Builder
            Create, view, and update Hugging Face Spaces with a custom file structure defined via markdown input.
            """
        )

        with gr.Row():
            with gr.Column(scale=1):
                api_token_input = gr.Textbox(
                    label="Hugging Face API Token",
                    type="password",
                    placeholder="Enter your HF token (hf_xxx)",
                    info="Get your token from hf.co/settings/tokens. Needs 'write' access for creating/updating spaces."
                )
            with gr.Column(scale=2):
                 gr.Markdown(" ") # Spacer or additional info can go here

        with gr.Tabs():
            with gr.TabItem("๐Ÿš€ Create New Space"):
                with gr.Row():
                    space_name_create_input = gr.Textbox(label="Space Name", placeholder="my-awesome-app (no slashes)", scale=2)
                    owner_create_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank for your HF username", scale=1)
                
                sdk_create_input = gr.Dropdown(
                    label="Space SDK", 
                    choices=["gradio", "streamlit", "docker", "static"], 
                    value="gradio",
                    info="Select the type of Space (Gradio, Streamlit, etc.)."
                )
                markdown_input_create = gr.Textbox(
                    label="Markdown File Structure & Content",
                    placeholder="""Define files using '### File: path/to/file.ext'
followed by content, optionally in code blocks.
Example:
### File: app.py
```python
print("Hello World!")
```

### File: README.md
```markdown
# My Awesome App
This is a test.
```
""", # Triple backticks within the example are commented out for display
                    lines=15,
                    interactive=True,
                    info="Define files using '### File: path/to/your/file.ext' followed by content, optionally in ```code blocks```."
                )
                create_btn = gr.Button("Create Space", variant="primary")
                create_output_md = gr.Markdown(label="Result")

            with gr.TabItem("๐Ÿ“„ View Space Files"):
                with gr.Row():
                    space_name_view_input = gr.Textbox(label="Space Name", placeholder="my-existing-app (no slashes)", scale=2)
                    owner_view_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank if it's your space", scale=1)
                view_btn = gr.Button("List Files", variant="primary")
                view_output_md = gr.Markdown(label="Files in Space")

            with gr.TabItem("โœ๏ธ Update Space File"):
                with gr.Row():
                    space_name_update_input = gr.Textbox(label="Space Name", placeholder="my-target-app (no slashes)", scale=2)
                    owner_update_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank if it's your space", scale=1)
                
                file_path_update_input = gr.Textbox(
                    label="File Path in Repository", 
                    placeholder="e.g., app.py or src/utils.py",
                    info="The full path to the file within the space you want to update/create."
                    )
                file_content_update_input = gr.Textbox(
                    label="New File Content", 
                    placeholder="Enter the complete new content for the file.", 
                    lines=10, 
                    interactive=True
                )
                commit_message_update_input = gr.Textbox(
                    label="Commit Message", 
                    placeholder="e.g., Update app.py with new feature",
                    info="Describe the changes you're making."
                )
                update_btn = gr.Button("Update File", variant="primary")
                update_output_md = gr.Markdown(label="Result")

        # Event handlers
        create_btn.click(
            fn=create_space,
            inputs=[api_token_input, space_name_create_input, owner_create_input, sdk_create_input, markdown_input_create],
            outputs=create_output_md,
        )
        view_btn.click(
            fn=view_space_files,
            inputs=[api_token_input, space_name_view_input, owner_view_input],
            outputs=view_output_md,
        )
        update_btn.click(
            fn=update_space_file,
            inputs=[
                api_token_input,
                space_name_update_input,
                owner_update_input,
                file_path_update_input,
                file_content_update_input,
                commit_message_update_input,
            ],
            outputs=update_output_md,
        )

    return demo

if __name__ == "__main__":
    demo = main_ui()
    demo.launch()