Update app.py
Browse files
app.py
CHANGED
@@ -12,11 +12,11 @@ def process(git_repo_url, space_destination, user_token):
|
|
12 |
your_username = api.whoami(token=user_token)["name"]
|
13 |
|
14 |
# Check if space ID is correct including username
|
15 |
-
if
|
16 |
print("username is present: good to continue")
|
17 |
else:
|
18 |
space_destination = f"{your_username}/{space_destination}"
|
19 |
-
print("user forgot to mention
|
20 |
|
21 |
# Create a temporary directory to clone the repository
|
22 |
tmp_dir = tempfile.mkdtemp()
|
@@ -24,6 +24,15 @@ def process(git_repo_url, space_destination, user_token):
|
|
24 |
# Clone the Hugging Face Spaces repository
|
25 |
repo = Repo.clone_from(git_repo_url, tmp_dir)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Log the contents of the tmp_dir with its subfolders
|
28 |
print("Contents of the cloned repository:")
|
29 |
for root, dirs, files in os.walk(tmp_dir):
|
@@ -41,13 +50,31 @@ def process(git_repo_url, space_destination, user_token):
|
|
41 |
repo_type="space",
|
42 |
token=user_token
|
43 |
)
|
44 |
-
return "
|
45 |
|
46 |
except HfHubHTTPError as e:
|
47 |
# Check if the error is a "Repository Not Found" error
|
48 |
if "404 Client Error" in str(e) and "Repository Not Found" in str(e):
|
49 |
print("Repository not found. Maybe we should Create the repository...")
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
else:
|
52 |
# Re-raise the exception if it's not a "Repository Not Found" error
|
53 |
raise e
|
|
|
12 |
your_username = api.whoami(token=user_token)["name"]
|
13 |
|
14 |
# Check if space ID is correct including username
|
15 |
+
if f"{your_username}/" in str(space_destination):
|
16 |
print("username is present: good to continue")
|
17 |
else:
|
18 |
space_destination = f"{your_username}/{space_destination}"
|
19 |
+
print(f"user forgot to mention a username in space_destination: {space_destination}")
|
20 |
|
21 |
# Create a temporary directory to clone the repository
|
22 |
tmp_dir = tempfile.mkdtemp()
|
|
|
24 |
# Clone the Hugging Face Spaces repository
|
25 |
repo = Repo.clone_from(git_repo_url, tmp_dir)
|
26 |
|
27 |
+
# Check if README.md already exists in tmp_dir
|
28 |
+
readme_path = os.path.join(tmp_dir, "README.md")
|
29 |
+
original_readme_path = os.path.join(tmp_dir, "ORIGINAL_README.md")
|
30 |
+
|
31 |
+
# Rename the existing README.md if it exists
|
32 |
+
if os.path.exists(readme_path):
|
33 |
+
print("Existing README.md found, renaming to ORIGINAL_README.md")
|
34 |
+
os.rename(readme_path, original_readme_path)
|
35 |
+
|
36 |
# Log the contents of the tmp_dir with its subfolders
|
37 |
print("Contents of the cloned repository:")
|
38 |
for root, dirs, files in os.walk(tmp_dir):
|
|
|
50 |
repo_type="space",
|
51 |
token=user_token
|
52 |
)
|
53 |
+
return f"Repository cloned ! Find it at hf.co/spaces/{space_destination}"
|
54 |
|
55 |
except HfHubHTTPError as e:
|
56 |
# Check if the error is a "Repository Not Found" error
|
57 |
if "404 Client Error" in str(e) and "Repository Not Found" in str(e):
|
58 |
print("Repository not found. Maybe we should Create the repository...")
|
59 |
+
|
60 |
+
api.create_repo(
|
61 |
+
repo_id=space_destination,
|
62 |
+
token=user_token,
|
63 |
+
private=True,
|
64 |
+
repo_type="space",
|
65 |
+
space_sdk="gradio"
|
66 |
+
)
|
67 |
+
|
68 |
+
print(f"New space repo created: {space_destination}")
|
69 |
+
|
70 |
+
api.upload_folder(
|
71 |
+
folder_path=tmp_dir,
|
72 |
+
repo_id=space_destination,
|
73 |
+
repo_type="space",
|
74 |
+
token=user_token
|
75 |
+
)
|
76 |
+
|
77 |
+
return f"Repository cloned ! Find it at hf.co/spaces/{space_destination}"
|
78 |
else:
|
79 |
# Re-raise the exception if it's not a "Repository Not Found" error
|
80 |
raise e
|