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()