Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,67 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import tempfile
         | 
| 3 | 
            +
            import shutil
         | 
| 4 | 
            +
            from git import Repo
         | 
| 5 | 
            +
            from github import Github
         | 
| 6 | 
            +
            import os
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            def clone_and_push(hf_username, hf_space_name, github_repo_name, github_token):
         | 
| 9 | 
            +
                tmp_dir = None
         | 
| 10 | 
            +
                try:
         | 
| 11 | 
            +
                    # Prepare the git URL for the Hugging Face Spaces repository
         | 
| 12 | 
            +
                    hf_repo_url = f"https://huggingface.co/spaces/{hf_username}/{hf_space_name}.git"
         | 
| 13 | 
            +
                    
         | 
| 14 | 
            +
                    # Create a temporary directory to clone the repository
         | 
| 15 | 
            +
                    tmp_dir = tempfile.mkdtemp()
         | 
| 16 | 
            +
                    
         | 
| 17 | 
            +
                    # Clone the Hugging Face Spaces repository
         | 
| 18 | 
            +
                    repo = Repo.clone_from(hf_repo_url, tmp_dir)
         | 
| 19 | 
            +
                    
         | 
| 20 | 
            +
                    # Authenticate with GitHub using the provided token
         | 
| 21 | 
            +
                    g = Github(github_token)
         | 
| 22 | 
            +
                    user = g.get_user()
         | 
| 23 | 
            +
                    
         | 
| 24 | 
            +
                    # Create a new GitHub repository
         | 
| 25 | 
            +
                    github_repo = user.create_repo(github_repo_name)
         | 
| 26 | 
            +
                    
         | 
| 27 | 
            +
                    # Remove the existing 'origin' remote pointing to Hugging Face
         | 
| 28 | 
            +
                    repo.delete_remote(repo.remotes.origin)
         | 
| 29 | 
            +
                    
         | 
| 30 | 
            +
                    # Construct the authenticated GitHub URL
         | 
| 31 | 
            +
                    github_username = user.login
         | 
| 32 | 
            +
                    github_remote_url = f"https://{github_username}:{github_token}@github.com/{github_username}/{github_repo_name}.git"
         | 
| 33 | 
            +
                    
         | 
| 34 | 
            +
                    # Add the new 'origin' remote pointing to GitHub
         | 
| 35 | 
            +
                    repo.create_remote('origin', github_remote_url)
         | 
| 36 | 
            +
                    
         | 
| 37 | 
            +
                    # Push the code to the new GitHub repository
         | 
| 38 | 
            +
                    repo.git.push('origin', 'HEAD:refs/heads/main', set_upstream=True)
         | 
| 39 | 
            +
                    
         | 
| 40 | 
            +
                    return f"Successfully pushed to GitHub repository: {github_repo.html_url}"
         | 
| 41 | 
            +
                except Exception as e:
         | 
| 42 | 
            +
                    return f"An error occurred: {str(e)}"
         | 
| 43 | 
            +
                finally:
         | 
| 44 | 
            +
                    # Clean up the temporary directory
         | 
| 45 | 
            +
                    if tmp_dir and os.path.exists(tmp_dir):
         | 
| 46 | 
            +
                        shutil.rmtree(tmp_dir)
         | 
| 47 | 
            +
             | 
| 48 | 
            +
            iface = gr.Interface(
         | 
| 49 | 
            +
                theme="Nymbo/Nymbo_Theme",
         | 
| 50 | 
            +
                fn=clone_and_push,
         | 
| 51 | 
            +
                inputs=[
         | 
| 52 | 
            +
                    gr.Textbox(label="Hugging Face Username", placeholder="e.g., john_doe"),
         | 
| 53 | 
            +
                    gr.Textbox(label="Hugging Face Spaces Name", placeholder="e.g., my-space"),
         | 
| 54 | 
            +
                    gr.Textbox(label="GitHub Repository Name", placeholder="e.g., my-new-repo"),
         | 
| 55 | 
            +
                    gr.Textbox(label="GitHub Personal Access Token", type="password"),
         | 
| 56 | 
            +
                ],
         | 
| 57 | 
            +
                outputs="text",
         | 
| 58 | 
            +
                title="Hugging Face Spaces to GitHub Repo",
         | 
| 59 | 
            +
                description="""
         | 
| 60 | 
            +
                Enter the details to clone a Hugging Face Spaces repository and push it to a new GitHub repository.
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                **Note:** Your GitHub personal access token must have permissions to create repositories and push code (usually the 'repo' scope).
         | 
| 63 | 
            +
                """,
         | 
| 64 | 
            +
            )
         | 
| 65 | 
            +
             | 
| 66 | 
            +
            if __name__ == "__main__":
         | 
| 67 | 
            +
                iface.launch()
         | 
