broadfield-dev commited on
Commit
bd49611
·
verified ·
1 Parent(s): c08b935

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +179 -1
README.md CHANGED
@@ -9,4 +9,182 @@ app_file: app.py
9
  pinned: false
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  pinned: false
10
  ---
11
 
12
+ Below is detailed documentation for the Assembler Space API, designed to be clear and precise for another LLM (or human developer) to understand and use effectively. It covers the API’s purpose, endpoints, request format, response structure, examples, and considerations.
13
+ Assembler Space API Documentation
14
+ Overview
15
+ The Assembler Space is a Hugging Face Space that acts as a factory for creating new Hugging Face Spaces dynamically. It exposes a Flask-based API that accepts JSON input containing code, files, parameters, and configuration details. Upon receiving a valid request, it creates a new Space repository on Hugging Face, populates it with the provided files, and triggers its deployment. The API supports multiple Space types (e.g., gradio, static, docker, streamlit) and multi-file submissions, making it versatile for generating a wide range of applications.
16
+ The Assembler Space itself runs as a Docker-based Space on Hugging Face, accessible via a public URL once deployed.
17
+ Base URL
18
+ The API is hosted at:
19
+ https://<your-username>-assembler.hf.space/create-space
20
+ Replace <your-username> with the Hugging Face username hosting the Assembler Space. The exact URL will be provided once the Space is deployed.
21
+ Endpoint
22
+ POST /create-space
23
+ Creates a new Hugging Face Space based on the provided JSON payload.
24
+ Request Format
25
+ Method: POST
26
+ Content-Type: application/json
27
+ Body: A JSON object with the following fields:
28
+ Field
29
+ Type
30
+ Required
31
+ Description
32
+ space_type
33
+ String
34
+ No
35
+ The type of Space to create. Options: gradio, static, docker, streamlit. Defaults to gradio if omitted.
36
+ files
37
+ Object
38
+ Yes
39
+ A dictionary where keys are filenames (e.g., app.py, index.html) and values are file contents as strings.
40
+ parameters
41
+ Object
42
+ No
43
+ A dictionary of key-value pairs to be injected into Python files as PARAMS or used by the generated Space’s code.
44
+ Request Constraints
45
+ At least one file must be provided in files.
46
+ Filenames in files should include extensions (e.g., .py, .html, .css, .txt) to ensure correct handling.
47
+ space_type must match one of the supported values, or the request will fail.
48
+ File contents should be valid for the intended space_type (e.g., Python code for gradio or docker, HTML for static).
49
+ Response Format
50
+ Content-Type: application/json
51
+ Status Codes:
52
+ 200 OK: Space creation succeeded.
53
+ 400 Bad Request: Invalid JSON or missing/invalid fields.
54
+ 500 Internal Server Error: Unexpected error during Space creation.
55
+ Body: A JSON object with the following fields:
56
+ Field
57
+ Type
58
+ Description
59
+ message
60
+ String
61
+ A brief status message (e.g., "New Space created").
62
+ url
63
+ String
64
+ The URL of the newly created Space (e.g., https://huggingface.co/spaces/<username>/<space-name>).
65
+ note
66
+ String
67
+ Additional information (e.g., deployment time warning).
68
+ error
69
+ String
70
+ (Only in error responses) Description of what went wrong.
71
+ Example Requests and Responses
72
+ Example 1: Static Space
73
+ Request:
74
+ json
75
+ {
76
+ "space_type": "static",
77
+ "files": {
78
+ "index.html": "<html><body><h1>Hello World</h1><p>Message: {{params['message']}}</p></body></html>",
79
+ "style.css": "h1 { color: green; }"
80
+ },
81
+ "parameters": {
82
+ "message": "Static Space Test"
83
+ }
84
+ }
85
+ Response (200 OK):
86
+ json
87
+ {
88
+ "message": "New Space created",
89
+ "url": "https://huggingface.co/spaces/your-username/GeneratedSpace-abc123",
90
+ "note": "It may take a few minutes to build and deploy."
91
+ }
92
+ Notes:
93
+ Static Spaces don’t automatically process parameters. The new Space’s code must handle them (e.g., via JavaScript or server-side templating if added).
94
+ Example 2: Docker Space with Flask
95
+ Request:
96
+ json
97
+ {
98
+ "space_type": "docker",
99
+ "files": {
100
+ "app.py": "from flask import Flask\napp = Flask(__name__)\[email protected]('/')\ndef home():\n return f'Hello, {PARAMS['name']}'\nif __name__ == '__main__':\n app.run(host='0.0.0.0', port=7860)",
101
+ "requirements.txt": "flask",
102
+ "Dockerfile": "FROM python:3.10-slim\nWORKDIR /app\nCOPY . .\nRUN pip install -r requirements.txt\nEXPOSE 7860\nCMD [\"python\", \"app.py\"]"
103
+ },
104
+ "parameters": {
105
+ "name": "Docker User"
106
+ }
107
+ }
108
+ Response (200 OK):
109
+ json
110
+ {
111
+ "message": "New Space created",
112
+ "url": "https://huggingface.co/spaces/your-username/GeneratedSpace-xyz789",
113
+ "note": "It may take a few minutes to build and deploy."
114
+ }
115
+ Notes:
116
+ The parameters are injected into app.py as PARAMS. The port is set to 7860 to match Hugging Face’s default.
117
+ Example 3: Gradio Space
118
+ Request:
119
+ json
120
+ {
121
+ "space_type": "gradio",
122
+ "files": {
123
+ "app.py": "import gradio as gr\ndef greet():\n return f'Hi, {PARAMS['user']}'\ninterface = gr.Interface(fn=greet, inputs=None, outputs='text')\ninterface.launch()"
124
+ },
125
+ "parameters": {
126
+ "user": "Gradio Fan"
127
+ }
128
+ }
129
+ Response (200 OK):
130
+ json
131
+ {
132
+ "message": "New Space created",
133
+ "url": "https://huggingface.co/spaces/your-username/GeneratedSpace-def456",
134
+ "note": "It may take a few minutes to build and deploy."
135
+ }
136
+ Example 4: Invalid Request
137
+ Request:
138
+ json
139
+ {
140
+ "space_type": "invalid",
141
+ "files": {}
142
+ }
143
+ Response (400 Bad Request):
144
+ json
145
+ {
146
+ "error": "Invalid space_type. Must be one of ['gradio', 'static', 'docker', 'streamlit']"
147
+ }
148
+ Usage Example (Python)
149
+ Here’s how to call the API using Python’s requests library:
150
+ python
151
+ import requests
152
+
153
+ url = "https://your-username-assembler.hf.space/create-space"
154
+ payload = {
155
+ "space_type": "static",
156
+ "files": {
157
+ "index.html": "<html><body><h1>Test Page</h1></body></html>"
158
+ },
159
+ "parameters": {
160
+ "key": "value"
161
+ }
162
+ }
163
+ headers = {"Content-Type": "application/json"}
164
+
165
+ response = requests.post(url, json=payload)
166
+ if response.status_code == 200:
167
+ print("Success:", response.json())
168
+ else:
169
+ print("Error:", response.status_code, response.json())
170
+ Additional Details
171
+ Supported Space Types
172
+ gradio: For interactive Python apps using the Gradio framework. Requires an app.py with Gradio code.
173
+ static: For static websites. Requires at least an index.html file; supports additional files like style.css.
174
+ docker: For custom apps (e.g., Flask, FastAPI). Requires a Dockerfile or uses a default one if omitted.
175
+ streamlit: For Streamlit apps. Requires an app.py with Streamlit code.
176
+ File Handling
177
+ Python Files (.py): The parameters object is injected as a global PARAMS variable at the top of the file.
178
+ Other Files: Contents are uploaded as-is; no automatic parameter injection (e.g., HTML files need custom logic to use parameters).
179
+ Requirements: If requirements.txt isn’t provided, a default is generated based on space_type (e.g., gradio for Gradio, flask for Docker).
180
+ Deployment Notes
181
+ Build Time: New Spaces take 1-5 minutes to build and deploy on Hugging Face. The API returns immediately with a URL, but the Space won’t be live until the build completes.
182
+ Port for Docker: Hugging Face expects Docker Spaces to listen on port 7860. Ensure your Dockerfile and app code align with this (see Example 2).
183
+ Security Considerations
184
+ The Assembler doesn’t execute the provided code; it only creates a new Space. However, ensure the generated Space’s code is safe if deploying publicly.
185
+ Avoid sending sensitive data in parameters unless the new Space is private (not implemented here but can be adjusted).
186
+ Limitations
187
+ Rate Limits: Hugging Face may restrict frequent Space creation. Check their API documentation for quotas.
188
+ File Size: Large files may fail to upload; keep contents reasonable (e.g., <1MB per file).
189
+ Error Handling: Basic validation is included, but complex syntax checking isn’t performed.
190
+ This documentation should equip another LLM or developer to use the Assembler Space API correctly. Let me know if you’d like to expand any section or add more examples!