Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from app_logic import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def main_ui():
|
| 5 |
with gr.Blocks(theme=gr.themes.Soft(), title="Image-to-Space Builder") as demo:
|
|
@@ -12,13 +17,16 @@ def main_ui():
|
|
| 12 |
and uses it to build and deploy a new Hugging Face Space.
|
| 13 |
"""
|
| 14 |
)
|
|
|
|
| 15 |
|
| 16 |
with gr.Row():
|
| 17 |
with gr.Column(scale=1):
|
| 18 |
-
gr.Markdown("### 1. Upload
|
| 19 |
repo_image_input = gr.Image(label="Repo-Embedded Image (PNG)", type="pil")
|
| 20 |
image_password_input = gr.Textbox(label="Image Decryption Password", type="password")
|
| 21 |
|
|
|
|
|
|
|
| 22 |
gr.Markdown("---")
|
| 23 |
gr.Markdown("### 2. Configure Your New Space")
|
| 24 |
api_token_input = gr.Textbox(
|
|
@@ -28,7 +36,7 @@ def main_ui():
|
|
| 28 |
)
|
| 29 |
owner_input = gr.Textbox(
|
| 30 |
label="Owner (Username/Org)",
|
| 31 |
-
placeholder="Autofills from token if left blank"
|
| 32 |
)
|
| 33 |
space_name_input = gr.Textbox(label="New Space Name", placeholder="e.g., my-new-app")
|
| 34 |
sdk_input = gr.Dropdown(
|
|
@@ -42,24 +50,42 @@ def main_ui():
|
|
| 42 |
create_button = gr.Button("Create Space from Image", variant="primary")
|
| 43 |
|
| 44 |
with gr.Column(scale=2):
|
| 45 |
-
gr.Markdown("###
|
| 46 |
-
output_status_md = gr.Markdown(label="
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def autofill_owner_if_empty(api_token, current_owner):
|
| 49 |
-
"""
|
| 50 |
-
Checks if the owner field is empty. If so, it tries to fetch the username.
|
| 51 |
-
This prevents overwriting a manually entered organization or username.
|
| 52 |
-
"""
|
| 53 |
if not current_owner.strip():
|
| 54 |
return get_username_from_token(api_token)
|
| 55 |
return current_owner
|
| 56 |
|
| 57 |
api_token_input.blur(
|
| 58 |
fn=autofill_owner_if_empty,
|
| 59 |
-
inputs=[api_token_input, owner_input],
|
| 60 |
outputs=[owner_input]
|
| 61 |
)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
create_button.click(
|
| 64 |
fn=build_space_from_image,
|
| 65 |
inputs=[
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from app_logic import (
|
| 3 |
+
build_space_from_image,
|
| 4 |
+
get_username_from_token,
|
| 5 |
+
preview_image_contents,
|
| 6 |
+
update_file_preview
|
| 7 |
+
)
|
| 8 |
|
| 9 |
def main_ui():
|
| 10 |
with gr.Blocks(theme=gr.themes.Soft(), title="Image-to-Space Builder") as demo:
|
|
|
|
| 17 |
and uses it to build and deploy a new Hugging Face Space.
|
| 18 |
"""
|
| 19 |
)
|
| 20 |
+
file_data_state = gr.State([])
|
| 21 |
|
| 22 |
with gr.Row():
|
| 23 |
with gr.Column(scale=1):
|
| 24 |
+
gr.Markdown("### 1. Upload & Preview Image")
|
| 25 |
repo_image_input = gr.Image(label="Repo-Embedded Image (PNG)", type="pil")
|
| 26 |
image_password_input = gr.Textbox(label="Image Decryption Password", type="password")
|
| 27 |
|
| 28 |
+
preview_button = gr.Button("Preview Contents", variant="secondary")
|
| 29 |
+
|
| 30 |
gr.Markdown("---")
|
| 31 |
gr.Markdown("### 2. Configure Your New Space")
|
| 32 |
api_token_input = gr.Textbox(
|
|
|
|
| 36 |
)
|
| 37 |
owner_input = gr.Textbox(
|
| 38 |
label="Owner (Username/Org)",
|
| 39 |
+
placeholder="Autofills from token if left blank"
|
| 40 |
)
|
| 41 |
space_name_input = gr.Textbox(label="New Space Name", placeholder="e.g., my-new-app")
|
| 42 |
sdk_input = gr.Dropdown(
|
|
|
|
| 50 |
create_button = gr.Button("Create Space from Image", variant="primary")
|
| 51 |
|
| 52 |
with gr.Column(scale=2):
|
| 53 |
+
gr.Markdown("### Status & Result")
|
| 54 |
+
output_status_md = gr.Markdown(label="Status")
|
| 55 |
+
with gr.Column(visible=False) as file_browser_ui:
|
| 56 |
+
gr.Markdown("### Image Contents Preview")
|
| 57 |
+
file_selector_dd = gr.Dropdown(label="Select file to preview", interactive=True)
|
| 58 |
+
file_preview_code = gr.Code(language="python", interactive=False, label="File Content")
|
| 59 |
+
|
| 60 |
def autofill_owner_if_empty(api_token, current_owner):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if not current_owner.strip():
|
| 62 |
return get_username_from_token(api_token)
|
| 63 |
return current_owner
|
| 64 |
|
| 65 |
api_token_input.blur(
|
| 66 |
fn=autofill_owner_if_empty,
|
| 67 |
+
inputs=[api_token_input, owner_input],
|
| 68 |
outputs=[owner_input]
|
| 69 |
)
|
| 70 |
|
| 71 |
+
preview_button.click(
|
| 72 |
+
fn=preview_image_contents,
|
| 73 |
+
inputs=[repo_image_input, image_password_input],
|
| 74 |
+
outputs=[
|
| 75 |
+
output_status_md,
|
| 76 |
+
file_browser_ui,
|
| 77 |
+
file_selector_dd,
|
| 78 |
+
file_preview_code,
|
| 79 |
+
file_data_state
|
| 80 |
+
]
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
file_selector_dd.change(
|
| 84 |
+
fn=update_file_preview,
|
| 85 |
+
inputs=[file_selector_dd, file_data_state],
|
| 86 |
+
outputs=[file_preview_code]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
create_button.click(
|
| 90 |
fn=build_space_from_image,
|
| 91 |
inputs=[
|