Spaces:
Runtime error
Runtime error
Download src/api/routes/graph.py from Harshilforworks/rag-backend: direct link, hf CLI and curl.
- Browser
- Download file 3.63 kB
-
https://huggingface.co/spaces/Harshilforworks/rag-backend/resolve/main/src/api/routes/graph.py
- Command line
-
hf download hf://spaces/Harshilforworks/rag-backend/src/api/routes/graph.py
-
curl -L -o graph.py https://huggingface.co/spaces/Harshilforworks/rag-backend/resolve/main/src/api/routes/graph.py
3.63 kB
| from __future__ import annotations | |
| import time | |
| from fastapi import APIRouter, Query, Request | |
| from src.api.models.responses import GraphAllResponse, GraphCourseResponse, HealthResponse, MetricsResponse | |
| from src.core.exceptions import GraphNodeNotFoundError | |
| from src.graph.reasoning import find_path_to_course, get_full_prereq_chain | |
| from src.graph.store import get_graph | |
| from src.ops.metrics import snapshot | |
| from src.retrieval.chroma_store import get_collection | |
| router = APIRouter() | |
| async def graph_course(course_code: str) -> GraphCourseResponse: | |
| graph = get_graph() | |
| code = course_code.upper().strip().replace(" ", "") | |
| if code not in graph: | |
| raise GraphNodeNotFoundError(f"Course {code} not found in graph") | |
| node_attrs = dict(graph.nodes[code]) | |
| prerequisites = sorted(list(graph.predecessors(code))) | |
| enables = sorted(list(graph.successors(code))) | |
| full_chain = get_full_prereq_chain(code) | |
| return GraphCourseResponse( | |
| course_code=code, | |
| course_name=str(node_attrs.get("course_name", code)), | |
| credits=int(node_attrs.get("credits", 0) or 0), | |
| prerequisites=prerequisites, | |
| enables=enables, | |
| full_prereq_chain=full_chain, | |
| node_attributes=node_attrs, | |
| ) | |
| async def graph_path( | |
| from_code: str = Query(alias="from"), | |
| to_code: str = Query(alias="to"), | |
| completed: str = "", | |
| ) -> dict: | |
| completed_courses = [course.strip().upper() for course in completed.split(",") if course.strip()] | |
| from_clean = from_code.strip().upper().replace(" ", "") | |
| if from_clean and from_clean not in completed_courses: | |
| completed_courses.append(from_clean) | |
| return find_path_to_course(to_code, completed_courses) | |
| async def graph_all() -> GraphAllResponse: | |
| graph = get_graph() | |
| nodes = [{"id": node, **attrs} for node, attrs in graph.nodes(data=True)] | |
| edges = [{"from": src, "to": dst, **attrs} for src, dst, attrs in graph.edges(data=True)] | |
| return GraphAllResponse(nodes=nodes, edges=edges, metadata=dict(graph.graph)) | |
| async def health(request: Request) -> HealthResponse: | |
| graph = get_graph() | |
| chroma_count = get_collection().count() | |
| started_at = float(getattr(request.app.state, "started_at", time.time())) | |
| uptime = max(0.0, time.time() - started_at) | |
| status = "ok" if chroma_count >= 0 else "degraded" | |
| return HealthResponse( | |
| status=status, | |
| chroma_document_count=int(chroma_count), | |
| graph_nodes=graph.number_of_nodes(), | |
| graph_edges=graph.number_of_edges(), | |
| uptime_seconds=uptime, | |
| ) | |
| async def ops_metrics() -> MetricsResponse: | |
| return MetricsResponse(**snapshot()) | |