Muhammadidrees commited on
Commit
a07f228
·
verified ·
1 Parent(s): 86d91a8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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 = f"https://huggingface.co/spaces/{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)}"
75
+
76
+ finally:
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()