File size: 3,983 Bytes
500dbde
c67b1ae
 
 
 
 
 
500dbde
 
f5c9778
 
 
 
 
 
 
 
 
 
c67b1ae
500dbde
 
 
c67b1ae
500dbde
 
 
c67b1ae
 
500dbde
 
 
 
 
 
 
f5c9778
 
c67b1ae
f5c9778
500dbde
 
 
 
 
 
 
 
 
 
 
 
c67b1ae
 
 
 
 
 
 
f5c9778
 
 
 
 
 
 
c67b1ae
f5c9778
 
 
c67b1ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
500dbde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c6f57c2
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
import gradio as gr
from app_logic import (
    build_space_from_image, 
    get_username_from_token,
    preview_image_contents,
    update_file_preview
)

def main_ui():
    with gr.Blocks(theme=gr.themes.Soft(), title="Image-to-Space Builder") as demo:
        gr.Markdown(
            """
            # 🖼️ Image-to-Space Builder
            ## Create a new Hugging Face Space directly from a repo-embedded image.
            
            This tool extracts the file structure and content from an encrypted image (created with a tool like KeyLock)
            and uses it to build and deploy a new Hugging Face Space.
            """
        )
        file_data_state = gr.State([])

        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("### 1. Upload & Preview Image")
                repo_image_input = gr.Image(label="Repo-Embedded Image (PNG)", type="pil")
                image_password_input = gr.Textbox(label="Image Decryption Password", type="password")
                
                preview_button = gr.Button("Preview Contents", variant="secondary")
                
                gr.Markdown("---")
                gr.Markdown("### 2. Configure Your New Space")
                api_token_input = gr.Textbox(
                    label="Hugging Face API Token (hf_xxx)",
                    type="password",
                    placeholder="Enter your write-permission token"
                )
                owner_input = gr.Textbox(
                    label="Owner (Username/Org)", 
                    placeholder="Autofills from token if left blank"
                )
                space_name_input = gr.Textbox(label="New Space Name", placeholder="e.g., my-new-app")
                sdk_input = gr.Dropdown(
                    label="Space SDK",
                    choices=["gradio", "streamlit", "docker", "static"],
                    value="gradio"
                )
                
                gr.Markdown("---")
                gr.Markdown("### 3. Build It!")
                create_button = gr.Button("Create Space from Image", variant="primary")

            with gr.Column(scale=2):
                gr.Markdown("### Status & Result")
                output_status_md = gr.Markdown(label="Status")
                with gr.Column(visible=False) as file_browser_ui:
                    gr.Markdown("### Image Contents Preview")
                    file_selector_dd = gr.Dropdown(label="Select file to preview", interactive=True)
                    file_preview_code = gr.Code(language="python", interactive=False, label="File Content")

        def autofill_owner_if_empty(api_token, current_owner):
            if not current_owner.strip():
                return get_username_from_token(api_token)
            return current_owner

        api_token_input.blur(
            fn=autofill_owner_if_empty,
            inputs=[api_token_input, owner_input],
            outputs=[owner_input]
        )

        preview_button.click(
            fn=preview_image_contents,
            inputs=[repo_image_input, image_password_input],
            outputs=[
                output_status_md, 
                file_browser_ui, 
                file_selector_dd, 
                file_preview_code, 
                file_data_state
            ]
        )
        
        file_selector_dd.change(
            fn=update_file_preview,
            inputs=[file_selector_dd, file_data_state],
            outputs=[file_preview_code]
        )

        create_button.click(
            fn=build_space_from_image,
            inputs=[
                api_token_input,
                repo_image_input,
                image_password_input,
                space_name_input,
                owner_input,
                sdk_input
            ],
            outputs=[output_status_md]
        )
        
    return demo

if __name__ == "__main__":
    demo = main_ui()
    demo.launch(show_error=True, ssr_mode=False)