Spaces:
Sleeping
Sleeping
MatteoAldovardi commited on
Commit ·
5ade02b
1
Parent(s): 864bc6d
aa
Browse files- Dockerfile +2 -2
- main.py +13 -0
Dockerfile
CHANGED
|
@@ -17,10 +17,10 @@ RUN mkdir -p models
|
|
| 17 |
|
| 18 |
# Expose the port FastAPI will run on. Hugging Face Spaces often expects
|
| 19 |
# port 8000 for web apps.
|
| 20 |
-
EXPOSE
|
| 21 |
|
| 22 |
# Command to run the Uvicorn server
|
| 23 |
# The --host 0.0.0.0 makes the server accessible from outside the
|
| 24 |
# container
|
| 25 |
# The --port 7860 matches the EXPOSE instruction
|
| 26 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "
|
|
|
|
| 17 |
|
| 18 |
# Expose the port FastAPI will run on. Hugging Face Spaces often expects
|
| 19 |
# port 8000 for web apps.
|
| 20 |
+
EXPOSE 7860
|
| 21 |
|
| 22 |
# Command to run the Uvicorn server
|
| 23 |
# The --host 0.0.0.0 makes the server accessible from outside the
|
| 24 |
# container
|
| 25 |
# The --port 7860 matches the EXPOSE instruction
|
| 26 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
CHANGED
|
@@ -3,6 +3,9 @@ from pydantic import BaseModel
|
|
| 3 |
import os
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# --- 0. Initialize FastAPI app ---
|
| 8 |
app = FastAPI()
|
|
@@ -87,4 +90,14 @@ async def predict_inference(data: InferenceInput):
|
|
| 87 |
except Exception as e:
|
| 88 |
raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
# --- 5. Run the app ---
|
|
|
|
| 3 |
import os
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
+
import threading
|
| 7 |
+
import time
|
| 8 |
+
import requests
|
| 9 |
|
| 10 |
# --- 0. Initialize FastAPI app ---
|
| 11 |
app = FastAPI()
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
raise HTTPException(status_code=500, detail=f"Inference failed: {str(e)}")
|
| 92 |
|
| 93 |
+
def keep_alive():
|
| 94 |
+
while True:
|
| 95 |
+
try:
|
| 96 |
+
requests.get("http://localhost:7860/health")
|
| 97 |
+
except Exception:
|
| 98 |
+
pass
|
| 99 |
+
time.sleep(60) # Ping every 60 seconds
|
| 100 |
+
|
| 101 |
+
threading.Thread(target=keep_alive, daemon=True).start()
|
| 102 |
+
|
| 103 |
# --- 5. Run the app ---
|