Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +78 -3
- app_logic.py +2 -2
app.py
CHANGED
|
@@ -16,9 +16,81 @@ from app_logic import (
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# --- FastAPI App Initialization ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
app = FastAPI(
|
| 20 |
title="Hugging Face Space Builder API",
|
| 21 |
-
description=
|
| 22 |
version="1.0.0",
|
| 23 |
)
|
| 24 |
|
|
@@ -29,6 +101,7 @@ class SpaceCreate(BaseModel):
|
|
| 29 |
owner: Optional[str] = None
|
| 30 |
sdk: str = "gradio"
|
| 31 |
markdown_content: str
|
|
|
|
| 32 |
|
| 33 |
class SpaceUpdate(BaseModel):
|
| 34 |
api_token: str
|
|
@@ -51,7 +124,8 @@ def api_create_space(space_data: SpaceCreate):
|
|
| 51 |
space_data.space_name,
|
| 52 |
space_data.owner,
|
| 53 |
space_data.sdk,
|
| 54 |
-
space_data.markdown_content
|
|
|
|
| 55 |
)
|
| 56 |
if "Error" in result:
|
| 57 |
raise HTTPException(status_code=400, detail=result)
|
|
@@ -192,11 +266,12 @@ Generate program files for a project as a single plain text string, strictly adh
|
|
| 192 |
space_name_create_input = gr.Textbox(label="Space Name", placeholder="my-awesome-app (no slashes)", scale=2)
|
| 193 |
owner_create_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank for your HF username", scale=1)
|
| 194 |
sdk_create_input = gr.Dropdown(label="Space SDK", choices=["gradio", "streamlit", "docker", "static"], value="gradio")
|
|
|
|
| 195 |
gr.Markdown("### Example Source: [/spaces/broadfield-dev/repo_to_md](https://huggingface.co/spaces/broadfield-dev/repo_to_md)")
|
| 196 |
markdown_input_create = gr.Textbox(label="Markdown File Structure & Content", placeholder="Example:\n### File: app.py\n# ```python\nprint(\"Hello\")\n# ```", lines=15, interactive=True)
|
| 197 |
create_btn = gr.Button("Create Space", variant="primary")
|
| 198 |
create_output_md = gr.Markdown(label="Result")
|
| 199 |
-
create_btn.click(create_space, [api_token_ui_input, space_name_create_input, owner_create_input, sdk_create_input, markdown_input_create], create_output_md)
|
| 200 |
|
| 201 |
|
| 202 |
# --- "Browse & Edit Files" Tab (Hub-based) ---
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# --- FastAPI App Initialization ---
|
| 19 |
+
description = """
|
| 20 |
+
API to create, manage, and delete Hugging Face Spaces.
|
| 21 |
+
|
| 22 |
+
**Note on Hugging Face Spaces:** Spaces only expose port 7860 for HTTP/HTTPS traffic.
|
| 23 |
+
|
| 24 |
+
### Markdown Format for AI Model Requests
|
| 25 |
+
|
| 26 |
+
To have an AI model generate the correct markdown for creating a space, use the following prompt structure.
|
| 27 |
+
|
| 28 |
+
**Prompt:**
|
| 29 |
+
|
| 30 |
+
Generate program files for a project as a single plain text string, strictly adhering to the markdown format below. **Every single line**, including backticks, language identifiers, file content, and empty lines, **must** be prefixed with '# ' to comment it out. This is critical to avoid code box interference. The output must include a complete file structure and the contents of each file, with all necessary code and configurations for a functional project. **Do not deviate from this format under any circumstances.**
|
| 31 |
+
|
| 32 |
+
**Format (exact return format with single leading "# "):**
|
| 33 |
+
```
|
| 34 |
+
# # Space: [owner/project-name]
|
| 35 |
+
# ## File Structure
|
| 36 |
+
# ```
|
| 37 |
+
# 📁 Root
|
| 38 |
+
# 📄 [file1]
|
| 39 |
+
# 📄 [file2]
|
| 40 |
+
# ...
|
| 41 |
+
# ```
|
| 42 |
+
#
|
| 43 |
+
# Below are the contents of all files in the space:
|
| 44 |
+
#
|
| 45 |
+
# ### File: [file1]
|
| 46 |
+
# ```[language]
|
| 47 |
+
# [content]
|
| 48 |
+
# ```
|
| 49 |
+
#
|
| 50 |
+
# ### File: [file2]
|
| 51 |
+
# ```[language]
|
| 52 |
+
# [content]
|
| 53 |
+
# ```
|
| 54 |
+
#
|
| 55 |
+
# ... (repeat for each file)
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
**Correct Example Output (exact, every line prefixed with '# '):**
|
| 59 |
+
```
|
| 60 |
+
# # Space: user/my-app
|
| 61 |
+
# ## File Structure
|
| 62 |
+
# ```
|
| 63 |
+
# 📁 Root
|
| 64 |
+
# 📄 app.py
|
| 65 |
+
# 📄 requirements.txt
|
| 66 |
+
# ```
|
| 67 |
+
#
|
| 68 |
+
# # Below are the contents of all files in the space:
|
| 69 |
+
#
|
| 70 |
+
# ### File: app.py
|
| 71 |
+
# ```python
|
| 72 |
+
# print("Hello, World!")
|
| 73 |
+
# ```
|
| 74 |
+
#
|
| 75 |
+
# ### File: requirements.txt
|
| 76 |
+
# ```text
|
| 77 |
+
# gradio==4.44.0
|
| 78 |
+
# ```
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
**Instructions:**
|
| 82 |
+
- Use exactly `# # Space: [owner/project-name]` as the header (e.g., `user/my-app`).
|
| 83 |
+
- Under `## File Structure`, start with `# 📁 Root` followed by `# 📄` for each file, using exact icons and spacing.
|
| 84 |
+
- For each file, use `# ### File: [filename]` followed by a code block where every line, including backticks (e.g., `# ```), language identifier (e.g., `# python`), and content (e.g., `# print("Hello")`), is prefixed with `# `.
|
| 85 |
+
- For binary files, use `# [Binary file - size in bytes]` as content with no language identifier.
|
| 86 |
+
- **Every line** must start with `# `, no exceptions, including empty lines within code blocks.
|
| 87 |
+
- Provide accurate, functional code or content for each file, suitable for the project’s purpose.
|
| 88 |
+
- Ensure the output is concise, complete, and parseable to extract file structure and contents.
|
| 89 |
+
- Output everything as a single plain text string within one code box.
|
| 90 |
+
"""
|
| 91 |
app = FastAPI(
|
| 92 |
title="Hugging Face Space Builder API",
|
| 93 |
+
description=description,
|
| 94 |
version="1.0.0",
|
| 95 |
)
|
| 96 |
|
|
|
|
| 101 |
owner: Optional[str] = None
|
| 102 |
sdk: str = "gradio"
|
| 103 |
markdown_content: str
|
| 104 |
+
private: bool = False
|
| 105 |
|
| 106 |
class SpaceUpdate(BaseModel):
|
| 107 |
api_token: str
|
|
|
|
| 124 |
space_data.space_name,
|
| 125 |
space_data.owner,
|
| 126 |
space_data.sdk,
|
| 127 |
+
space_data.markdown_content,
|
| 128 |
+
space_data.private
|
| 129 |
)
|
| 130 |
if "Error" in result:
|
| 131 |
raise HTTPException(status_code=400, detail=result)
|
|
|
|
| 266 |
space_name_create_input = gr.Textbox(label="Space Name", placeholder="my-awesome-app (no slashes)", scale=2)
|
| 267 |
owner_create_input = gr.Textbox(label="Owner Username/Org", placeholder="Leave blank for your HF username", scale=1)
|
| 268 |
sdk_create_input = gr.Dropdown(label="Space SDK", choices=["gradio", "streamlit", "docker", "static"], value="gradio")
|
| 269 |
+
private_create_input = gr.Checkbox(label="Private Space", value=False)
|
| 270 |
gr.Markdown("### Example Source: [/spaces/broadfield-dev/repo_to_md](https://huggingface.co/spaces/broadfield-dev/repo_to_md)")
|
| 271 |
markdown_input_create = gr.Textbox(label="Markdown File Structure & Content", placeholder="Example:\n### File: app.py\n# ```python\nprint(\"Hello\")\n# ```", lines=15, interactive=True)
|
| 272 |
create_btn = gr.Button("Create Space", variant="primary")
|
| 273 |
create_output_md = gr.Markdown(label="Result")
|
| 274 |
+
create_btn.click(create_space, [api_token_ui_input, space_name_create_input, owner_create_input, sdk_create_input, markdown_input_create, private_create_input], create_output_md)
|
| 275 |
|
| 276 |
|
| 277 |
# --- "Browse & Edit Files" Tab (Hub-based) ---
|
app_logic.py
CHANGED
|
@@ -202,7 +202,7 @@ def list_space_files_for_browsing(ui_api_token_from_textbox, space_name_ui, owne
|
|
| 202 |
|
| 203 |
|
| 204 |
# --- Core Functions: `create_space`, `update_space_file` (Unchanged from previous correct versions) ---
|
| 205 |
-
def create_space(ui_api_token_from_textbox, space_name_ui, owner_ui, sdk_ui, markdown_input):
|
| 206 |
repo_id_for_error_logging = f"{owner_ui}/{space_name_ui}" if owner_ui else space_name_ui
|
| 207 |
try:
|
| 208 |
resolved_api_token, token_err = _get_api_token(ui_api_token_from_textbox)
|
|
@@ -221,7 +221,7 @@ def create_space(ui_api_token_from_textbox, space_name_ui, owner_ui, sdk_ui, mar
|
|
| 221 |
file_path_abs.parent.mkdir(parents=True, exist_ok=True)
|
| 222 |
with open(file_path_abs, "w", encoding="utf-8") as f: f.write(file_info["content"])
|
| 223 |
try:
|
| 224 |
-
create_repo(repo_id=repo_id, token=resolved_api_token, repo_type="space", space_sdk=sdk_ui, private=
|
| 225 |
except Exception as e:
|
| 226 |
err_str = str(e).lower()
|
| 227 |
if not ("already exists" in err_str or "you already created this repo" in err_str or "exists" in err_str):
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
# --- Core Functions: `create_space`, `update_space_file` (Unchanged from previous correct versions) ---
|
| 205 |
+
def create_space(ui_api_token_from_textbox, space_name_ui, owner_ui, sdk_ui, markdown_input, private_ui: bool = False):
|
| 206 |
repo_id_for_error_logging = f"{owner_ui}/{space_name_ui}" if owner_ui else space_name_ui
|
| 207 |
try:
|
| 208 |
resolved_api_token, token_err = _get_api_token(ui_api_token_from_textbox)
|
|
|
|
| 221 |
file_path_abs.parent.mkdir(parents=True, exist_ok=True)
|
| 222 |
with open(file_path_abs, "w", encoding="utf-8") as f: f.write(file_info["content"])
|
| 223 |
try:
|
| 224 |
+
create_repo(repo_id=repo_id, token=resolved_api_token, repo_type="space", space_sdk=sdk_ui, private=private_ui)
|
| 225 |
except Exception as e:
|
| 226 |
err_str = str(e).lower()
|
| 227 |
if not ("already exists" in err_str or "you already created this repo" in err_str or "exists" in err_str):
|