Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,272 +1,123 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
from huggingface_hub import (
|
7 |
-
create_repo,
|
8 |
-
upload_folder,
|
9 |
-
list_repo_files,
|
10 |
-
# delete_file, # Not used
|
11 |
-
Repository,
|
12 |
-
whoami,
|
13 |
)
|
14 |
-
import logging
|
15 |
-
from pathlib import Path
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
# Handle file content collection, especially for code blocks
|
35 |
-
if in_file_content:
|
36 |
-
if line_content_stripped.startswith("```"):
|
37 |
-
if in_code_block: # Closing ```
|
38 |
-
file_content.append(line_content_orig) # Keep the closing backticks as part of content
|
39 |
-
in_code_block = False
|
40 |
-
# Don't immediately save here, let the next ### File or end of input handle it
|
41 |
-
# This allows for text after a code block but before the next file.
|
42 |
-
else: # Opening ```
|
43 |
-
in_code_block = True
|
44 |
-
file_content.append(line_content_orig)
|
45 |
-
elif in_code_block: # Inside a code block
|
46 |
-
file_content.append(line_content_orig)
|
47 |
-
elif not in_code_block: # Plain text line within ### File: block but outside ```
|
48 |
-
# Check if this line is a new file marker, if so, current file ends.
|
49 |
-
if line_content_stripped.startswith("### File:") or line_content_stripped.startswith("## File Structure") or line_content_stripped.startswith("# Space:"):
|
50 |
-
if current_file and file_content:
|
51 |
-
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
52 |
-
current_file = None # Reset
|
53 |
-
file_content = []
|
54 |
-
in_file_content = False # Current file ended
|
55 |
-
# Reprocess this line if it's a new file marker (will be handled by outer ifs)
|
56 |
-
else: # Regular content line
|
57 |
-
file_content.append(line_content_orig)
|
58 |
-
|
59 |
-
|
60 |
-
# Detect major structural elements
|
61 |
-
if line_content_stripped.startswith("# Space:"):
|
62 |
-
if current_file and file_content: # Save previous file if any
|
63 |
-
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
64 |
-
full_space_name_md = line_content_stripped.replace("# Space:", "").strip()
|
65 |
-
if "/" in full_space_name_md:
|
66 |
-
space_info["owner_md"], space_info["repo_name_md"] = full_space_name_md.split("/", 1)
|
67 |
-
else:
|
68 |
-
space_info["repo_name_md"] = full_space_name_md
|
69 |
-
current_file = None
|
70 |
-
file_content = []
|
71 |
-
in_file_content = False
|
72 |
-
in_code_block = False
|
73 |
-
|
74 |
-
elif line_content_stripped.startswith("## File Structure"):
|
75 |
-
if current_file and file_content: # Save previous file if any
|
76 |
-
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
77 |
-
current_file = None
|
78 |
-
file_content = []
|
79 |
-
in_file_content = False
|
80 |
-
in_code_block = False
|
81 |
-
continue # Just a section header
|
82 |
-
|
83 |
-
elif line_content_stripped.startswith("### File:"):
|
84 |
-
if current_file and file_content: # Save content of the previous file
|
85 |
-
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
86 |
-
|
87 |
-
current_file = line_content_stripped.replace("### File:", "").strip()
|
88 |
-
file_content = [] # Reset for new file
|
89 |
-
in_file_content = True # Start collecting content lines for this file
|
90 |
-
in_code_block = False # Reset code block state for new file
|
91 |
-
|
92 |
-
# Note: 📄 and 📁 are ignored if ### File: is the primary mechanism as implemented.
|
93 |
-
# If they are meant to define empty files, that logic would need to be added.
|
94 |
-
# Current parser prioritizes ### File: sections for content.
|
95 |
-
|
96 |
-
# Append the last file's content if any
|
97 |
-
if current_file and file_content:
|
98 |
-
space_info["files"].append({"path": current_file, "content": "\n".join(file_content)})
|
99 |
-
|
100 |
-
space_info["files"] = [f for f in space_info["files"] if f.get("path")] # Filter out empty path entries
|
101 |
-
return space_info
|
102 |
-
|
103 |
-
|
104 |
-
def _determine_repo_id(api_token, space_name_ui, owner_ui):
|
105 |
-
"""
|
106 |
-
Determines the final owner and constructs the repo_id.
|
107 |
-
space_name_ui should be just the name, not 'owner/name'.
|
108 |
-
owner_ui is the value from the UI's owner field.
|
109 |
-
Returns (repo_id, error_message)
|
110 |
-
"""
|
111 |
-
if not space_name_ui:
|
112 |
-
return None, "Error: Space Name cannot be empty."
|
113 |
-
if "/" in space_name_ui: # User should not put slash in space name field
|
114 |
-
return None, "Error: Space Name should not contain '/'. Please use the Owner field for the namespace."
|
115 |
-
|
116 |
-
final_owner = owner_ui
|
117 |
-
error_message = None
|
118 |
-
|
119 |
-
if not final_owner:
|
120 |
-
if not api_token:
|
121 |
-
return None, "Error: API token is required to automatically determine owner when Owner field is empty."
|
122 |
-
try:
|
123 |
-
user_info = whoami(token=api_token)
|
124 |
-
if user_info and 'name' in user_info:
|
125 |
-
final_owner = user_info['name']
|
126 |
-
logger.info(f"Determined owner: {final_owner} from API token.")
|
127 |
-
else:
|
128 |
-
logger.error(f"whoami(token=...) returned: {user_info} - 'name' field missing or user_info is None.")
|
129 |
-
error_message = "Error: Could not retrieve username from API token. Ensure token is valid and has 'Read profile' permissions. Or, specify Owner manually."
|
130 |
-
except Exception as e:
|
131 |
-
logger.error(f"Error calling whoami for owner: {str(e)}")
|
132 |
-
error_message = f"Error retrieving username from API token: {str(e)}. Please specify Owner manually."
|
133 |
-
|
134 |
-
if error_message:
|
135 |
-
return None, error_message
|
136 |
-
|
137 |
-
if not final_owner:
|
138 |
-
return None, "Error: Owner could not be determined. Please provide an owner or ensure your API token is valid."
|
139 |
-
|
140 |
-
return f"{final_owner}/{space_name_ui}", None
|
141 |
-
|
142 |
-
# Function to create and populate a Space
|
143 |
-
def create_space(api_token, space_name_ui, owner_ui, sdk_ui, markdown_input):
|
144 |
-
"""Create a Hugging Face Space and populate it with files from markdown input."""
|
145 |
-
try:
|
146 |
-
if not api_token:
|
147 |
-
return "Error: Please provide a valid Hugging Face API token."
|
148 |
-
|
149 |
-
repo_id, err = _determine_repo_id(api_token, space_name_ui, owner_ui)
|
150 |
-
if err:
|
151 |
-
return err
|
152 |
-
|
153 |
-
space_info = parse_markdown(markdown_input)
|
154 |
-
if not space_info["files"]:
|
155 |
-
return "Error: No files found in the markdown input. Ensure '### File: path/to/file.ext' markers are used correctly with content."
|
156 |
-
|
157 |
-
# Create temporary directory
|
158 |
-
with tempfile.TemporaryDirectory() as temp_dir:
|
159 |
-
repo_local_path = Path(temp_dir) / "repo_content_for_upload"
|
160 |
-
repo_local_path.mkdir(exist_ok=True)
|
161 |
-
|
162 |
-
# Write files to temporary directory
|
163 |
-
for file_info in space_info["files"]:
|
164 |
-
if not file_info.get("path"):
|
165 |
-
logger.warning(f"Skipping file with no path: {file_info}")
|
166 |
-
continue
|
167 |
-
file_path_abs = repo_local_path / file_info["path"]
|
168 |
-
file_path_abs.parent.mkdir(parents=True, exist_ok=True)
|
169 |
-
with open(file_path_abs, "w", encoding="utf-8") as f:
|
170 |
-
f.write(file_info["content"])
|
171 |
-
logger.info(f"Wrote file: {file_path_abs}")
|
172 |
-
|
173 |
-
# Create repository on Hugging Face
|
174 |
-
try:
|
175 |
-
create_repo(
|
176 |
-
repo_id=repo_id,
|
177 |
-
token=api_token,
|
178 |
-
repo_type="space", # Correctly set
|
179 |
-
space_sdk=sdk_ui,
|
180 |
-
private=False,
|
181 |
)
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
#
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from app_logic import (
|
3 |
+
create_space,
|
4 |
+
view_space_files,
|
5 |
+
update_space_file,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
)
|
|
|
|
|
7 |
|
8 |
+
# Gradio interface
|
9 |
+
def main_ui():
|
10 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.blue, secondary_hue=gr.themes.colors.sky), title="Hugging Face Space Builder") as demo:
|
11 |
+
gr.Markdown(
|
12 |
+
"""
|
13 |
+
# 🛠️ Hugging Face Space Builder
|
14 |
+
Create, view, and update Hugging Face Spaces with a custom file structure defined via markdown input.
|
15 |
+
"""
|
16 |
+
)
|
17 |
+
|
18 |
+
with gr.Row():
|
19 |
+
with gr.Column(scale=1):
|
20 |
+
api_token_input = gr.Textbox(
|
21 |
+
label="Hugging Face API Token",
|
22 |
+
type="password",
|
23 |
+
placeholder="Enter your HF token (hf_xxx)",
|
24 |
+
info="Get your token from hf.co/settings/tokens. Needs 'write' access for creating/updating spaces."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
)
|
26 |
+
with gr.Column(scale=2):
|
27 |
+
gr.Markdown(" ") # Spacer or additional info can go here
|
28 |
+
|
29 |
+
with gr.Tabs():
|
30 |
+
with gr.TabItem("🚀 Create New Space"):
|
31 |
+
with gr.Row():
|
32 |
+
space_name_create_input = gr.Textbox(label="Space Name", placeholder="my-awesome-app (no slashes)", scale=2)
|
33 |
+
owner_create_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank for your HF username", scale=1)
|
34 |
+
|
35 |
+
sdk_create_input = gr.Dropdown(
|
36 |
+
label="Space SDK",
|
37 |
+
choices=["gradio", "streamlit", "docker", "static"],
|
38 |
+
value="gradio",
|
39 |
+
info="Select the type of Space (Gradio, Streamlit, etc.)."
|
40 |
+
)
|
41 |
+
markdown_input_create = gr.Textbox(
|
42 |
+
label="Markdown File Structure & Content",
|
43 |
+
placeholder="""Define files using '### File: path/to/file.ext'
|
44 |
+
followed by content, optionally in code blocks.
|
45 |
+
Example:
|
46 |
+
### File: app.py
|
47 |
+
# ```python
|
48 |
+
print("Hello World!")
|
49 |
+
# ```
|
50 |
+
|
51 |
+
### File: README.md
|
52 |
+
# ```markdown
|
53 |
+
# My Awesome App
|
54 |
+
This is a test.
|
55 |
+
# ```
|
56 |
+
""", # Triple backticks within the example are commented out for display
|
57 |
+
lines=15,
|
58 |
+
interactive=True,
|
59 |
+
info="Define files using '### File: path/to/your/file.ext' followed by content, optionally in ```code blocks```."
|
60 |
+
)
|
61 |
+
create_btn = gr.Button("Create Space", variant="primary")
|
62 |
+
create_output_md = gr.Markdown(label="Result")
|
63 |
+
|
64 |
+
with gr.TabItem("📄 View Space Files"):
|
65 |
+
with gr.Row():
|
66 |
+
space_name_view_input = gr.Textbox(label="Space Name", placeholder="my-existing-app (no slashes)", scale=2)
|
67 |
+
owner_view_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank if it's your space", scale=1)
|
68 |
+
view_btn = gr.Button("List Files", variant="primary")
|
69 |
+
view_output_md = gr.Markdown(label="Files in Space")
|
70 |
+
|
71 |
+
with gr.TabItem("✏️ Update Space File"):
|
72 |
+
with gr.Row():
|
73 |
+
space_name_update_input = gr.Textbox(label="Space Name", placeholder="my-target-app (no slashes)", scale=2)
|
74 |
+
owner_update_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank if it's your space", scale=1)
|
75 |
+
|
76 |
+
file_path_update_input = gr.Textbox(
|
77 |
+
label="File Path in Repository",
|
78 |
+
placeholder="e.g., app.py or src/utils.py",
|
79 |
+
info="The full path to the file within the space you want to update/create."
|
80 |
+
)
|
81 |
+
file_content_update_input = gr.Textbox(
|
82 |
+
label="New File Content",
|
83 |
+
placeholder="Enter the complete new content for the file.",
|
84 |
+
lines=10,
|
85 |
+
interactive=True
|
86 |
+
)
|
87 |
+
commit_message_update_input = gr.Textbox(
|
88 |
+
label="Commit Message",
|
89 |
+
placeholder="e.g., Update app.py with new feature",
|
90 |
+
info="Describe the changes you're making."
|
91 |
+
)
|
92 |
+
update_btn = gr.Button("Update File", variant="primary")
|
93 |
+
update_output_md = gr.Markdown(label="Result")
|
94 |
+
|
95 |
+
# Event handlers
|
96 |
+
create_btn.click(
|
97 |
+
fn=create_space,
|
98 |
+
inputs=[api_token_input, space_name_create_input, owner_create_input, sdk_create_input, markdown_input_create],
|
99 |
+
outputs=create_output_md,
|
100 |
+
)
|
101 |
+
view_btn.click(
|
102 |
+
fn=view_space_files,
|
103 |
+
inputs=[api_token_input, space_name_view_input, owner_view_input],
|
104 |
+
outputs=view_output_md,
|
105 |
+
)
|
106 |
+
update_btn.click(
|
107 |
+
fn=update_space_file,
|
108 |
+
inputs=[
|
109 |
+
api_token_input,
|
110 |
+
space_name_update_input,
|
111 |
+
owner_update_input,
|
112 |
+
file_path_update_input,
|
113 |
+
file_content_update_input,
|
114 |
+
commit_message_update_input,
|
115 |
+
],
|
116 |
+
outputs=update_output_md,
|
117 |
+
)
|
118 |
+
|
119 |
+
return demo
|
120 |
+
|
121 |
+
if __name__ == "__main__":
|
122 |
+
demo = main_ui()
|
123 |
+
demo.launch()
|