update
Browse files8 unmodified lines91011121314151617188 unmodified linespinned: falsedisable_embedding: truehf_oauth: truehf_oauth_scopes: - "openid" # Always included: ID token - "profile" # Always included: user profile info - "write-repos" # Needed to create/upload to Spaces on behalf of the user---
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference21 unmodified lines22232425262728291129 unmodified lines115911601161116011611162116311641165116611621163116411651166116711681169117011711172117339 unmodified lines121312141215121212161217121823 unmodified lines124212431244124512461247124812494 unmodified lines1254125512561252125312571258125923 unmodified lines1283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132912841285128613301331133213331334133513361337133821 unmodified linesimport gradio as grfrom huggingface_hub import InferenceClientfrom tavily import TavilyClientfrom huggingface_hub import HfApiimport tempfile
# Gradio supported languages for syntax highlightingGRADIO_SUPPORTED_LANGUAGES = [1129 unmodified lines last_login_state = gr.State(None)
with gr.Sidebar(): # Remove Hugging Face Login Button and login-required message # login_button = gr.LoginButton( # value="Sign in with Hugging Face", # variant="huggingface", # size="lg" # ) # login_required_msg = gr.Markdown("**Please sign in with Hugging Face to use the app.**", visible=True) login_button = gr.LoginButton() space_name_input = gr.Textbox( label="Space name (e.g. my-cool-space)", placeholder="Enter your Space name", lines=1, visible=True ) 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...",39 unmodified lines label="Model", visible=True # Always visible ) # Remove provider_choices and provider_dropdown, set provider_state to 'auto' only provider_state = gr.State("auto") gr.Markdown("**Quick start**", visible=True) with gr.Column(visible=True) as quick_examples_col:23 unmodified lines outputs=[current_model, image_input] ) # Remove the Advanced accordion and system prompt editing UI # login_button.render() # space_name_input.render()
with gr.Column(): with gr.Tabs():4 unmodified lines interactive=False, label="Generated code" ) # Rename button to 'π Deploy App' deploy_btn = gr.Button("π Deploy App", variant="primary", size="sm", visible=True) with gr.Tab("Preview"): sandbox = gr.HTML(label="Live preview") with gr.Tab("History"):23 unmodified lines
# Deploy to Spaces logic
def 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 in return 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.username repo_id = f"{username}/{space_name.strip()}" api = HfApi(token=token.token) # Create the Space if it doesn't exist try: # Fix create_repo call: use repo_id, not name api.create_repo( repo_id=repo_id, # e.g. username/space_name repo_type="space", space_sdk="static", # or "gradio" if you want a Gradio Space exist_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 file with tempfile.NamedTemporaryFile("w", suffix=".html", delete=False) as f: f.write(code) temp_path = f.name # Upload the file try: api.upload_file( path_or_fileobj=temp_path, path_in_repo="index.html", repo_id=repo_id, repo_type="space" ) space_url = f"https://huggingface.co/spaces/{repo_id}" return gr.update(value=f"β
Deployed! [Open your Space here]({space_url})", visible=True) except Exception as e: return gr.update(value=f"Error uploading file: {e}", visible=True)
# Connect the deploy button to the new function deploy_btn.click( fn=lambda code: deploy_to_spaces_static(code), inputs=code_output, outputs=None 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)README.md+4
app.py+61-13