broadfield-dev commited on
Commit
60d3ef3
·
verified ·
1 Parent(s): f89e3c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -269
app.py CHANGED
@@ -1,272 +1,123 @@
1
- import os
2
- import re
3
- import tempfile
4
- import shutil
5
- import git
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
- # Configure logging
18
- logging.basicConfig(level=logging.INFO)
19
- logger = logging.getLogger(__name__)
20
-
21
- # Function to parse markdown input
22
- def parse_markdown(markdown_input):
23
- """Parse markdown input to extract space details and file structure."""
24
- space_info = {"repo_name_md": "", "owner_md": "", "files": []}
25
- current_file = None
26
- file_content = []
27
- in_file_content = False # Tracks if we are inside a ### File: block content
28
- in_code_block = False # Tracks if we are inside a ``` code block ```
29
-
30
- lines = markdown_input.strip().split("\n")
31
- for line_idx, line_content_orig in enumerate(lines):
32
- line_content_stripped = line_content_orig.strip()
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
- logger.info(f"Created Space repo: {repo_id}")
183
- except Exception as e:
184
- err_str = str(e).lower()
185
- if "already exists" in err_str or "you already created this repo" in err_str or "exists" in err_str: # More robust check
186
- logger.info(f"Space {repo_id} already exists, proceeding to upload/update files.")
187
- else:
188
- return f"Error creating Space '{repo_id}': {str(e)}"
189
-
190
- # Push to Hugging Face Space
191
- upload_folder(
192
- repo_id=repo_id,
193
- folder_path=str(repo_local_path), # upload_folder expects string path
194
- path_in_repo=".",
195
- token=api_token,
196
- repo_type="space", # ***** ADD THIS LINE *****
197
- commit_message=f"Initial Space setup of {repo_id} via Builder",
198
- # allow_patterns=["*.py", "*.md", "*.txt", "Dockerfile", ".gitattributes", "*.json", "*.yaml", "*.yml"], # Example: be more specific if needed
199
- # ignore_patterns=["*.git/*", ".*", "__pycache__/*"], # Example
200
- )
201
- logger.info(f"Uploaded files to Space: {repo_id}")
202
- return f"Successfully created/updated Space: [{repo_id}](https://huggingface.co/spaces/{repo_id})"
203
-
204
- except Exception as e:
205
- logger.exception(f"Error in create_space for {repo_id if 'repo_id' in locals() else 'unknown repo'}:") # Log full traceback
206
- return f"Error during Space creation/update: {str(e)}"
207
-
208
- # Function to view Space files
209
- def view_space_files(api_token, space_name_ui, owner_ui):
210
- """List files in a Hugging Face Space."""
211
- repo_id_for_error = f"{owner_ui}/{space_name_ui}" if owner_ui else space_name_ui
212
- try:
213
- if not api_token:
214
- return "Error: Please provide a valid Hugging Face API token."
215
-
216
- repo_id, err = _determine_repo_id(api_token, space_name_ui, owner_ui)
217
- if err:
218
- return err
219
-
220
- files = list_repo_files(repo_id=repo_id, token=api_token, repo_type="space") # Correctly set
221
- if files:
222
- return f"Files in `{repo_id}`:\n\n" + "\n".join([f"- `{f}`" for f in files])
223
- else:
224
- return f"No files found in the Space `{repo_id}`."
225
- except Exception as e:
226
- logger.exception(f"Error in view_space_files for {repo_id_for_error}:")
227
- return f"Error listing files for `{repo_id_for_error}`: {str(e)}"
228
-
229
- # Function to update a Space file
230
- def update_space_file(api_token, space_name_ui, owner_ui, file_path_in_repo, file_content, commit_message_ui):
231
- """Update a file in a Hugging Face Space with a commit."""
232
- repo_id_for_error = f"{owner_ui}/{space_name_ui}" if owner_ui else space_name_ui
233
- try:
234
- if not api_token:
235
- return "Error: Please provide a valid Hugging Face API token."
236
-
237
- repo_id, err = _determine_repo_id(api_token, space_name_ui, owner_ui)
238
- if err:
239
- return err
240
-
241
- if not file_path_in_repo:
242
- return "Error: File Path cannot be empty."
243
- if not commit_message_ui:
244
- commit_message_ui = f"Update {file_path_in_repo} via Space Builder"
245
-
246
- with tempfile.TemporaryDirectory() as temp_dir:
247
- repo_local_clone_path = Path(temp_dir) / "cloned_space_repo"
248
-
249
- cloned_repo = Repository(
250
- local_dir=str(repo_local_clone_path), # Repository expects string path
251
- clone_from=f"https://huggingface.co/spaces/{repo_id}", # Ensure this URL is correct
252
- repo_type="space", # Correctly set
253
- use_auth_token=api_token,
254
- git_user="Space Builder Bot",
255
- git_email="[email protected]"
256
- )
257
- logger.info(f"Cloned Space {repo_id} to {repo_local_clone_path}")
258
-
259
- full_local_file_path = cloned_repo.local_dir / file_path_in_repo # Path object arithmetic
260
- full_local_file_path.parent.mkdir(parents=True, exist_ok=True)
261
- with open(full_local_file_path, "w", encoding="utf-8") as f:
262
- f.write(file_content)
263
- logger.info(f"Wrote updated file {file_path_in_repo} locally.")
264
-
265
- cloned_repo.push_to_hub(commit_message=commit_message_ui)
266
- logger.info(f"Pushed changes for {file_path_in_repo} to {repo_id}")
267
-
268
- return f"Successfully updated `{file_path_in_repo}` in Space [{repo_id}](https://huggingface.co/spaces/{repo_id})"
269
-
270
- except Exception as e:
271
- logger.exception(f"Error in update_space_file for {repo_id_for_error}:")
272
- return f"Error updating file for `{repo_id_for_error}`: {str(e)}"
 
 
 
 
 
 
 
 
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()