mgbam commited on
Commit
846f240
·
verified ·
1 Parent(s): 692fda2

Update deployment.py

Browse files
Files changed (1) hide show
  1. deployment.py +23 -3
deployment.py CHANGED
@@ -4,21 +4,41 @@ 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
 
 
 
 
 
 
 
 
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
+ """
8
+ Creates or updates a Hugging Face Space and uploads the generated code.
9
+ """
10
+ if not code.strip():
11
+ return "Cannot deploy: No code has been generated."
12
+ if not space_name.strip():
13
+ return "Cannot deploy: Please provide a name for your app."
14
 
15
  try:
16
  api = HfApi(token=hf_token)
17
+ # Get the username associated with the token
18
  username = api.whoami(token=hf_token)['name']
19
  repo_id = f"{username}/{space_name.strip()}"
20
+
21
+ # Create the repository on the Hub
22
  api.create_repo(repo_id, repo_type="space", space_sdk=sdk, exist_ok=True)
23
 
24
+ # Determine the correct filename based on the SDK
25
  file_path_in_repo = "index.html" if sdk == 'static' else "app.py"
26
+
27
+ # Upload the generated code file
28
  api.upload_file(
29
  path_or_fileobj=code.encode('utf-8'),
30
  path_in_repo=file_path_in_repo,
31
  repo_id=repo_id,
32
  repo_type="space"
33
  )
34
+
35
+ # Construct the URL to the newly deployed Space
36
  space_url = f"https://huggingface.co/spaces/{repo_id}"
37
+
38
+ # <--- FIX: The string literal is now correctly terminated ---
39
+ return f"✅ Deployed successfully! [Open your Space]({space_url})"
40
+
41
+ except Exception as e:
42
+ # Provide a more informative error message
43
+ logging.error(f"Failed to deploy to Hugging Face Space: {e}")
44
+ return f"❌ Deployment failed: {str(e)}"