Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -241,95 +241,95 @@ def create_hf_space():
|
|
241 |
data = request.get_json()
|
242 |
if not data:
|
243 |
return jsonify({"error": "No JSON data provided"}), 400
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
repo_type="space",
|
264 |
-
space_sdk=space_type,
|
265 |
-
token=HF_TOKEN,
|
266 |
-
private=False
|
267 |
-
)
|
268 |
-
|
269 |
-
# Handle multi-file uploads
|
270 |
-
for filename, content in files.items():
|
271 |
-
# Write content to a temporary file
|
272 |
-
with open(f"temp_{filename}", "w") as f:
|
273 |
-
if filename.endswith(".py"):
|
274 |
-
# Inject parameters into Python files if present
|
275 |
-
content = f"PARAMS = {json.dumps(params)}\n\n{content}"
|
276 |
-
f.write(content)
|
277 |
-
|
278 |
-
# Upload to the new Space
|
279 |
-
upload_file(
|
280 |
-
path_or_fileobj=f"temp_{filename}",
|
281 |
-
path_in_repo=filename,
|
282 |
-
repo_id=full_repo_id,
|
283 |
repo_type="space",
|
284 |
-
|
|
|
|
|
285 |
)
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
"""
|
317 |
-
with open("temp_Dockerfile", "w") as f:
|
318 |
-
f.write(default_dockerfile)
|
319 |
upload_file(
|
320 |
-
path_or_fileobj="
|
321 |
-
path_in_repo="
|
322 |
repo_id=full_repo_id,
|
323 |
repo_type="space",
|
324 |
token=HF_TOKEN
|
325 |
)
|
326 |
-
os.remove("
|
327 |
-
|
328 |
-
|
329 |
-
"
|
330 |
-
|
331 |
-
|
332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|
334 |
except json.JSONDecodeError:
|
335 |
return jsonify({"error": "Invalid JSON format"}), 400
|
|
|
241 |
data = request.get_json()
|
242 |
if not data:
|
243 |
return jsonify({"error": "No JSON data provided"}), 400
|
244 |
+
# Extract parameters
|
245 |
+
space_type = data.get("space_type", "gradio") # Default to gradio if not specified
|
246 |
+
files = data.get("files", {}) # Dictionary of filename: content
|
247 |
+
params = data.get("parameters", {}) # Optional parameters
|
248 |
+
|
249 |
+
if not files:
|
250 |
+
return jsonify({"error": "No files provided in JSON"}), 400
|
251 |
+
|
252 |
+
# Validate space_type
|
253 |
+
valid_space_types = ["gradio", "static", "docker", "streamlit"]
|
254 |
+
if space_type not in valid_space_types:
|
255 |
+
return jsonify({"error": f"Invalid space_type. Must be one of {valid_space_types}"}), 400
|
256 |
+
|
257 |
+
# Create a unique Space name and repo
|
258 |
+
space_name = generate_space_name()
|
259 |
+
full_repo_id = f"{hf_api.whoami(HF_TOKEN)['name']}/{space_name}"
|
260 |
+
|
261 |
+
create_repo(
|
262 |
+
repo_id=space_name,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
263 |
repo_type="space",
|
264 |
+
space_sdk=space_type,
|
265 |
+
token=HF_TOKEN,
|
266 |
+
private=False
|
267 |
)
|
268 |
+
|
269 |
+
# Handle multi-file uploads
|
270 |
+
for filename, content in files.items():
|
271 |
+
# Write content to a temporary file
|
272 |
+
with open(f"temp_{filename}", "w") as f:
|
273 |
+
if filename.endswith(".py"):
|
274 |
+
# Inject parameters into Python files if present
|
275 |
+
content = f"PARAMS = {json.dumps(params)}\n\n{content}"
|
276 |
+
f.write(content)
|
277 |
+
|
278 |
+
# Upload to the new Space
|
279 |
+
upload_file(
|
280 |
+
path_or_fileobj=f"temp_{filename}",
|
281 |
+
path_in_repo=filename,
|
282 |
+
repo_id=full_repo_id,
|
283 |
+
repo_type="space",
|
284 |
+
token=HF_TOKEN
|
285 |
+
)
|
286 |
+
os.remove(f"temp_{filename}")
|
287 |
+
|
288 |
+
# Add requirements.txt if not provided (basic defaults)
|
289 |
+
if "requirements.txt" not in files:
|
290 |
+
default_requirements = {
|
291 |
+
"gradio": "gradio",
|
292 |
+
"static": "",
|
293 |
+
"docker": "flask", # Example; adjust based on needs
|
294 |
+
"streamlit": "streamlit"
|
295 |
+
}.get(space_type, "")
|
296 |
+
with open("temp_requirements.txt", "w") as f:
|
297 |
+
f.write(default_requirements)
|
|
|
|
|
|
|
298 |
upload_file(
|
299 |
+
path_or_fileobj="temp_requirements.txt",
|
300 |
+
path_in_repo="requirements.txt",
|
301 |
repo_id=full_repo_id,
|
302 |
repo_type="space",
|
303 |
token=HF_TOKEN
|
304 |
)
|
305 |
+
os.remove("temp_requirements.txt")
|
306 |
+
|
307 |
+
# Special handling for Docker Spaces
|
308 |
+
if space_type == "docker" and "Dockerfile" not in files:
|
309 |
+
default_dockerfile = """
|
310 |
+
FROM python:3.10-slim
|
311 |
+
WORKDIR /app
|
312 |
+
COPY . .
|
313 |
+
RUN pip install -r requirements.txt
|
314 |
+
EXPOSE 7860
|
315 |
+
CMD ["python", "app.py"]
|
316 |
+
"""
|
317 |
+
with open("temp_Dockerfile", "w") as f:
|
318 |
+
f.write(default_dockerfile)
|
319 |
+
upload_file(
|
320 |
+
path_or_fileobj="temp_Dockerfile",
|
321 |
+
path_in_repo="Dockerfile",
|
322 |
+
repo_id=full_repo_id,
|
323 |
+
repo_type="space",
|
324 |
+
token=HF_TOKEN
|
325 |
+
)
|
326 |
+
os.remove("temp_Dockerfile")
|
327 |
+
space_url = f"https://huggingface.co/spaces/{full_repo_id}"
|
328 |
+
return jsonify({
|
329 |
+
"message": "New Space created",
|
330 |
+
"url": space_url,
|
331 |
+
"note": "It may take a few minutes to build and deploy."
|
332 |
+
}), 200
|
333 |
|
334 |
except json.JSONDecodeError:
|
335 |
return jsonify({"error": "Invalid JSON format"}), 400
|