File size: 1,048 Bytes
2b55265
 
 
 
 
 
 
 
 
 
 
 
 
 
ec08817
2b55265
 
 
 
a41e2dc
2b55265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import logging
import requests
from fastapi import FastAPI

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("hf-keepalive")

app = FastAPI()

HF_TOKEN = os.getenv("HF_TOKEN")

# All private spaces
SPACES = [
    "https://jebin2-captioncreator.hf.space/run-command/create_publish?type=chess,pexels&id=onetime",
    "https://jebin2-comic-panel-extractor.hf.space/",
    "https://jebin2-paper.hf.space/",
    "https://jebin2-stt.hf.space/",
    "https://jebin2-tts.hf.space/",
    "https://jebin2-ttt.hf.space/",
]

TIMEOUT = 120


@app.get("/")
def home():
    return {"status": "alive"}


@app.get("/trigger")
def trigger_all():
    logger.info("Keepalive triggered. Calling all spaces...")

    headers = {"Authorization": f"Bearer {HF_TOKEN}"}

    for url in SPACES:
        try:
            logger.info(f"Calling {url}")
            requests.get(url, headers=headers, timeout=TIMEOUT)
        except Exception as e:
            logger.warning(f"Failed: {url} -> {e}")

    # No heavy response needed
    return "OK"