Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -24,7 +24,7 @@ app = Flask(__name__)
|
|
| 24 |
app.secret_key = os.getenv("FLASK_SECRET", "changeme123")
|
| 25 |
limiter = Limiter(get_remote_address, app=app)
|
| 26 |
|
| 27 |
-
# --- Memory store for OTPs
|
| 28 |
otp_store = {}
|
| 29 |
|
| 30 |
# ---------------- ROUTES ---------------- #
|
|
@@ -40,10 +40,16 @@ def admin():
|
|
| 40 |
pwd = request.form.get("password")
|
| 41 |
if user == ADMIN_USER and pwd == ADMIN_PASS:
|
| 42 |
session["logged_in"] = True
|
| 43 |
-
return redirect(url_for("
|
| 44 |
flash("Invalid credentials", "error")
|
| 45 |
return render_template("login.html")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
@app.route("/generate_otp")
|
| 48 |
def generate_otp():
|
| 49 |
if not session.get("logged_in"):
|
|
@@ -53,11 +59,15 @@ def generate_otp():
|
|
| 53 |
expiry = datetime.utcnow() + timedelta(minutes=OTP_EXPIRY_MINUTES)
|
| 54 |
otp_store[otp] = {"expiry": expiry, "used": False}
|
| 55 |
|
| 56 |
-
# Save to dataset
|
| 57 |
save_otp_to_dataset(otp, expiry.strftime("%Y-%m-%d %H:%M:%S UTC"))
|
| 58 |
|
| 59 |
return render_template("otp_result.html", otp=otp, expiry=expiry.strftime("%Y-%m-%d %H:%M UTC"))
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
# ---------------- HELPERS ---------------- #
|
| 62 |
|
| 63 |
def save_otp_to_dataset(otp: str, expiry: str):
|
|
@@ -65,32 +75,24 @@ def save_otp_to_dataset(otp: str, expiry: str):
|
|
| 65 |
if not HF_TOKEN:
|
| 66 |
print("❌ HF_TOKEN not set.")
|
| 67 |
return
|
| 68 |
-
|
| 69 |
HfFolder.save_token(HF_TOKEN)
|
| 70 |
api = HfApi()
|
| 71 |
|
| 72 |
-
# Load existing CSV or create new
|
| 73 |
local_file = "otp_temp.csv"
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
df = pd.concat([df, pd.DataFrame([{"otp": otp, "expiry": expiry}])], ignore_index=True)
|
| 78 |
df.to_csv(local_file, index=False)
|
| 79 |
|
| 80 |
-
# Push to HF dataset
|
| 81 |
dataset = HFDataset.from_pandas(df)
|
| 82 |
dataset.push_to_hub(REPO_ID, token=HF_TOKEN)
|
| 83 |
|
| 84 |
-
print(f"✅ OTP {otp}
|
| 85 |
except Exception as e:
|
| 86 |
-
print("❌
|
| 87 |
-
|
| 88 |
-
# ---------------- TEMPLATES ---------------- #
|
| 89 |
-
|
| 90 |
-
@app.route("/logout")
|
| 91 |
-
def logout():
|
| 92 |
-
session.clear()
|
| 93 |
-
return redirect(url_for("admin"))
|
| 94 |
|
| 95 |
@app.after_request
|
| 96 |
def allow_iframe(response):
|
|
|
|
| 24 |
app.secret_key = os.getenv("FLASK_SECRET", "changeme123")
|
| 25 |
limiter = Limiter(get_remote_address, app=app)
|
| 26 |
|
| 27 |
+
# --- Memory store for OTPs
|
| 28 |
otp_store = {}
|
| 29 |
|
| 30 |
# ---------------- ROUTES ---------------- #
|
|
|
|
| 40 |
pwd = request.form.get("password")
|
| 41 |
if user == ADMIN_USER and pwd == ADMIN_PASS:
|
| 42 |
session["logged_in"] = True
|
| 43 |
+
return redirect(url_for("dashboard"))
|
| 44 |
flash("Invalid credentials", "error")
|
| 45 |
return render_template("login.html")
|
| 46 |
|
| 47 |
+
@app.route("/dashboard")
|
| 48 |
+
def dashboard():
|
| 49 |
+
if not session.get("logged_in"):
|
| 50 |
+
return redirect(url_for("admin"))
|
| 51 |
+
return render_template("dashboard.html")
|
| 52 |
+
|
| 53 |
@app.route("/generate_otp")
|
| 54 |
def generate_otp():
|
| 55 |
if not session.get("logged_in"):
|
|
|
|
| 59 |
expiry = datetime.utcnow() + timedelta(minutes=OTP_EXPIRY_MINUTES)
|
| 60 |
otp_store[otp] = {"expiry": expiry, "used": False}
|
| 61 |
|
|
|
|
| 62 |
save_otp_to_dataset(otp, expiry.strftime("%Y-%m-%d %H:%M:%S UTC"))
|
| 63 |
|
| 64 |
return render_template("otp_result.html", otp=otp, expiry=expiry.strftime("%Y-%m-%d %H:%M UTC"))
|
| 65 |
|
| 66 |
+
@app.route("/logout")
|
| 67 |
+
def logout():
|
| 68 |
+
session.clear()
|
| 69 |
+
return redirect(url_for("admin"))
|
| 70 |
+
|
| 71 |
# ---------------- HELPERS ---------------- #
|
| 72 |
|
| 73 |
def save_otp_to_dataset(otp: str, expiry: str):
|
|
|
|
| 75 |
if not HF_TOKEN:
|
| 76 |
print("❌ HF_TOKEN not set.")
|
| 77 |
return
|
| 78 |
+
|
| 79 |
HfFolder.save_token(HF_TOKEN)
|
| 80 |
api = HfApi()
|
| 81 |
|
|
|
|
| 82 |
local_file = "otp_temp.csv"
|
| 83 |
+
if not os.path.exists(local_file):
|
| 84 |
+
pd.DataFrame(columns=["otp", "expiry"]).to_csv(local_file, index=False)
|
| 85 |
+
|
| 86 |
+
df = pd.read_csv(local_file)
|
| 87 |
df = pd.concat([df, pd.DataFrame([{"otp": otp, "expiry": expiry}])], ignore_index=True)
|
| 88 |
df.to_csv(local_file, index=False)
|
| 89 |
|
|
|
|
| 90 |
dataset = HFDataset.from_pandas(df)
|
| 91 |
dataset.push_to_hub(REPO_ID, token=HF_TOKEN)
|
| 92 |
|
| 93 |
+
print(f"✅ OTP {otp} pushed to dataset.")
|
| 94 |
except Exception as e:
|
| 95 |
+
print("❌ Dataset push failed:", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
@app.after_request
|
| 98 |
def allow_iframe(response):
|