File size: 4,021 Bytes
1d75522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""Update Hugging Face Space using git commands."""

import os
import subprocess
import logging
from pathlib import Path
import shutil
from huggingface_hub import HfApi
from dotenv import load_dotenv

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def run_command(command, cwd=None):
    """Run a shell command and log output."""
    try:
        result = subprocess.run(
            command,
            shell=True,
            check=True,
            text=True,
            capture_output=True,
            cwd=cwd
        )
        logger.info(f"Command output: {result.stdout}")
        return True
    except subprocess.CalledProcessError as e:
        logger.error(f"Command failed: {e.stderr}")
        return False

def update_space():
    """Update the Hugging Face Space using git commands."""
    try:
        # Load environment variables
        load_dotenv()
        token = os.getenv("HUGGINGFACE_TOKEN")
        if not token:
            raise ValueError("HUGGINGFACE_TOKEN not found in environment variables")

        # Space configuration
        SPACE_NAME = "nananie143/agentic-system"
        REPO_URL = f"https://huggingface.co/spaces/{SPACE_NAME}"
        
        logger.info("Starting Space update process...")
        
        # 1. Initialize Hugging Face API
        api = HfApi(token=token)
        
        # 2. Create the Space if it doesn't exist
        logger.info("Creating/Checking Space...")
        try:
            api.create_repo(
                repo_id=SPACE_NAME,
                repo_type="space",
                space_sdk="gradio",
                private=False,
                exist_ok=True
            )
        except Exception as e:
            logger.warning(f"Note about Space creation: {e}")
        
        # 3. Set up the repository directory
        repo_dir = Path("space_repo")
        if repo_dir.exists():
            logger.info("Cleaning up existing repository...")
            shutil.rmtree(repo_dir)
        
        # 4. Clone the Space repository with token
        logger.info("Cloning Space repository...")
        clone_url = f"https://user:{token}@huggingface.co/spaces/{SPACE_NAME}"
        run_command(f"git clone {clone_url} {repo_dir}")
        
        # 5. Copy files to the repository
        logger.info("Copying files to repository...")
        files_to_copy = [
            "app.py",
            "agentic_system.py",
            "requirements.txt",
            "space.yml",
            "download_models_space.py",
            "app_space.sh",
            "reasoning",
            "orchestrator.py",
            "team_management.py",
            "meta_learning.py",
            "config.py"
        ]
        
        for file in files_to_copy:
            src = Path(file)
            dst = repo_dir / src.name
            if src.is_file():
                shutil.copy2(src, dst)
            elif src.is_dir():
                if dst.exists():
                    shutil.rmtree(dst)
                shutil.copytree(src, dst)
        
        # 6. Configure git
        logger.info("Configuring git...")
        run_command('git config user.email "[email protected]"', cwd=repo_dir)
        run_command('git config user.name "Cascade Bot"', cwd=repo_dir)
        
        # 7. Add and commit changes
        logger.info("Committing changes...")
        run_command("git add .", cwd=repo_dir)
        run_command('git commit -m "Update Space with latest changes and model configurations"', cwd=repo_dir)
        
        # 8. Push changes
        logger.info("Pushing changes to Space...")
        run_command("git push", cwd=repo_dir)
        
        # 9. Clean up
        logger.info("Cleaning up...")
        shutil.rmtree(repo_dir)
        
        logger.info(f"Space updated successfully! Visit: {REPO_URL}")
        return True
        
    except Exception as e:
        logger.error(f"Error updating Space: {e}")
        return False

if __name__ == "__main__":
    update_space()