Spaces:
Configuration error
Configuration error
Download streamlit_app/utils/api_client.py from rockysreenivasa/MathTutor: direct link, hf CLI and curl.
- Browser
- Download file 2.82 kB
-
https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/streamlit_app/utils/api_client.py
- Command line
-
hf download hf://spaces/rockysreenivasa/MathTutor/streamlit_app/utils/api_client.py
-
curl -L -o api_client.py https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/streamlit_app/utils/api_client.py
2.82 kB
| """ | |
| 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() | |