remove load
Browse files1156 unmodified lines11571158115911601161116211635 unmodified lines116911701171117211731174117511761177117811791180118111821183118411851186112 unmodified lines12991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132814 unmodified lines1343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871156 unmodified linescurrent_model = gr.State(AVAILABLE_MODELS[0]) # Moonshot Kimi-K2open_panel = gr.State(None)last_login_state = gr.State(None)loaded_space_id = gr.State(None)with gr.Sidebar():login_button = gr.LoginButton()5 unmodified lines)deploy_btn = gr.Button("🚀 Deploy App", variant="primary")deploy_status = gr.Markdown(visible=False, label="Deploy status")# --- Load Space UI ---load_space_id = gr.Textbox(label="Load Space (username/space_name)",placeholder="e.g. huggingface-projects/hello-static",lines=1,visible=True)load_space_btn = gr.Button("Load Space", variant="secondary")load_space_status = gr.Markdown(visible=False)# --- End Load Space UI ---input = gr.Textbox(label="What would you like to build?",placeholder="Describe your application...",112 unmodified linesdef deploy_to_user_space(code,space_name,profile, # OAuth profile from LoginButtontoken, # OAuth token from LoginButtonloaded_space_id=None):if not code or not code.strip():return gr.update(value="No code to deploy.", visible=True)if profile is None or token is None:return gr.update(value="Please log in with your Hugging Face account to deploy to your own Space.", visible=True)api = HfApi(token=token)if loaded_space_id:repo_id = loaded_space_idelse:username = profile.usernamerepo_id = f"{username}/{space_name.strip()}"# Create the Space if it doesn't existtry:api.create_repo(repo_id=repo_id,repo_type="space",space_sdk="static",exist_ok=True)except Exception as e:return gr.update(value=f"Error creating Space: {e}", visible=True)# Save code to a temporary filewith tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f:f.write(code)14 unmodified lines# Connect the deploy button to the new functiondeploy_btn.click(deploy_to_user_space,inputs=[code_output, space_name_input, login_button, login_button, loaded_space_id],outputs=deploy_status)# Keep the old deploy method as fallback (if not logged in, user can still use the old method)# Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.# --- Load Space logic ---from huggingface_hub import HfApi, hf_hub_downloaddef load_space_index_html(space_id, history):api = HfApi()try:files = api.list_repo_files(space_id, repo_type="space")if "index.html" not in files:return gr.update(value="No index.html found in this Space.", visible=True), gr.update(value="", language="html"), None, "", gr.update(visible=True), history, None# Download the filelocal_path = hf_hub_download(repo_id=space_id, filename="index.html", repo_type="space")with open(local_path, "r", encoding="utf-8") as f:html_code = f.read()# Set as last assistant message in historynew_history = history.copy() if history else []# Use a synthetic user message for claritynew_history.append([f"Loaded index.html from {space_id}", html_code])return (gr.update(value=f"Loaded index.html from {space_id}", visible=True),gr.update(value=html_code, language="html"),send_to_sandbox(html_code),history_to_chatbot_messages(new_history),gr.update(visible=True),new_history,space_id)except Exception as e:return gr.update(value=f"Error: {e}", visible=True), gr.update(value="", language="html"), None, "", gr.update(visible=True), history, Noneload_space_btn.click(load_space_index_html,inputs=[load_space_id, history],outputs=[load_space_status, code_output, sandbox, history_output, load_space_status, history, loaded_space_id])# --- End Load Space logic ---if __name__ == "__main__":demo.queue(api_open=False, default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=False, show_api=False)1156 unmodified lines1157115811591160116111625 unmodified lines116811691170117111721173112 unmodified lines128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131314 unmodified lines132813291330133113321333133413351336133713381156 unmodified linescurrent_model = gr.State(AVAILABLE_MODELS[0]) # Moonshot Kimi-K2open_panel = gr.State(None)last_login_state = gr.State(None)with gr.Sidebar():login_button = gr.LoginButton()5 unmodified lines)deploy_btn = gr.Button("🚀 Deploy App", variant="primary")deploy_status = gr.Markdown(visible=False, label="Deploy status")input = gr.Textbox(label="What would you like to build?",placeholder="Describe your application...",112 unmodified linesdef deploy_to_user_space(code,space_name,profile: gr.OAuthProfile | None = None,token: gr.OAuthToken | None = None):if not code or not code.strip():return gr.update(value="No code to deploy.", visible=True)if profile is None or token is None:# Fallback to old method if not logged inreturn gr.update(value="Please log in with your Hugging Face account to deploy to your own Space. Otherwise, use the default deploy (opens in new tab).", visible=True)username = profile.usernamerepo_id = f"{username}/{space_name.strip()}"api = HfApi(token=token.token)# Create the Space if it doesn't existtry:# Fix create_repo call: use repo_id, not nameapi.create_repo(repo_id=repo_id, # e.g. username/space_namerepo_type="space",space_sdk="static", # or "gradio" if you want a Gradio Spaceexist_ok=True # Don't error if it already exists)except Exception as e:return gr.update(value=f"Error creating Space: {e}", visible=True)# Save code to a temporary filewith tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f:f.write(code)14 unmodified lines# Connect the deploy button to the new functiondeploy_btn.click(deploy_to_user_space,inputs=[code_output, space_name_input],outputs=deploy_status)# Keep the old deploy method as fallback (if not logged in, user can still use the old method)# Optionally, you can keep the old deploy_btn.click for the default method as a secondary button.if __name__ == "__main__":demo.queue(api_open=False, default_concurrency_limit=20).launch(ssr_mode=True, mcp_server=False, show_api=False)
app.py+19-68