File size: 1,202 Bytes
0e6522f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import logging

from huggingface_hub import HfApi

from app.config.base import HF_TOKEN

logger = logging.getLogger(__name__)

ACTIVE_SPACE_STATES = ["APP_STARTING", "RUNNING", "BUILDING", "RUNNING_APP_STARTING"]


class SpaceController:
    def __init__(self):
        self.token = HF_TOKEN
        self.api = HfApi(token=self.token)

    def get_space_status(self, space_id):
        """Get the current status of a space"""
        try:
            space_info = self.api.space_info(repo_id=space_id)
            return space_info.runtime.stage
        except Exception as e:
            logger.error(f"Error getting space status: {e}")
            return None

    def start_space(self, space_id):
        """Start a paused space"""
        try:
            current_status = self.get_space_status(space_id)

            if current_status in ACTIVE_SPACE_STATES:
                logger.info(f"Space {space_id} is already active with status: {current_status}")
            else:
                logger.info(f"Starting space {space_id}...")
                self.api.restart_space(repo_id=space_id)

        except Exception as e:
            logger.error(f"Error starting space: {e}")
            raise