jebin2 commited on
Commit
2b55265
·
1 Parent(s): ee8f71d

implement a keep-alive service to periodically trigger Hugging Face Spaces.

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. app.py +44 -0
  3. requirements.txt +3 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ ENV PYTHONUNBUFFERED=1
4
+ WORKDIR /app
5
+
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ COPY app.py .
10
+
11
+ EXPOSE 7860
12
+
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import logging
3
+ import requests
4
+ from fastapi import FastAPI
5
+
6
+ logging.basicConfig(level=logging.INFO)
7
+ logger = logging.getLogger("hf-keepalive")
8
+
9
+ app = FastAPI()
10
+
11
+ HF_TOKEN = os.getenv("HF_TOKEN")
12
+
13
+ # All private spaces
14
+ SPACES = [
15
+ "https://jebin2-captioncreator.hf.space/run-command/create_publish?type=pexels&id=",
16
+ "https://jebin2-comic-panel-extractor.hf.space/",
17
+ "https://jebin2-paper.hf.space/",
18
+ "https://jebin2-stt.hf.space/",
19
+ "https://jebin2-tts.hf.space/",
20
+ ]
21
+
22
+ TIMEOUT = 120
23
+
24
+
25
+ @app.get("/")
26
+ def home():
27
+ return {"status": "alive"}
28
+
29
+
30
+ @app.get("/trigger")
31
+ def trigger_all():
32
+ logger.info("Keepalive triggered. Calling all spaces...")
33
+
34
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
35
+
36
+ for url in SPACES:
37
+ try:
38
+ logger.info(f"Calling {url}")
39
+ requests.get(url, headers=headers, timeout=TIMEOUT)
40
+ except Exception as e:
41
+ logger.warning(f"Failed: {url} -> {e}")
42
+
43
+ # No heavy response needed
44
+ return "OK"
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ requests