Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,73 +2,40 @@ import os
|
|
| 2 |
import shutil
|
| 3 |
import tempfile
|
| 4 |
from git import Repo
|
| 5 |
-
from huggingface_hub import
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
-
|
| 9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
def clone_and_push(
|
| 13 |
-
github_repo_url: str,
|
| 14 |
-
target_space: str,
|
| 15 |
-
commit_message: str = "Sync from GitHub"
|
| 16 |
-
):
|
| 17 |
-
"""
|
| 18 |
-
Clone a GitHub repo and push it to a Hugging Face Space
|
| 19 |
-
"""
|
| 20 |
|
|
|
|
| 21 |
if not HF_TOKEN:
|
| 22 |
-
return "β HF_TOKEN not
|
| 23 |
|
| 24 |
tmp_dir = tempfile.mkdtemp()
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
# 1οΈβ£ Clone GitHub repository
|
| 28 |
repo_path = os.path.join(tmp_dir, "github_repo")
|
| 29 |
-
Repo.clone_from(github_repo_url, repo_path)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
for item in os.listdir(space_path):
|
| 42 |
-
if item != ".git":
|
| 43 |
-
item_path = os.path.join(space_path, item)
|
| 44 |
-
if os.path.isdir(item_path):
|
| 45 |
-
shutil.rmtree(item_path)
|
| 46 |
-
else:
|
| 47 |
-
os.remove(item_path)
|
| 48 |
-
|
| 49 |
-
# 4οΈβ£ Copy GitHub repo contents into Space
|
| 50 |
-
for item in os.listdir(repo_path):
|
| 51 |
-
if item == ".git":
|
| 52 |
-
continue
|
| 53 |
-
src = os.path.join(repo_path, item)
|
| 54 |
-
dst = os.path.join(space_path, item)
|
| 55 |
-
if os.path.isdir(src):
|
| 56 |
-
shutil.copytree(src, dst)
|
| 57 |
-
else:
|
| 58 |
-
shutil.copy2(src, dst)
|
| 59 |
-
|
| 60 |
-
# 5οΈβ£ Commit & push
|
| 61 |
-
repo = Repo(space_path)
|
| 62 |
-
repo.git.add(A=True)
|
| 63 |
-
repo.index.commit(commit_message)
|
| 64 |
-
|
| 65 |
-
with repo.git.custom_environment(
|
| 66 |
-
GIT_USERNAME="hf",
|
| 67 |
-
GIT_PASSWORD=HF_TOKEN
|
| 68 |
-
):
|
| 69 |
-
repo.remote("origin").push()
|
| 70 |
-
|
| 71 |
-
return "β
Repository successfully synced to Hugging Face Space!"
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
return f"β Error: {str(e)}"
|
|
@@ -77,33 +44,11 @@ def clone_and_push(
|
|
| 77 |
shutil.rmtree(tmp_dir, ignore_errors=True)
|
| 78 |
|
| 79 |
|
| 80 |
-
# π
|
| 81 |
-
with gr.Blocks(title="GitHub β Hugging Face Space
|
| 82 |
-
gr.Markdown("## π
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
placeholder="https://github.com/username/repo"
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
target_space = gr.Textbox(
|
| 90 |
-
label="Target Hugging Face Space",
|
| 91 |
-
placeholder="username/space-name"
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
commit_msg = gr.Textbox(
|
| 95 |
-
label="Commit Message",
|
| 96 |
-
value="Sync from GitHub"
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
output = gr.Textbox(label="Status")
|
| 100 |
-
|
| 101 |
-
btn = gr.Button("Clone & Push")
|
| 102 |
-
|
| 103 |
-
btn.click(
|
| 104 |
-
clone_and_push,
|
| 105 |
-
inputs=[github_repo, target_space, commit_msg],
|
| 106 |
-
outputs=output
|
| 107 |
-
)
|
| 108 |
|
| 109 |
demo.launch()
|
|
|
|
| 2 |
import shutil
|
| 3 |
import tempfile
|
| 4 |
from git import Repo
|
| 5 |
+
from huggingface_hub import upload_folder
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
+
GITHUB_REPO = "https://github.com/JawadAliAI/Cybersecurity-ProjectS.git"
|
| 11 |
+
TARGET_SPACE = "Muhammadidrees/cyberpunk"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def clone_and_store():
|
| 15 |
if not HF_TOKEN:
|
| 16 |
+
return "β HF_TOKEN not found. Add it in Space β Settings β Secrets."
|
| 17 |
|
| 18 |
tmp_dir = tempfile.mkdtemp()
|
| 19 |
|
| 20 |
try:
|
|
|
|
| 21 |
repo_path = os.path.join(tmp_dir, "github_repo")
|
|
|
|
| 22 |
|
| 23 |
+
# 1οΈβ£ Clone GitHub repository
|
| 24 |
+
Repo.clone_from(GITHUB_REPO, repo_path)
|
| 25 |
+
|
| 26 |
+
# 2οΈβ£ Remove git metadata
|
| 27 |
+
shutil.rmtree(os.path.join(repo_path, ".git"), ignore_errors=True)
|
| 28 |
+
|
| 29 |
+
# 3οΈβ£ Upload contents to Hugging Face Space
|
| 30 |
+
upload_folder(
|
| 31 |
+
folder_path=repo_path,
|
| 32 |
+
repo_id=TARGET_SPACE,
|
| 33 |
+
repo_type="space",
|
| 34 |
+
token=HF_TOKEN,
|
| 35 |
+
commit_message="Deploy Cybersecurity-ProjectS from GitHub"
|
| 36 |
)
|
| 37 |
|
| 38 |
+
return "β
GitHub repository successfully stored in Hugging Face Space!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
except Exception as e:
|
| 41 |
return f"β Error: {str(e)}"
|
|
|
|
| 44 |
shutil.rmtree(tmp_dir, ignore_errors=True)
|
| 45 |
|
| 46 |
|
| 47 |
+
# π Minimal UI
|
| 48 |
+
with gr.Blocks(title="GitHub β Hugging Face Space Deployer") as demo:
|
| 49 |
+
gr.Markdown("## π Deploy Cybersecurity Project to Hugging Face Space")
|
| 50 |
+
status = gr.Textbox(label="Status", lines=3)
|
| 51 |
+
deploy_btn = gr.Button("Clone & Store")
|
| 52 |
+
deploy_btn.click(clone_and_store, outputs=status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
demo.launch()
|