Update deployment.py
Browse files- deployment.py +11 -107
deployment.py
CHANGED
@@ -1,120 +1,24 @@
|
|
1 |
# /deployment.py
|
2 |
-
|
3 |
-
"""
|
4 |
-
Handles deployment of generated code to Hugging Face Spaces.
|
5 |
-
|
6 |
-
This module provides functions to wrap generated code into a runnable
|
7 |
-
Gradio or Static HTML app and to programmatically create and upload
|
8 |
-
it to a user's Hugging Face Space.
|
9 |
-
"""
|
10 |
-
import tempfile
|
11 |
-
import webbrowser
|
12 |
import logging
|
13 |
-
from
|
14 |
-
|
15 |
-
from huggingface_hub import HfApi, HfFolder
|
16 |
-
import gradio as gr
|
17 |
-
|
18 |
-
def _create_space_readme(space_name: str, sdk: str) -> str:
|
19 |
-
"""Generates a standard README.md file for the new Space."""
|
20 |
-
return f"""---
|
21 |
-
title: {space_name}
|
22 |
-
emoji: π
|
23 |
-
colorFrom: blue
|
24 |
-
colorTo: green
|
25 |
-
sdk: {sdk}
|
26 |
-
---
|
27 |
-
|
28 |
-
# {space_name}
|
29 |
-
|
30 |
-
This Space was generated by [AnyCoder](<YOUR_APP_SPACE_URL>).
|
31 |
-
"""
|
32 |
-
|
33 |
-
def deploy_to_hf_space(
|
34 |
-
code: str,
|
35 |
-
space_name: str,
|
36 |
-
sdk: str,
|
37 |
-
hf_token: str
|
38 |
-
) -> str:
|
39 |
-
"""
|
40 |
-
Creates or updates a Hugging Face Space and uploads the generated code.
|
41 |
-
|
42 |
-
Args:
|
43 |
-
code: The code to deploy (HTML or Python).
|
44 |
-
space_name: The desired name for the Space.
|
45 |
-
sdk: The SDK for the Space ('static', 'gradio', 'streamlit').
|
46 |
-
hf_token: The user's Hugging Face write token.
|
47 |
-
|
48 |
-
Returns:
|
49 |
-
A success or error message with a link to the Space.
|
50 |
-
"""
|
51 |
-
if not code or not code.strip():
|
52 |
-
return "Cannot deploy: No code has been generated."
|
53 |
-
if not space_name or not space_name.strip():
|
54 |
-
return "Cannot deploy: Please provide a name for your app."
|
55 |
-
if not hf_token:
|
56 |
-
# Fallback to URL-based deployment if no token is provided
|
57 |
-
return deploy_via_url(code, space_name, sdk)
|
58 |
|
|
|
|
|
|
|
|
|
59 |
try:
|
60 |
api = HfApi(token=hf_token)
|
61 |
-
|
62 |
-
username = user_info['name']
|
63 |
repo_id = f"{username}/{space_name.strip()}"
|
64 |
-
|
65 |
api.create_repo(repo_id, repo_type="space", space_sdk=sdk, exist_ok=True)
|
66 |
-
|
67 |
-
if sdk == 'static'
|
68 |
-
file_content = code
|
69 |
-
file_path_in_repo = "index.html"
|
70 |
-
else: # gradio or streamlit
|
71 |
-
file_content = code # Assume code is already wrapped for Python
|
72 |
-
file_path_in_repo = "app.py"
|
73 |
-
|
74 |
-
# Upload the main app file
|
75 |
api.upload_file(
|
76 |
-
path_or_fileobj=
|
77 |
path_in_repo=file_path_in_repo,
|
78 |
repo_id=repo_id,
|
79 |
repo_type="space"
|
80 |
)
|
81 |
-
# Upload a README
|
82 |
-
readme_content = _create_space_readme(space_name, sdk)
|
83 |
-
api.upload_file(
|
84 |
-
path_or_fileobj=readme_content.encode('utf-8'),
|
85 |
-
path_in_repo="README.md",
|
86 |
-
repo_id=repo_id,
|
87 |
-
repo_type="space"
|
88 |
-
)
|
89 |
-
|
90 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
91 |
-
return f"β
Deployed successfully! [Open your
|
92 |
-
|
93 |
-
except Exception as e:
|
94 |
-
logging.error(f"Failed to deploy to Hugging Face Space: {e}")
|
95 |
-
return f"β Deployment failed: {str(e)}"
|
96 |
-
|
97 |
-
def deploy_via_url(code: str, space_name: str, sdk: str) -> str:
|
98 |
-
"""
|
99 |
-
Opens a new browser tab with pre-filled parameters to create a Space.
|
100 |
-
This is a fallback for users who are not logged in via OAuth.
|
101 |
-
"""
|
102 |
-
if sdk == 'static':
|
103 |
-
app_file = "index.html"
|
104 |
-
content = code
|
105 |
-
else:
|
106 |
-
app_file = "app.py"
|
107 |
-
# Basic wrapping for Python-based SDKs if needed
|
108 |
-
content = code # Assuming code is python string
|
109 |
-
|
110 |
-
params = urlencode({
|
111 |
-
"name": space_name,
|
112 |
-
"sdk": sdk,
|
113 |
-
"files[0][path]": app_file,
|
114 |
-
"files[0][content]": content
|
115 |
-
})
|
116 |
-
base_url = "https://huggingface.co/new-space"
|
117 |
-
full_url = f"{base_url}?{params}"
|
118 |
-
|
119 |
-
webbrowser.open_new_tab(full_url)
|
120 |
-
return "π Your app is ready to launch! Check the new browser tab."
|
|
|
1 |
# /deployment.py
|
2 |
+
""" Handles deployment of generated code to Hugging Face Spaces. """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import logging
|
4 |
+
from huggingface_hub import HfApi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
def deploy_to_hf_space(code: str, space_name: str, sdk: str, hf_token: str) -> str:
|
7 |
+
if not code.strip(): return "Cannot deploy: No code generated."
|
8 |
+
if not space_name.strip(): return "Cannot deploy: Please provide an app name."
|
9 |
+
|
10 |
try:
|
11 |
api = HfApi(token=hf_token)
|
12 |
+
username = api.whoami(token=hf_token)['name']
|
|
|
13 |
repo_id = f"{username}/{space_name.strip()}"
|
|
|
14 |
api.create_repo(repo_id, repo_type="space", space_sdk=sdk, exist_ok=True)
|
15 |
+
|
16 |
+
file_path_in_repo = "index.html" if sdk == 'static' else "app.py"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
api.upload_file(
|
18 |
+
path_or_fileobj=code.encode('utf-8'),
|
19 |
path_in_repo=file_path_in_repo,
|
20 |
repo_id=repo_id,
|
21 |
repo_type="space"
|
22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
space_url = f"https://huggingface.co/spaces/{repo_id}"
|
24 |
+
return f"β
Deployed successfully! [Open your
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|