File size: 4,703 Bytes
dcb2a99
1a7fda5
daade8b
 
 
 
 
 
 
 
 
 
1a7fda5
daade8b
 
 
1a7fda5
daade8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dcb2a99
daade8b
 
 
 
 
 
 
 
 
 
 
 
 
dcb2a99
daade8b
 
 
 
 
 
 
 
dcb2a99
daade8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7fda5
daade8b
 
 
 
dcb2a99
daade8b
 
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import sys
import time
import subprocess
from pathlib import Path
import logging
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from typing import Optional
from dotenv import load_dotenv
from huggingface_hub import HfApi, create_repo

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

def setup_requests_session(
    retries: int = 5,
    backoff_factor: float = 1.0,
    status_forcelist: Optional[list] = None
) -> requests.Session:
    """Configure requests session with retries."""
    if status_forcelist is None:
        status_forcelist = [408, 429, 500, 502, 503, 504]
    
    session = requests.Session()
    retry = Retry(
        total=retries,
        read=retries,
        connect=retries,
        backoff_factor=backoff_factor,
        status_forcelist=status_forcelist,
    )
    adapter = HTTPAdapter(max_retries=retry)
    session.mount('http://', adapter)
    session.mount('https://', adapter)
    return session

def check_network_connectivity(host: str = "8.8.8.8", timeout: int = 5) -> bool:
    """Check if network is accessible."""
    try:
        # Try DNS resolution first
        subprocess.run(
            ["ping", "-c", "1", "-W", str(timeout), host],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True
        )
        return True
    except subprocess.CalledProcessError:
        return False

def check_huggingface_connectivity(timeout: int = 5) -> bool:
    """Check if Hugging Face is accessible."""
    session = setup_requests_session()
    try:
        response = session.get("https://huggingface.co", timeout=timeout)
        return response.status_code == 200
    except:
        return False

def wait_for_network(
    max_attempts: int = 5,
    delay: int = 10,
    hosts: Optional[list] = None
) -> bool:
    """Wait for network connectivity."""
    if hosts is None:
        hosts = ["8.8.8.8", "1.1.1.1"]
    
    for attempt in range(max_attempts):
        logger.info(f"Checking network connectivity (attempt {attempt + 1}/{max_attempts})")
        
        # Try different DNS servers
        for host in hosts:
            if check_network_connectivity(host):
                logger.info(f"Network connectivity established via {host}")
                return True
                
        # Check Hugging Face specifically
        if check_huggingface_connectivity():
            logger.info("Hugging Face is accessible")
            return True
            
        if attempt < max_attempts - 1:
            logger.warning(f"Network check failed. Waiting {delay} seconds before retry...")
            time.sleep(delay)
    
    return False

def upload_to_huggingface():
    """Upload the project to Hugging Face."""
    creds_path = None
    try:
        # Load environment variables
        load_dotenv()
        token = os.getenv("HUGGINGFACE_TOKEN")
        if not token:
            raise ValueError("HUGGINGFACE_TOKEN not found in environment variables")
        
        # Check network connectivity
        if not wait_for_network():
            raise ConnectionError("Unable to establish network connectivity")
        
        # Initialize Hugging Face API
        api = HfApi(token=token)
        
        logger.info("Starting upload to Hugging Face Space...")
        
        # Space configuration
        space_name = "nananie143/advanced-reasoning"
        
        try:
            # Try to create the space (will fail if it exists)
            create_repo(
                repo_id=space_name,
                token=token,
                repo_type="space",
                space_sdk="gradio",
                private=False
            )
            logger.info("Created new Hugging Face Space")
        except Exception as e:
            if "409" in str(e):  # HTTP 409 Conflict - repo already exists
                logger.info("Space already exists, continuing with upload...")
            else:
                raise
        
        # Upload files
        logger.info("Uploading files to space...")
        api.upload_folder(
            folder_path=".",
            repo_id=space_name,
            repo_type="space",
            ignore_patterns=["*.pyc", "__pycache__", ".git", ".env", "*.gguf"]
        )
        
        logger.info("Upload completed successfully!")
        
    except Exception as e:
        logger.error(f"Upload failed: {str(e)}")
        sys.exit(1)
    finally:
        # Clean up credentials if they exist
        if creds_path and os.path.exists(creds_path):
            os.unlink(creds_path)

if __name__ == "__main__":
    upload_to_huggingface()