Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- main.py +41 -0
- static/index.html +32 -11
main.py
CHANGED
|
@@ -5,6 +5,9 @@ Serves the chat API and static frontend.
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import json
|
|
|
|
|
|
|
|
|
|
| 8 |
import httpx
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from fastapi import FastAPI, Request
|
|
@@ -18,6 +21,9 @@ load_dotenv()
|
|
| 18 |
|
| 19 |
app = FastAPI(title="Irish Maths AI")
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 22 |
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
|
| 23 |
|
|
@@ -248,6 +254,41 @@ async def quick_topics():
|
|
| 248 |
app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")), name="static")
|
| 249 |
|
| 250 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
@app.get("/")
|
| 252 |
async def root():
|
| 253 |
return FileResponse(os.path.join(os.path.dirname(__file__), "static", "index.html"))
|
|
|
|
| 5 |
|
| 6 |
import os
|
| 7 |
import json
|
| 8 |
+
import hashlib
|
| 9 |
+
import hmac
|
| 10 |
+
import time
|
| 11 |
import httpx
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
from fastapi import FastAPI, Request
|
|
|
|
| 21 |
|
| 22 |
app = FastAPI(title="Irish Maths AI")
|
| 23 |
|
| 24 |
+
APP_PIN = os.getenv("APP_PIN", "AP19")
|
| 25 |
+
PIN_SECRET = os.getenv("PIN_SECRET", "mathai-pin-secret-key-2026")
|
| 26 |
+
|
| 27 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY", "")
|
| 28 |
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
|
| 29 |
|
|
|
|
| 254 |
app.mount("/static", StaticFiles(directory=os.path.join(os.path.dirname(__file__), "static")), name="static")
|
| 255 |
|
| 256 |
|
| 257 |
+
# ── PIN Auth ──────────────────────────────────────────────────────────────
|
| 258 |
+
|
| 259 |
+
class PinRequest(BaseModel):
|
| 260 |
+
pin: str
|
| 261 |
+
|
| 262 |
+
def _make_pin_token():
|
| 263 |
+
"""Create an HMAC token that proves PIN was entered correctly."""
|
| 264 |
+
payload = f"mathai-ok-{int(time.time()) // (86400 * 365)}"
|
| 265 |
+
sig = hmac.new(PIN_SECRET.encode(), payload.encode(), hashlib.sha256).hexdigest()[:32]
|
| 266 |
+
return f"{payload}.{sig}"
|
| 267 |
+
|
| 268 |
+
def _verify_pin_token(token: str) -> bool:
|
| 269 |
+
try:
|
| 270 |
+
parts = token.rsplit(".", 1)
|
| 271 |
+
if len(parts) != 2:
|
| 272 |
+
return False
|
| 273 |
+
payload, sig = parts
|
| 274 |
+
expected = hmac.new(PIN_SECRET.encode(), payload.encode(), hashlib.sha256).hexdigest()[:32]
|
| 275 |
+
return hmac.compare_digest(sig, expected)
|
| 276 |
+
except Exception:
|
| 277 |
+
return False
|
| 278 |
+
|
| 279 |
+
@app.post("/api/pin/verify")
|
| 280 |
+
async def verify_pin(req: PinRequest):
|
| 281 |
+
if req.pin == APP_PIN:
|
| 282 |
+
return {"ok": True, "token": _make_pin_token()}
|
| 283 |
+
return JSONResponse({"ok": False, "error": "Incorrect code"}, status_code=401)
|
| 284 |
+
|
| 285 |
+
@app.post("/api/pin/check")
|
| 286 |
+
async def check_pin_token(request: Request):
|
| 287 |
+
body = await request.json()
|
| 288 |
+
token = body.get("token", "")
|
| 289 |
+
return {"ok": _verify_pin_token(token)}
|
| 290 |
+
|
| 291 |
+
|
| 292 |
@app.get("/")
|
| 293 |
async def root():
|
| 294 |
return FileResponse(os.path.join(os.path.dirname(__file__), "static", "index.html"))
|
static/index.html
CHANGED
|
@@ -133,23 +133,44 @@
|
|
| 133 |
</div>
|
| 134 |
|
| 135 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
function checkPin() {
|
| 137 |
var pin = document.getElementById('pinInput').value.trim();
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
}
|
| 147 |
document.getElementById('pinInput').addEventListener('keydown', function(e) {
|
| 148 |
if (e.key === 'Enter') checkPin();
|
| 149 |
});
|
| 150 |
-
if (sessionStorage.getItem('nf_pin_ok') === '1') {
|
| 151 |
-
document.getElementById('pinOverlay').style.display = 'none';
|
| 152 |
-
}
|
| 153 |
</script>
|
| 154 |
<script src="/static/app.js"></script>
|
| 155 |
</body>
|
|
|
|
| 133 |
</div>
|
| 134 |
|
| 135 |
<script>
|
| 136 |
+
(async function() {
|
| 137 |
+
var overlay = document.getElementById('pinOverlay');
|
| 138 |
+
var stored = localStorage.getItem('mathai_pin_token');
|
| 139 |
+
if (stored) {
|
| 140 |
+
try {
|
| 141 |
+
var r = await fetch('/api/pin/check', {
|
| 142 |
+
method: 'POST',
|
| 143 |
+
headers: {'Content-Type': 'application/json'},
|
| 144 |
+
body: JSON.stringify({token: stored})
|
| 145 |
+
});
|
| 146 |
+
var d = await r.json();
|
| 147 |
+
if (d.ok) { overlay.style.display = 'none'; return; }
|
| 148 |
+
} catch(e) {}
|
| 149 |
+
localStorage.removeItem('mathai_pin_token');
|
| 150 |
+
}
|
| 151 |
+
})();
|
| 152 |
function checkPin() {
|
| 153 |
var pin = document.getElementById('pinInput').value.trim();
|
| 154 |
+
fetch('/api/pin/verify', {
|
| 155 |
+
method: 'POST',
|
| 156 |
+
headers: {'Content-Type': 'application/json'},
|
| 157 |
+
body: JSON.stringify({pin: pin})
|
| 158 |
+
}).then(function(r) { return r.json(); }).then(function(d) {
|
| 159 |
+
if (d.ok) {
|
| 160 |
+
localStorage.setItem('mathai_pin_token', d.token);
|
| 161 |
+
document.getElementById('pinOverlay').style.display = 'none';
|
| 162 |
+
} else {
|
| 163 |
+
document.getElementById('pinError').textContent = 'Incorrect code. Try again.';
|
| 164 |
+
document.getElementById('pinInput').value = '';
|
| 165 |
+
document.getElementById('pinInput').focus();
|
| 166 |
+
}
|
| 167 |
+
}).catch(function() {
|
| 168 |
+
document.getElementById('pinError').textContent = 'Connection error. Try again.';
|
| 169 |
+
});
|
| 170 |
}
|
| 171 |
document.getElementById('pinInput').addEventListener('keydown', function(e) {
|
| 172 |
if (e.key === 'Enter') checkPin();
|
| 173 |
});
|
|
|
|
|
|
|
|
|
|
| 174 |
</script>
|
| 175 |
<script src="/static/app.js"></script>
|
| 176 |
</body>
|