Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import HfApi, create_repo | |
| from huggingface_hub.utils import RepositoryNotFoundError | |
| import os | |
| def create_interface(): | |
| """Create the Gradio interface with OAuth login.""" | |
| with gr.Blocks(title="HF Space Creator") as demo: | |
| gr.Markdown("# Hugging Face Space Creator") | |
| gr.Markdown("Login with your Hugging Face account to create a private space.") | |
| # OAuth login | |
| login_btn = gr.LoginButton() | |
| # User info display | |
| user_info = gr.Markdown("") | |
| # Space creation section (initially hidden) | |
| with gr.Column(visible=False) as create_section: | |
| gr.Markdown("### Create a New Private Space") | |
| space_name_input = gr.Textbox( | |
| label="Space Name", | |
| placeholder="my-awesome-space", | |
| info="Enter a name for your new private space (lowercase, no spaces)" | |
| ) | |
| create_btn = gr.Button("Create Private Space", variant="primary") | |
| create_status = gr.Markdown("") | |
| def create_user_space(space_name: str, oauth_token: gr.OAuthToken | None) -> str: | |
| """Create a private Hugging Face space for the user.""" | |
| if not oauth_token: | |
| return "β Please login first!" | |
| try: | |
| # Use the OAuth token | |
| token = oauth_token.token | |
| if not token: | |
| return "β No access token found. Please login again." | |
| api = HfApi(token=token) | |
| # Get the current user's username | |
| user_info = api.whoami() | |
| username = user_info["name"] | |
| # Create a private space repository | |
| repo_id = f"{username}/{space_name}" | |
| # Check if the space already exists | |
| try: | |
| api.repo_info(repo_id, repo_type="space") | |
| return f"β Space '{repo_id}' already exists!" | |
| except RepositoryNotFoundError: | |
| # Space doesn't exist, we can create it | |
| pass | |
| # Create the space | |
| url = create_repo( | |
| repo_id=repo_id, | |
| repo_type="space", | |
| space_sdk="gradio", | |
| private=True, | |
| token=token | |
| ) | |
| # Create a simple app.py file for the new space | |
| app_content = '''import gradio as gr | |
| def greet(name): | |
| return f"Hello {name}! This is your private space." | |
| demo = gr.Interface(fn=greet, inputs="text", outputs="text") | |
| if __name__ == "__main__": | |
| demo.launch() | |
| ''' | |
| api.upload_file( | |
| path_or_fileobj=app_content.encode(), | |
| path_in_repo="app.py", | |
| repo_id=repo_id, | |
| repo_type="space", | |
| token=token | |
| ) | |
| return f"β Successfully created private space: {url}" | |
| except Exception as e: | |
| return f"β Error creating space: {str(e)}" | |
| def update_ui(profile: gr.OAuthProfile | None) -> tuple: | |
| """Update UI based on login status.""" | |
| if profile: | |
| # OAuthProfile has username attribute | |
| username = profile.username | |
| return ( | |
| f"β Logged in as **{username}**", | |
| gr.update(visible=True) | |
| ) | |
| else: | |
| return ( | |
| "", | |
| gr.update(visible=False) | |
| ) | |
| # Update UI when login state changes | |
| demo.load(update_ui, inputs=None, outputs=[user_info, create_section]) | |
| # Create space button click - use oauth_token parameter | |
| create_btn.click( | |
| fn=create_user_space, | |
| inputs=[space_name_input], | |
| outputs=[create_status] | |
| ) | |
| return demo | |
| def main(): | |
| # Check if running in Hugging Face Spaces | |
| if os.getenv("SPACE_ID"): | |
| # Running in Spaces, launch with appropriate settings | |
| demo = create_interface() | |
| demo.launch() | |
| else: | |
| # Running locally, note that OAuth won't work | |
| print("Note: OAuth login only works when deployed to Hugging Face Spaces.") | |
| print("To test locally, deploy this as a Space with hf_oauth: true in README.md") | |
| demo = create_interface() | |
| demo.launch() | |
| if __name__ == "__main__": | |
| main() |