lanna_lalala;- commited on
Commit
c2769bf
·
2 Parent(s): a7f43d1e902769

Merge branch 'main' of https://github.com/Alalalallalalalalalalalal/FinED-Front

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. utils/api.py +15 -35
.gitignore CHANGED
@@ -3,3 +3,5 @@ __pycache__/
3
  .venv/
4
  .env
5
  .DS_Store
 
 
 
3
  .venv/
4
  .env
5
  .DS_Store
6
+ FinED-Front-output.md
7
+ FinED-Front-output.txt
utils/api.py CHANGED
@@ -6,58 +6,43 @@ 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()
@@ -65,9 +50,4 @@ def login(email: str, password: str):
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()
 
6
  raise RuntimeError("BACKEND_URL is not set in Space secrets.")
7
 
8
  TOKEN = (os.getenv("BACKEND_TOKEN") or "").strip()
 
9
 
10
  _session = requests.Session()
11
+ _session.timeout = 30 # default timeout for all calls via this session
12
  if TOKEN:
13
  _session.headers["Authorization"] = f"Bearer {TOKEN}"
14
 
15
  def _req(method: str, path: str, **kw):
 
 
 
16
  url = f"{BACKEND}{path}"
17
+ r = _session.request(method, url, **kw)
 
18
  try:
 
19
  r.raise_for_status()
20
  except requests.HTTPError as e:
21
+ # make errors readable in Streamlit
22
+ raise RuntimeError(f"{method} {path} failed [{r.status_code}]: {r.text[:500]}") from e
 
 
 
 
 
 
 
 
23
  return r
24
 
25
  def health():
26
  return _req("GET", "/health").json()
27
 
28
+ def start_agent(student_id:int, lesson_id:int, level_slug:str):
29
  return _req("POST", "/agent/start",
30
+ json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug}).json()
31
 
32
+ def get_quiz(student_id:int, lesson_id:int, level_slug:str):
33
  return _req("POST", "/agent/quiz",
34
+ json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug}).json()["items"]
35
 
36
+ def grade_quiz(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
 
37
  d = _req("POST", "/agent/grade",
38
+ json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,
39
+ "answers":answers,"assignment_id":assignment_id}).json()
40
  return d["score"], d["total"]
41
 
42
+ def next_step(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
 
43
  return _req("POST", "/agent/coach_or_celebrate",
44
+ json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,
45
+ "answers":answers,"assignment_id":assignment_id}).json()
46
 
47
  def login(email: str, password: str):
48
  return _req("POST", "/auth/login", json={"email": email, "password": password}).json()
 
50
  def signup_student(name: str, email: str, password: str, level_label: str, country_label: str):
51
  return _req("POST", "/auth/signup/student",
52
  json={"name": name, "email": email, "password": password,
53
+ "level_label": level_label, "country_label": country_label}).json()