Guillaume Raille commited on
Commit
e138257
Β·
unverified Β·
0 Parent(s):

Initial commit

Browse files
Files changed (2) hide show
  1. README.md +42 -0
  2. app.py +137 -0
README.md ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: HF Space Creator
3
+ emoji: πŸš€
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.34.0
8
+ app_file: app.py
9
+ pinned: false
10
+ hf_oauth: true
11
+ hf_oauth_scopes:
12
+ - read-repos
13
+ - write-repos
14
+ - manage-repos
15
+ ---
16
+
17
+ # Hugging Face Space Creator
18
+
19
+ This is a simple Gradio app that allows Hugging Face users to login with OAuth and create private spaces.
20
+
21
+ ## Features
22
+
23
+ - OAuth login using Hugging Face account
24
+ - Create private spaces with a single click
25
+ - Automatically sets up a basic Gradio app in the new space
26
+
27
+ ## How to Use
28
+
29
+ 1. Click the "Sign in with Hugging Face" button
30
+ 2. Authorize the application
31
+ 3. Enter a name for your new private space
32
+ 4. Click "Create Private Space"
33
+
34
+ ## Deployment
35
+
36
+ When deploying this Space, make sure:
37
+ 1. The `hf_oauth: true` is set in the README.md metadata
38
+ 2. The necessary OAuth scopes are configured (`read-repos`, `write-repos`, `manage-repos`)
39
+
40
+ ## Local Development
41
+
42
+ OAuth login only works when deployed to Hugging Face Spaces. For local testing, you'll need to deploy the app as a Space.
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import HfApi, create_repo
3
+ from huggingface_hub.utils import RepositoryNotFoundError
4
+ import os
5
+
6
+
7
+ def create_user_space(profile_data: dict, space_name: str) -> str:
8
+ """Create a private Hugging Face space for the user."""
9
+ if not profile_data:
10
+ return "❌ Please login first!"
11
+
12
+ try:
13
+ # Get the OAuth token from the profile data
14
+ token = profile_data.get("access_token")
15
+ if not token:
16
+ return "❌ No access token found. Please login again."
17
+
18
+ api = HfApi(token=token)
19
+
20
+ # Get username from profile data
21
+ username = profile_data.get("preferred_username") or profile_data.get("name")
22
+ if not username:
23
+ return "❌ Could not get username from profile data."
24
+
25
+ # Create a private space repository
26
+ repo_id = f"{username}/{space_name}"
27
+
28
+ # Check if the space already exists
29
+ try:
30
+ api.repo_info(repo_id, repo_type="space")
31
+ return f"❌ Space '{repo_id}' already exists!"
32
+ except RepositoryNotFoundError:
33
+ # Space doesn't exist, we can create it
34
+ pass
35
+
36
+ # Create the space
37
+ url = create_repo(
38
+ repo_id=repo_id,
39
+ repo_type="space",
40
+ space_sdk="gradio",
41
+ private=True,
42
+ token=token
43
+ )
44
+
45
+ # Create a simple app.py file for the new space
46
+ app_content = '''import gradio as gr
47
+
48
+ def greet(name):
49
+ return f"Hello {name}! This is your private space."
50
+
51
+ demo = gr.Interface(fn=greet, inputs="text", outputs="text")
52
+
53
+ if __name__ == "__main__":
54
+ demo.launch()
55
+ '''
56
+
57
+ api.upload_file(
58
+ path_or_fileobj=app_content.encode(),
59
+ path_in_repo="app.py",
60
+ repo_id=repo_id,
61
+ repo_type="space",
62
+ token=token
63
+ )
64
+
65
+ return f"βœ… Successfully created private space: {url}"
66
+
67
+ except Exception as e:
68
+ return f"❌ Error creating space: {str(e)}"
69
+
70
+
71
+ def create_interface():
72
+ """Create the Gradio interface with OAuth login."""
73
+
74
+ with gr.Blocks(title="HF Space Creator") as demo:
75
+ gr.Markdown("# Hugging Face Space Creator")
76
+ gr.Markdown("Login with your Hugging Face account to create a private space.")
77
+
78
+ # OAuth login
79
+ login_btn = gr.LoginButton()
80
+
81
+ # User info display
82
+ user_info = gr.Markdown("")
83
+
84
+ # Space creation section (initially hidden)
85
+ with gr.Column(visible=False) as create_section:
86
+ gr.Markdown("### Create a New Private Space")
87
+ space_name_input = gr.Textbox(
88
+ label="Space Name",
89
+ placeholder="my-awesome-space",
90
+ info="Enter a name for your new private space (lowercase, no spaces)"
91
+ )
92
+ create_btn = gr.Button("Create Private Space", variant="primary")
93
+ create_status = gr.Markdown("")
94
+
95
+ def update_ui(profile: gr.OAuthProfile | None) -> tuple:
96
+ """Update UI based on login status."""
97
+ if profile:
98
+ username = profile.preferred_username or profile.name
99
+ return (
100
+ f"βœ… Logged in as **{username}**",
101
+ gr.update(visible=True)
102
+ )
103
+ else:
104
+ return (
105
+ "",
106
+ gr.update(visible=False)
107
+ )
108
+
109
+ # Update UI when login state changes
110
+ demo.load(update_ui, inputs=None, outputs=[user_info, create_section])
111
+
112
+ # Create space button click
113
+ create_btn.click(
114
+ fn=create_user_space,
115
+ inputs=[login_btn, space_name_input],
116
+ outputs=[create_status]
117
+ )
118
+
119
+ return demo
120
+
121
+
122
+ def main():
123
+ # Check if running in Hugging Face Spaces
124
+ if os.getenv("SPACE_ID"):
125
+ # Running in Spaces, launch with appropriate settings
126
+ demo = create_interface()
127
+ demo.launch()
128
+ else:
129
+ # Running locally, note that OAuth won't work
130
+ print("Note: OAuth login only works when deployed to Hugging Face Spaces.")
131
+ print("To test locally, deploy this as a Space with hf_oauth: true in README.md")
132
+ demo = create_interface()
133
+ demo.launch()
134
+
135
+
136
+ if __name__ == "__main__":
137
+ main()