Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -747,6 +747,114 @@ Deployed from: {repo_git}
|
|
| 747 |
shutil.rmtree(folder)
|
| 748 |
yield f"โ Error: {str(e)}"
|
| 749 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
css = """
|
| 751 |
.container {
|
| 752 |
max-width: 900px;
|
|
@@ -761,57 +869,127 @@ css = """
|
|
| 761 |
font-size: 14px;
|
| 762 |
line-height: 1.5;
|
| 763 |
}
|
|
|
|
|
|
|
|
|
|
| 764 |
"""
|
| 765 |
|
| 766 |
-
with gr.Blocks(css=css) as demo:
|
| 767 |
-
gr.Markdown("#
|
| 768 |
|
| 769 |
-
|
| 770 |
-
gr.
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
|
| 783 |
-
|
| 784 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 797 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 798 |
|
| 799 |
-
|
|
|
|
| 800 |
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
)
|
| 809 |
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 815 |
|
| 816 |
if __name__ == "__main__":
|
| 817 |
demo.launch()
|
|
|
|
| 747 |
shutil.rmtree(folder)
|
| 748 |
yield f"โ Error: {str(e)}"
|
| 749 |
|
| 750 |
+
def space_to_github(hf_username, hf_space_name, github_repo_name, github_token):
|
| 751 |
+
"""Clone HuggingFace Space and push to GitHub"""
|
| 752 |
+
tmp_dir = None
|
| 753 |
+
try:
|
| 754 |
+
# HF Space URL ์์ฑ
|
| 755 |
+
hf_repo_url = f"https://huggingface.co/spaces/{hf_username}/{hf_space_name}.git"
|
| 756 |
+
|
| 757 |
+
# ์์ ๋๋ ํ ๋ฆฌ ์์ฑ ๋ฐ ํด๋ก
|
| 758 |
+
tmp_dir = tempfile.mkdtemp()
|
| 759 |
+
yield f"๐ฅ Cloning HF Space: {hf_username}/{hf_space_name}..."
|
| 760 |
+
|
| 761 |
+
# Git ํด๋ก
|
| 762 |
+
env = os.environ.copy()
|
| 763 |
+
env['GIT_LFS_SKIP_SMUDGE'] = '1' # LFS ํ์ผ ์คํต
|
| 764 |
+
|
| 765 |
+
clone_cmd = ['git', 'clone', hf_repo_url, tmp_dir]
|
| 766 |
+
result = subprocess.run(clone_cmd, env=env, capture_output=True, text=True)
|
| 767 |
+
|
| 768 |
+
if result.returncode != 0:
|
| 769 |
+
raise Exception(f"Clone failed: {result.stderr}")
|
| 770 |
+
|
| 771 |
+
yield "โ
HF Space cloned successfully"
|
| 772 |
+
|
| 773 |
+
# GitHub ์ธ์ฆ ๋ฐ ๋ ํฌ ์์ฑ
|
| 774 |
+
yield "๐ Authenticating with GitHub..."
|
| 775 |
+
|
| 776 |
+
# GitHub API ํธ์ถ๋ก ๋ ํฌ ์์ฑ
|
| 777 |
+
headers = {
|
| 778 |
+
"Authorization": f"token {github_token}",
|
| 779 |
+
"Accept": "application/vnd.github.v3+json"
|
| 780 |
+
}
|
| 781 |
+
|
| 782 |
+
# ์ฌ์ฉ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
| 783 |
+
user_response = requests.get("https://api.github.com/user", headers=headers)
|
| 784 |
+
if user_response.status_code != 200:
|
| 785 |
+
raise Exception("GitHub authentication failed")
|
| 786 |
+
|
| 787 |
+
github_username = user_response.json()["login"]
|
| 788 |
+
yield f"โ
Authenticated as GitHub user: {github_username}"
|
| 789 |
+
|
| 790 |
+
# ๋ ํฌ์งํ ๋ฆฌ ์์ฑ
|
| 791 |
+
yield f"๐ฆ Creating GitHub repository: {github_repo_name}..."
|
| 792 |
+
create_data = {
|
| 793 |
+
"name": github_repo_name,
|
| 794 |
+
"private": False,
|
| 795 |
+
"auto_init": False
|
| 796 |
+
}
|
| 797 |
+
|
| 798 |
+
create_response = requests.post(
|
| 799 |
+
"https://api.github.com/user/repos",
|
| 800 |
+
headers=headers,
|
| 801 |
+
json=create_data
|
| 802 |
+
)
|
| 803 |
+
|
| 804 |
+
if create_response.status_code == 201:
|
| 805 |
+
repo_info = create_response.json()
|
| 806 |
+
yield f"โ
GitHub repository created: {repo_info['html_url']}"
|
| 807 |
+
elif create_response.status_code == 422:
|
| 808 |
+
# ๋ ํฌ๊ฐ ์ด๋ฏธ ์กด์ฌํจ
|
| 809 |
+
repo_info = {"html_url": f"https://github.com/{github_username}/{github_repo_name}"}
|
| 810 |
+
yield f"โน๏ธ Repository already exists: {repo_info['html_url']}"
|
| 811 |
+
else:
|
| 812 |
+
raise Exception(f"Failed to create repository: {create_response.json().get('message', 'Unknown error')}")
|
| 813 |
+
|
| 814 |
+
# Git remote ๋ณ๊ฒฝ ๋ฐ ํธ์
|
| 815 |
+
yield "๐ค Pushing to GitHub..."
|
| 816 |
+
|
| 817 |
+
# git ๋ช
๋ น์ด๋ก remote ๋ณ๊ฒฝ ๋ฐ ํธ์
|
| 818 |
+
os.chdir(tmp_dir)
|
| 819 |
+
|
| 820 |
+
# ๊ธฐ์กด origin ์ ๊ฑฐ
|
| 821 |
+
subprocess.run(['git', 'remote', 'remove', 'origin'], capture_output=True)
|
| 822 |
+
|
| 823 |
+
# GitHub remote ์ถ๊ฐ
|
| 824 |
+
github_remote_url = f"https://{github_username}:{github_token}@github.com/{github_username}/{github_repo_name}.git"
|
| 825 |
+
subprocess.run(['git', 'remote', 'add', 'origin', github_remote_url], check=True)
|
| 826 |
+
|
| 827 |
+
# main ๋ธ๋์น๋ก ํธ์
|
| 828 |
+
push_result = subprocess.run(
|
| 829 |
+
['git', 'push', '-u', 'origin', 'HEAD:main'],
|
| 830 |
+
capture_output=True,
|
| 831 |
+
text=True
|
| 832 |
+
)
|
| 833 |
+
|
| 834 |
+
if push_result.returncode != 0:
|
| 835 |
+
# master ๋ธ๋์น๋ก ์ฌ์๋
|
| 836 |
+
push_result = subprocess.run(
|
| 837 |
+
['git', 'push', '-u', 'origin', 'HEAD:master'],
|
| 838 |
+
capture_output=True,
|
| 839 |
+
text=True
|
| 840 |
+
)
|
| 841 |
+
|
| 842 |
+
if push_result.returncode != 0:
|
| 843 |
+
raise Exception(f"Push failed: {push_result.stderr}")
|
| 844 |
+
|
| 845 |
+
yield "โ
Successfully pushed to GitHub!"
|
| 846 |
+
yield f"\n๐ GitHub Repository: https://github.com/{github_username}/{github_repo_name}"
|
| 847 |
+
yield f"\nโจ Clone command: `git clone https://github.com/{github_username}/{github_repo_name}.git`"
|
| 848 |
+
|
| 849 |
+
except Exception as e:
|
| 850 |
+
yield f"โ Error: {str(e)}"
|
| 851 |
+
finally:
|
| 852 |
+
# ์์ ๋๋ ํ ๋ฆฌ ์ ๋ฆฌ
|
| 853 |
+
if tmp_dir and os.path.exists(tmp_dir):
|
| 854 |
+
os.chdir("/") # ๋๋ ํ ๋ฆฌ ๋ฐ์ผ๋ก ์ด๋
|
| 855 |
+
shutil.rmtree(tmp_dir)
|
| 856 |
+
yield "๐งน Cleaned up temporary files"
|
| 857 |
+
|
| 858 |
css = """
|
| 859 |
.container {
|
| 860 |
max-width: 900px;
|
|
|
|
| 869 |
font-size: 14px;
|
| 870 |
line-height: 1.5;
|
| 871 |
}
|
| 872 |
+
.tab-content {
|
| 873 |
+
margin-top: 20px;
|
| 874 |
+
}
|
| 875 |
"""
|
| 876 |
|
| 877 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 878 |
+
gr.Markdown("# ๐ GitHub โ HuggingFace Space Converter")
|
| 879 |
|
| 880 |
+
with gr.Tabs():
|
| 881 |
+
with gr.Tab("GitHub โ HF Space"):
|
| 882 |
+
if not os.getenv("HF_TOKEN"):
|
| 883 |
+
gr.Markdown("โ HF_TOKEN Required - Set it in Space settings")
|
| 884 |
+
else:
|
| 885 |
+
gr.Markdown("โ
HF_TOKEN Found")
|
| 886 |
+
|
| 887 |
+
with gr.Row():
|
| 888 |
+
with gr.Column():
|
| 889 |
+
repo_git = gr.Textbox(
|
| 890 |
+
label="GitHub Repository URL",
|
| 891 |
+
placeholder="https://github.com/username/repository"
|
| 892 |
+
)
|
| 893 |
+
repo_hf = gr.Textbox(
|
| 894 |
+
label="Hugging Face Space Name",
|
| 895 |
+
placeholder="my-awesome-space"
|
| 896 |
+
)
|
| 897 |
+
sdk_choices = gr.Radio(
|
| 898 |
+
["gradio", "streamlit", "docker", "static"],
|
| 899 |
+
label="Space SDK",
|
| 900 |
+
value="gradio"
|
| 901 |
+
)
|
| 902 |
+
skip_lfs = gr.Checkbox(
|
| 903 |
+
label="Skip Git LFS files",
|
| 904 |
+
value=True
|
| 905 |
+
)
|
| 906 |
+
enable_smart_generation = gr.Checkbox(
|
| 907 |
+
label="๐ค Enable Smart app.py Generation",
|
| 908 |
+
value=False,
|
| 909 |
+
info="Requires FIREWORKS_API_KEY in environment variables"
|
| 910 |
+
)
|
| 911 |
+
|
| 912 |
+
btn_to_hf = gr.Button("๐ Deploy to HF Space", variant="primary")
|
| 913 |
+
|
| 914 |
+
with gr.Column():
|
| 915 |
+
output_to_hf = gr.Textbox(
|
| 916 |
+
label="Progress",
|
| 917 |
+
lines=15,
|
| 918 |
+
elem_classes=["output-box"],
|
| 919 |
+
interactive=False,
|
| 920 |
+
show_copy_button=True
|
| 921 |
+
)
|
| 922 |
+
|
| 923 |
+
btn_to_hf.click(
|
| 924 |
+
fn=clone,
|
| 925 |
+
inputs=[repo_git, repo_hf, sdk_choices, skip_lfs, enable_smart_generation],
|
| 926 |
+
outputs=output_to_hf
|
| 927 |
)
|
| 928 |
+
|
| 929 |
+
with gr.Tab("HF Space โ GitHub"):
|
| 930 |
+
gr.Markdown("""
|
| 931 |
+
๐ **Export your HuggingFace Space to GitHub**
|
| 932 |
|
| 933 |
+
Create a GitHub repository from your existing HuggingFace Space.
|
| 934 |
+
""")
|
| 935 |
|
| 936 |
+
with gr.Row():
|
| 937 |
+
with gr.Column():
|
| 938 |
+
hf_username = gr.Textbox(
|
| 939 |
+
label="HuggingFace Username",
|
| 940 |
+
placeholder="e.g., john_doe"
|
| 941 |
+
)
|
| 942 |
+
hf_space_name = gr.Textbox(
|
| 943 |
+
label="HuggingFace Space Name",
|
| 944 |
+
placeholder="e.g., my-space"
|
| 945 |
+
)
|
| 946 |
+
github_repo_name = gr.Textbox(
|
| 947 |
+
label="GitHub Repository Name",
|
| 948 |
+
placeholder="e.g., my-new-repo"
|
| 949 |
+
)
|
| 950 |
+
github_token = gr.Textbox(
|
| 951 |
+
label="GitHub Personal Access Token",
|
| 952 |
+
type="password",
|
| 953 |
+
placeholder="ghp_xxxxxxxxxxxx",
|
| 954 |
+
info="Token needs 'repo' scope. Get it from: https://github.com/settings/tokens"
|
| 955 |
+
)
|
| 956 |
+
|
| 957 |
+
btn_to_github = gr.Button("๐ค Export to GitHub", variant="primary")
|
| 958 |
+
|
| 959 |
+
with gr.Column():
|
| 960 |
+
output_to_github = gr.Textbox(
|
| 961 |
+
label="Progress",
|
| 962 |
+
lines=15,
|
| 963 |
+
elem_classes=["output-box"],
|
| 964 |
+
interactive=False,
|
| 965 |
+
show_copy_button=True
|
| 966 |
+
)
|
| 967 |
+
|
| 968 |
+
btn_to_github.click(
|
| 969 |
+
fn=space_to_github,
|
| 970 |
+
inputs=[hf_username, hf_space_name, github_repo_name, github_token],
|
| 971 |
+
outputs=output_to_github
|
| 972 |
)
|
| 973 |
|
| 974 |
+
gr.Markdown("""
|
| 975 |
+
---
|
| 976 |
+
### ๐ Instructions
|
| 977 |
+
|
| 978 |
+
**GitHub โ HF Space:**
|
| 979 |
+
- Clone any public GitHub repository to your HuggingFace account
|
| 980 |
+
- Automatically handles LFS issues
|
| 981 |
+
- Optional AI-powered app.py generation
|
| 982 |
+
|
| 983 |
+
**HF Space โ GitHub:**
|
| 984 |
+
- Export your HuggingFace Space to a new GitHub repository
|
| 985 |
+
- Requires GitHub Personal Access Token with 'repo' scope
|
| 986 |
+
- Preserves all files and structure
|
| 987 |
+
|
| 988 |
+
### ๐ Required Tokens
|
| 989 |
+
- **HF_TOKEN**: Set in Space settings for GitHub โ HF
|
| 990 |
+
- **GitHub Token**: Required for HF โ GitHub (create at [GitHub Settings](https://github.com/settings/tokens))
|
| 991 |
+
- **FIREWORKS_API_KEY**: Optional, for smart app generation
|
| 992 |
+
""")
|
| 993 |
|
| 994 |
if __name__ == "__main__":
|
| 995 |
demo.launch()
|