| 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"] |
|
|