File size: 2,820 Bytes
e46d575
 
 
 
 
 
 
 
 
 
 
 
 
c12625f
 
e46d575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c12625f
 
e46d575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
httpx-based API client for calling the MathMind FastAPI backend.
All calls include the Supabase JWT in the Authorization header.
"""

import os
import httpx
import streamlit as st
from dotenv import load_dotenv

load_dotenv()

API_BASE = os.getenv("API_BASE_URL", "http://localhost:8000")
TIMEOUT = 120.0  # seconds for non-stream requests
STREAM_TIMEOUT = httpx.Timeout(connect=10.0, read=None, write=120.0, pool=120.0)


def _headers() -> dict:
    token = st.session_state.get("access_token", "")
    return {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}


def health_check() -> bool:
    try:
        r = httpx.get(f"{API_BASE}/api/health", timeout=5)
        return r.status_code == 200
    except Exception:
        return False


def stream_explanation(query: str, grade: int):
    """
    Generator that yields text chunks from the streaming explanation endpoint.
    Use with st.write_stream().
    """
    # Keep read timeout disabled for streamed responses from local OSS models.
    with httpx.Client(timeout=STREAM_TIMEOUT) as client:
        with client.stream(
            "POST",
            f"{API_BASE}/api/explain",
            json={"query": query, "grade": grade},
            headers=_headers(),
        ) as response:
            response.raise_for_status()
            for chunk in response.iter_text():
                if chunk:
                    yield chunk


def get_practice(topic: str, grade: int, session_id: str, count: int = 3) -> dict:
    r = httpx.post(
        f"{API_BASE}/api/practice",
        json={"topic": topic, "grade": grade, "session_id": session_id, "count": count},
        headers=_headers(),
        timeout=TIMEOUT,
    )
    r.raise_for_status()
    return r.json()


def generate_test(topic: str, grade: int, session_id: str) -> dict:
    r = httpx.post(
        f"{API_BASE}/api/test/generate",
        json={"topic": topic, "grade": grade, "session_id": session_id},
        headers=_headers(),
        timeout=TIMEOUT,
    )
    r.raise_for_status()
    return r.json()


def evaluate_test(session_id: str, answers: list[dict], attempt_number: int = 1) -> dict:
    r = httpx.post(
        f"{API_BASE}/api/test/evaluate",
        json={"session_id": session_id, "answers": answers, "attempt_number": attempt_number},
        headers=_headers(),
        timeout=TIMEOUT,
    )
    r.raise_for_status()
    return r.json()


def get_badges() -> list[dict]:
    r = httpx.get(f"{API_BASE}/api/badges", headers=_headers(), timeout=TIMEOUT)
    r.raise_for_status()
    return r.json()


def check_badges(session_id: str) -> dict:
    r = httpx.post(
        f"{API_BASE}/api/badges/check",
        json={"session_id": session_id},
        headers=_headers(),
        timeout=TIMEOUT,
    )
    r.raise_for_status()
    return r.json()