lanna_lalala;- commited on
Commit ·
a7f43d1
1
Parent(s): d68de5a
init frontend
Browse files- app.py +0 -1
- utils/api.py +57 -30
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
st.set_page_config(
|
| 4 |
page_title="Financial Education App",
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
st.set_page_config(
|
| 3 |
page_title="Financial Education App",
|
utils/api.py
CHANGED
|
@@ -1,46 +1,73 @@
|
|
| 1 |
# utils/api.py
|
| 2 |
import os, requests
|
| 3 |
|
| 4 |
-
BACKEND = (os.getenv("BACKEND_URL") or "").strip()
|
| 5 |
if not BACKEND:
|
| 6 |
raise RuntimeError("BACKEND_URL is not set in Space secrets.")
|
| 7 |
-
BACKEND = BACKEND.rstrip("/")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def health():
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
def get_quiz(student_id:int, lesson_id:int, level_slug:str):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return r.json()["items"]
|
| 24 |
|
| 25 |
-
def grade_quiz(student_id:int, lesson_id:int, level_slug:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
def next_step(student_id:int, lesson_id:int, level_slug:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
def login(email: str, password: str):
|
| 36 |
-
|
| 37 |
-
r.raise_for_status()
|
| 38 |
-
return r.json()
|
| 39 |
|
| 40 |
def signup_student(name: str, email: str, password: str, level_label: str, country_label: str):
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
| 1 |
# utils/api.py
|
| 2 |
import os, requests
|
| 3 |
|
| 4 |
+
BACKEND = (os.getenv("BACKEND_URL") or "").strip().rstrip("/")
|
| 5 |
if not BACKEND:
|
| 6 |
raise RuntimeError("BACKEND_URL is not set in Space secrets.")
|
|
|
|
| 7 |
|
| 8 |
+
TOKEN = (os.getenv("BACKEND_TOKEN") or "").strip()
|
| 9 |
+
DEFAULT_TIMEOUT = int(os.getenv("BACKEND_TIMEOUT", "30"))
|
| 10 |
|
| 11 |
+
_session = requests.Session()
|
| 12 |
+
if TOKEN:
|
| 13 |
+
_session.headers["Authorization"] = f"Bearer {TOKEN}"
|
| 14 |
+
|
| 15 |
+
def _req(method: str, path: str, **kw):
|
| 16 |
+
# make sure path starts with '/'
|
| 17 |
+
if not path.startswith("/"):
|
| 18 |
+
path = "/" + path
|
| 19 |
+
url = f"{BACKEND}{path}"
|
| 20 |
+
# apply a default timeout unless the caller overrides it
|
| 21 |
+
kw.setdefault("timeout", DEFAULT_TIMEOUT)
|
| 22 |
+
try:
|
| 23 |
+
r = _session.request(method, url, **kw)
|
| 24 |
+
r.raise_for_status()
|
| 25 |
+
except requests.HTTPError as e:
|
| 26 |
+
# Surface readable API errors in the UI
|
| 27 |
+
body = ""
|
| 28 |
+
try:
|
| 29 |
+
body = r.text[:500]
|
| 30 |
+
except Exception:
|
| 31 |
+
pass
|
| 32 |
+
raise RuntimeError(f"{method} {path} failed [{r.status_code}]: {body}") from e
|
| 33 |
+
except requests.RequestException as e:
|
| 34 |
+
# Network/DNS/timeouts, etc.
|
| 35 |
+
raise RuntimeError(f"{method} {path} failed: {e.__class__.__name__}: {e}") from e
|
| 36 |
+
return r
|
| 37 |
|
| 38 |
def health():
|
| 39 |
+
return _req("GET", "/health").json()
|
| 40 |
+
|
| 41 |
+
def start_agent(student_id: int, lesson_id: int, level_slug: str):
|
| 42 |
+
return _req("POST", "/agent/start",
|
| 43 |
+
json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug}).json()
|
| 44 |
|
| 45 |
+
def get_quiz(student_id: int, lesson_id: int, level_slug: str):
|
| 46 |
+
return _req("POST", "/agent/quiz",
|
| 47 |
+
json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug}).json()["items"]
|
|
|
|
| 48 |
|
| 49 |
+
def grade_quiz(student_id: int, lesson_id: int, level_slug: str,
|
| 50 |
+
answers: list[str], assignment_id: int | None = None):
|
| 51 |
+
d = _req("POST", "/agent/grade",
|
| 52 |
+
json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug,
|
| 53 |
+
"answers": answers, "assignment_id": assignment_id}).json()
|
| 54 |
+
return d["score"], d["total"]
|
| 55 |
|
| 56 |
+
def next_step(student_id: int, lesson_id: int, level_slug: str,
|
| 57 |
+
answers: list[str], assignment_id: int | None = None):
|
| 58 |
+
return _req("POST", "/agent/coach_or_celebrate",
|
| 59 |
+
json={"student_id": student_id, "lesson_id": lesson_id, "level_slug": level_slug,
|
| 60 |
+
"answers": answers, "assignment_id": assignment_id}).json()
|
| 61 |
|
| 62 |
def login(email: str, password: str):
|
| 63 |
+
return _req("POST", "/auth/login", json={"email": email, "password": password}).json()
|
|
|
|
|
|
|
| 64 |
|
| 65 |
def signup_student(name: str, email: str, password: str, level_label: str, country_label: str):
|
| 66 |
+
return _req("POST", "/auth/signup/student",
|
| 67 |
+
json={"name": name, "email": email, "password": password,
|
| 68 |
+
"level_label": level_label, "country_label": country_label}).json()
|
| 69 |
+
|
| 70 |
+
# Optional: if your frontend has a teacher sign-up flow
|
| 71 |
+
def signup_teacher(title: str, name: str, email: str, password: str):
|
| 72 |
+
return _req("POST", "/auth/signup/teacher",
|
| 73 |
+
json={"title": title, "name": name, "email": email, "password": password}).json()
|