Kerikim commited on
Commit
3283a85
·
1 Parent(s): d68de5a

frontend stuff elkay

Browse files
Files changed (3) hide show
  1. FinED-Front-output.md +0 -0
  2. FinED-Front-output.txt +0 -0
  3. utils/api.py +34 -27
FinED-Front-output.md ADDED
The diff for this file is too large to render. See raw diff
 
FinED-Front-output.txt ADDED
The diff for this file is too large to render. See raw diff
 
utils/api.py CHANGED
@@ -1,46 +1,53 @@
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
- def start_agent(student_id:int, lesson_id:int, level_slug:str):
11
- r = requests.post(f"{BACKEND}/agent/start", json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug})
12
- r.raise_for_status()
13
- return r.json()
 
 
 
 
 
 
 
 
 
 
14
 
15
  def health():
16
- r = requests.get(f"{BACKEND}/", timeout=10)
17
- r.raise_for_status()
18
- return r.json()
 
 
19
 
20
  def get_quiz(student_id:int, lesson_id:int, level_slug:str):
21
- r = requests.post(f"{BACKEND}/agent/quiz", json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug})
22
- r.raise_for_status()
23
- return r.json()["items"]
24
 
25
  def grade_quiz(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
26
- r = requests.post(f"{BACKEND}/agent/grade", json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,"answers":answers,"assignment_id":assignment_id})
27
- r.raise_for_status()
28
- d = r.json(); return d["score"], d["total"]
 
29
 
30
  def next_step(student_id:int, lesson_id:int, level_slug:str, answers:list[str], assignment_id:int|None=None):
31
- r = requests.post(f"{BACKEND}/agent/coach_or_celebrate", json={"student_id":student_id,"lesson_id":lesson_id,"level_slug":level_slug,"answers":answers,"assignment_id":assignment_id})
32
- r.raise_for_status()
33
- return r.json()
34
 
35
  def login(email: str, password: str):
36
- r = requests.post(f"{BACKEND}/auth/login", json={"email": email, "password": password})
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
- r = requests.post(f"{BACKEND}/auth/signup/student", json={
42
- "name": name, "email": email, "password": password,
43
- "level_label": level_label, "country_label": country_label
44
- })
45
- r.raise_for_status()
46
- return r.json()
 
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
 
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()
 
 
49
 
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()