Really-amin commited on
Commit
46c9082
·
verified ·
1 Parent(s): ca083ff

Upload api/resources_endpoint.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. api/resources_endpoint.py +73 -18
api/resources_endpoint.py CHANGED
@@ -1,31 +1,86 @@
1
  """
2
- Resources Endpoint - API router for resource statistics
3
  """
4
- from fastapi import APIRouter
5
- from typing import Dict, Any
6
  from datetime import datetime
7
- import logging
8
 
9
- logger = logging.getLogger(__name__)
10
 
11
  router = APIRouter(prefix="/api/resources", tags=["resources"])
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  @router.get("/stats")
14
  async def resources_stats() -> Dict[str, Any]:
15
- """Get resource statistics"""
16
- return {
17
- "total": 0,
18
- "active": 0,
19
- "categories": [],
20
- "timestamp": datetime.utcnow().isoformat() + "Z"
21
- }
22
 
23
  @router.get("/list")
24
  async def resources_list() -> Dict[str, Any]:
25
- """Get list of all resources"""
26
- return {
27
- "resources": [],
28
- "total": 0,
29
- "timestamp": datetime.utcnow().isoformat() + "Z"
30
- }
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  """
2
+ Resources Endpoint - API router for resource statistics (patched for real registry data).
3
  """
 
 
4
  from datetime import datetime
5
+ from typing import Any, Dict
6
 
7
+ from fastapi import APIRouter
8
 
9
  router = APIRouter(prefix="/api/resources", tags=["resources"])
10
 
11
+
12
+ def _registry_stats() -> Dict[str, Any]:
13
+ try:
14
+ import hf_unified_server as server
15
+
16
+ summary, categories = server._summarize_resources()
17
+ return {
18
+ "total": summary.get("total", 0),
19
+ "active": summary.get("free", 0) + summary.get("premium", 0),
20
+ "free": summary.get("free", 0),
21
+ "premium": summary.get("premium", 0),
22
+ "categories": categories,
23
+ "registry_loaded": bool(getattr(server, "_RESOURCES_CACHE", None)),
24
+ }
25
+ except Exception:
26
+ try:
27
+ from unified_resource_loader import get_loader
28
+
29
+ loader = get_loader()
30
+ stats = loader.get_stats()
31
+ categories = [
32
+ {"name": cat, "count": count}
33
+ for cat, count in stats.get("categories", {}).items()
34
+ ]
35
+ return {
36
+ "total": stats.get("total_resources", 0),
37
+ "active": stats.get("total_resources", 0),
38
+ "free": stats.get("free_resources", 0),
39
+ "premium": stats.get("auth_required", 0),
40
+ "categories": categories,
41
+ "registry_loaded": loader.loaded,
42
+ }
43
+ except Exception:
44
+ return {"total": 0, "active": 0, "categories": [], "registry_loaded": False}
45
+
46
+
47
  @router.get("/stats")
48
  async def resources_stats() -> Dict[str, Any]:
49
+ """Get resource statistics from unified registry."""
50
+ data = _registry_stats()
51
+ data["timestamp"] = datetime.utcnow().isoformat() + "Z"
52
+ data["success"] = True
53
+ return data
54
+
 
55
 
56
  @router.get("/list")
57
  async def resources_list() -> Dict[str, Any]:
58
+ """Get list of all resources."""
59
+ try:
60
+ from unified_resource_loader import get_loader
 
 
 
61
 
62
+ loader = get_loader()
63
+ resources = [
64
+ {
65
+ "id": r.id,
66
+ "name": r.name,
67
+ "category": r.category,
68
+ "base_url": r.base_url,
69
+ "requires_auth": r.requires_auth(),
70
+ }
71
+ for r in loader.resources.values()
72
+ ]
73
+ return {
74
+ "success": True,
75
+ "resources": resources,
76
+ "total": len(resources),
77
+ "timestamp": datetime.utcnow().isoformat() + "Z",
78
+ }
79
+ except Exception as exc:
80
+ return {
81
+ "success": False,
82
+ "resources": [],
83
+ "total": 0,
84
+ "error": str(exc),
85
+ "timestamp": datetime.utcnow().isoformat() + "Z",
86
+ }