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()