Spaces:
Runtime error
Runtime error
Commit ·
cff28f2
1
Parent(s): 4fe89e2
changes
Browse files- .DS_Store +0 -0
- app.py +33 -0
- requirements.txt +2 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
from werkzeug.utils import secure_filename
|
| 3 |
from flask_cors import CORS # Import Flask-CORS
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import sys
|
|
|
|
| 6 |
|
| 7 |
print("Starting Flask app...", file=sys.stderr)
|
| 8 |
print("Loading model...", file=sys.stderr)
|
|
@@ -57,6 +60,36 @@ def upload_file():
|
|
| 57 |
def uploaded_file(filename):
|
| 58 |
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
if __name__ == '__main__':
|
| 61 |
port = int(os.environ.get('PORT', 5000))
|
| 62 |
app.run(host='0.0.0.0', port=port, debug=False)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, send_from_directory
|
| 2 |
from werkzeug.utils import secure_filename
|
| 3 |
from flask_cors import CORS # Import Flask-CORS
|
| 4 |
+
from apscheduler.schedulers.background import BackgroundScheduler
|
| 5 |
+
import requests
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
+
import atexit
|
| 9 |
|
| 10 |
print("Starting Flask app...", file=sys.stderr)
|
| 11 |
print("Loading model...", file=sys.stderr)
|
|
|
|
| 60 |
def uploaded_file(filename):
|
| 61 |
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
| 62 |
|
| 63 |
+
# ============ KEEP-ALIVE CRON JOB ============
|
| 64 |
+
# Self-ping to prevent Hugging Face from sleeping
|
| 65 |
+
|
| 66 |
+
def keep_alive():
|
| 67 |
+
"""Ping the health endpoint to keep the server awake"""
|
| 68 |
+
try:
|
| 69 |
+
# Get the URL from environment or use default
|
| 70 |
+
app_url = os.environ.get('SPACE_HOST', None)
|
| 71 |
+
if app_url:
|
| 72 |
+
url = f"https://{app_url}"
|
| 73 |
+
else:
|
| 74 |
+
url = os.environ.get('APP_URL', 'http://localhost:5000')
|
| 75 |
+
|
| 76 |
+
response = requests.get(f"{url}/", timeout=30)
|
| 77 |
+
print(f"[Keep-Alive] Pinged {url} - Status: {response.status_code}", file=sys.stderr)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"[Keep-Alive] Ping failed: {str(e)}", file=sys.stderr)
|
| 80 |
+
|
| 81 |
+
# Initialize scheduler
|
| 82 |
+
scheduler = BackgroundScheduler(daemon=True)
|
| 83 |
+
# Ping every 14 minutes (Hugging Face sleeps after 15 minutes of inactivity)
|
| 84 |
+
scheduler.add_job(keep_alive, 'interval', minutes=14)
|
| 85 |
+
scheduler.start()
|
| 86 |
+
|
| 87 |
+
# Shut down the scheduler when exiting the app
|
| 88 |
+
atexit.register(lambda: scheduler.shutdown())
|
| 89 |
+
|
| 90 |
+
print("Keep-alive cron job started (pinging every 14 minutes)", file=sys.stderr)
|
| 91 |
+
# ============================================
|
| 92 |
+
|
| 93 |
if __name__ == '__main__':
|
| 94 |
port = int(os.environ.get('PORT', 5000))
|
| 95 |
app.run(host='0.0.0.0', port=port, debug=False)
|
requirements.txt
CHANGED
|
@@ -18,6 +18,8 @@ Flask==3.0.0
|
|
| 18 |
flask-cors==4.0.0
|
| 19 |
Werkzeug==3.0.1
|
| 20 |
gunicorn==21.2.0
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Image handling
|
| 23 |
scikit-image==0.22.0
|
|
|
|
| 18 |
flask-cors==4.0.0
|
| 19 |
Werkzeug==3.0.1
|
| 20 |
gunicorn==21.2.0
|
| 21 |
+
APScheduler==3.10.4
|
| 22 |
+
requests==2.31.0
|
| 23 |
|
| 24 |
# Image handling
|
| 25 |
scikit-image==0.22.0
|