Muhammadidrees commited on
Commit
f9ce145
Β·
verified Β·
1 Parent(s): 95a33d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -80
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 HfApi
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 set in Space secrets."
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
- # 2️⃣ Clone Hugging Face Space
32
- space_path = os.path.join(tmp_dir, "hf_space")
33
- hf_repo_url = target_space
34
- Repo.clone_from(
35
- hf_repo_url,
36
- space_path,
37
- env={"GIT_ASKPASS": "echo", "GIT_USERNAME": "hf"}
 
 
 
 
 
 
38
  )
39
 
40
- # 3️⃣ Clear existing Space files (except .git)
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
- # πŸŽ› Gradio UI
81
- with gr.Blocks(title="GitHub β†’ Hugging Face Space Sync") as demo:
82
- gr.Markdown("## πŸš€ GitHub to Hugging Face Space Deployer")
83
-
84
- github_repo = gr.Textbox(
85
- label="GitHub Repository URL",
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()