Optimize for stability: 20min timeout + Granular loading
Browse files- Dockerfile +1 -1
- server.py +13 -7
Dockerfile
CHANGED
|
@@ -30,4 +30,4 @@ EXPOSE 7860
|
|
| 30 |
# Run Gunicorn to serve the Flask app
|
| 31 |
# -b 0.0.0.0:7860 binds to all interfaces on port 7860
|
| 32 |
# server:app refers to server.py and the 'app' object inside it
|
| 33 |
-
CMD ["gunicorn", "-b", "0.0.0.0:7860", "server:app", "--timeout", "
|
|
|
|
| 30 |
# Run Gunicorn to serve the Flask app
|
| 31 |
# -b 0.0.0.0:7860 binds to all interfaces on port 7860
|
| 32 |
# server:app refers to server.py and the 'app' object inside it
|
| 33 |
+
CMD ["gunicorn", "-b", "0.0.0.0:7860", "server:app", "--timeout", "1200"]
|
server.py
CHANGED
|
@@ -60,15 +60,18 @@ def download_artifacts():
|
|
| 60 |
token=token
|
| 61 |
)
|
| 62 |
print(f"β
Downloaded {filename}", flush=True)
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
print(f"β CRITICAL ERROR: Failed to download {filename}", flush=True)
|
| 65 |
print(f"Error details: {e}", flush=True)
|
| 66 |
print("HINT: Is the Dataset Name correct? Is it Private? (If private, the Space needs a token)", flush=True)
|
| 67 |
else:
|
| 68 |
print(f"β
Found existing {filename} in {base_dir}", flush=True)
|
|
|
|
| 69 |
|
| 70 |
# --- ASYNC DOWNLOAD SETUP ---
|
| 71 |
-
|
|
|
|
| 72 |
DATA_ERROR = None
|
| 73 |
|
| 74 |
def download_worker():
|
|
@@ -81,13 +84,16 @@ def download_worker():
|
|
| 81 |
for f in FILES_TO_DOWNLOAD:
|
| 82 |
if not os.path.exists(f):
|
| 83 |
missing.append(f)
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
if missing:
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
else:
|
| 89 |
print("β
DATA READY: All files downloaded and verified.", flush=True)
|
| 90 |
-
DATA_READY = True
|
| 91 |
|
| 92 |
except Exception as e:
|
| 93 |
DATA_ERROR = f"Download crashed: {e}"
|
|
@@ -102,15 +108,15 @@ def home():
|
|
| 102 |
@app.route("/health")
|
| 103 |
def health():
|
| 104 |
if DATA_ERROR: return jsonify({"status": "error", "details": DATA_ERROR})
|
| 105 |
-
return jsonify({"status": "ready" if
|
| 106 |
|
| 107 |
@app.route("/search", methods=["POST"])
|
| 108 |
def search_api():
|
| 109 |
if DATA_ERROR:
|
| 110 |
return jsonify({"error": f"Server Error: {DATA_ERROR}. Check Logs."}), 500
|
| 111 |
|
| 112 |
-
if not
|
| 113 |
-
return jsonify({"error": "System is
|
| 114 |
|
| 115 |
data = request.json
|
| 116 |
query = data.get("query", "")
|
|
|
|
| 60 |
token=token
|
| 61 |
)
|
| 62 |
print(f"β
Downloaded {filename}", flush=True)
|
| 63 |
+
SHARDS_READY[filename] = True
|
| 64 |
except Exception as e:
|
| 65 |
print(f"β CRITICAL ERROR: Failed to download {filename}", flush=True)
|
| 66 |
print(f"Error details: {e}", flush=True)
|
| 67 |
print("HINT: Is the Dataset Name correct? Is it Private? (If private, the Space needs a token)", flush=True)
|
| 68 |
else:
|
| 69 |
print(f"β
Found existing {filename} in {base_dir}", flush=True)
|
| 70 |
+
SHARDS_READY[filename] = True
|
| 71 |
|
| 72 |
# --- ASYNC DOWNLOAD SETUP ---
|
| 73 |
+
# Granular status tracking: {filename: bool}
|
| 74 |
+
SHARDS_READY = {}
|
| 75 |
DATA_ERROR = None
|
| 76 |
|
| 77 |
def download_worker():
|
|
|
|
| 84 |
for f in FILES_TO_DOWNLOAD:
|
| 85 |
if not os.path.exists(f):
|
| 86 |
missing.append(f)
|
| 87 |
+
else:
|
| 88 |
+
# Mark this file as ready immediately
|
| 89 |
+
SHARDS_READY[f] = True
|
| 90 |
|
| 91 |
if missing:
|
| 92 |
+
# Just log error, don't crash full app status if some files work?
|
| 93 |
+
# But for now, let's keep it simple: if any fail, we have a partial state.
|
| 94 |
+
pass
|
| 95 |
else:
|
| 96 |
print("β
DATA READY: All files downloaded and verified.", flush=True)
|
|
|
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
DATA_ERROR = f"Download crashed: {e}"
|
|
|
|
| 108 |
@app.route("/health")
|
| 109 |
def health():
|
| 110 |
if DATA_ERROR: return jsonify({"status": "error", "details": DATA_ERROR})
|
| 111 |
+
return jsonify({"status": "ready" if len(SHARDS_READY) > 0 else "loading", "shards": SHARDS_READY})
|
| 112 |
|
| 113 |
@app.route("/search", methods=["POST"])
|
| 114 |
def search_api():
|
| 115 |
if DATA_ERROR:
|
| 116 |
return jsonify({"error": f"Server Error: {DATA_ERROR}. Check Logs."}), 500
|
| 117 |
|
| 118 |
+
if not SHARDS_READY:
|
| 119 |
+
return jsonify({"error": "System is initializing. No data available yet. Please wait."}), 503
|
| 120 |
|
| 121 |
data = request.json
|
| 122 |
query = data.get("query", "")
|