File size: 1,993 Bytes
7b4e4e5
 
f00d49e
7b4e4e5
 
f00d49e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b4e4e5
 
 
 
 
 
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
FROM python:3.9-slim

# Install dependencies
RUN pip install --no-cache-dir fastapi uvicorn

# Create the app.py file using a Heredoc to avoid shell syntax errors
RUN cat <<EOF > app.py
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
import time

app = FastAPI()

@app.get("/ping")
async def ping():
    return {"status": "pong"}

@app.get("/", response_class=HTMLResponse)
async def index():
    return """
    <!DOCTYPE html>
    <html>
        <head>
            <title>HF Space Ping</title>
            <style>
                body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background: #f4f4f9; }
                .card { background: white; padding: 2rem; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); text-align: center; }
                #ping-value { font-size: 3rem; font-weight: bold; color: #4a90e2; }
                .unit { font-size: 1.2rem; color: #666; }
            </style>
        </head>
        <body>
            <div class="card">
                <h2>Your Latency to Space</h2>
                <div id="ping-value">0</div>
                <div class="unit">ms</div>
            </div>
            <script>
                async function measurePing() {
                    const start = performance.now();
                    try {
                        await fetch('/ping');
                        const end = performance.now();
                        document.getElementById('ping-value').innerText = Math.round(end - start);
                    } catch (e) {
                        document.getElementById('ping-value').innerText = 'Error';
                    }
                }
                setInterval(measurePing, 1000);
                measurePing();
            </script>
        </body>
    </html>
    """
EOF

# Hugging Face Spaces use port 7860
EXPOSE 7860

# Run the server
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]