Spaces:
Sleeping
Sleeping
File size: 8,756 Bytes
5849687 bbac39e 5849687 bbac39e 5849687 | 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from huggingface_hub import HfApi, SpaceInfo
import os
import httpx
app = FastAPI(
title="Recovery Compass MCP Hub",
description="AI-powered recovery support with Hugging Face MCP integration",
version="1.0.0"
)
# CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize HF API
hf_api = HfApi()
@app.get("/", response_class=HTMLResponse)
async def root():
"""Welcome page with OAuth login option"""
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>Recovery Compass MCP Hub</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.container {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 20px;
padding: 40px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
}
h1 {
text-align: center;
font-size: 2.5em;
margin-bottom: 10px;
}
.tagline {
text-align: center;
font-size: 1.2em;
margin-bottom: 30px;
opacity: 0.9;
}
.features {
margin: 30px 0;
}
.feature {
background: rgba(255, 255, 255, 0.1);
padding: 15px;
margin: 10px 0;
border-radius: 10px;
}
.btn {
display: block;
width: 200px;
margin: 20px auto;
padding: 15px;
background: #ff6b6b;
color: white;
text-align: center;
text-decoration: none;
border-radius: 10px;
font-weight: bold;
transition: all 0.3s;
}
.btn:hover {
background: #ee5a5a;
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(0,0,0,0.3);
}
.api-link {
text-align: center;
margin-top: 20px;
}
.api-link a {
color: #ffeb3b;
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<h1>π§ Recovery Compass</h1>
<p class="tagline">AI-Powered Recovery Support with MCP Integration</p>
<div class="features">
<div class="feature">
<strong>π€ MCP Server Integration</strong>
<p>Connect to Hugging Face models, datasets, and Spaces</p>
</div>
<div class="feature">
<strong>π Semantic Search</strong>
<p>Find recovery resources, research papers, and support tools</p>
</div>
<div class="feature">
<strong>π¬ AI Chat Support</strong>
<p>24/7 AI-powered recovery guidance and support</p>
</div>
<div class="feature">
<strong>π Progress Tracking</strong>
<p>Monitor your recovery journey with AI insights</p>
</div>
</div>
<a href="https://huggingface.co/oauth/authorize?client_id=Recovery-Compass-79&scope=profile&redirect_uri=https://huggingface.co/spaces/Recovery-Compass-79/README" class="btn">
π Login with Hugging Face
</a>
<div class="api-link">
<a href="/docs">π API Documentation</a> |
<a href="/health">β€οΈ Health Check</a> |
<a href="/mcp">π MCP Info</a>
</div>
</div>
</body>
</html>
"""
return html_content
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"service": "Recovery Compass MCP Hub",
"version": "1.0.0",
"mcp_enabled": True
}
@app.get("/mcp")
async def mcp_info():
"""MCP server information"""
return {
"mcp_server": "Recovery Compass Hub",
"capabilities": [
"semantic_search",
"model_inference",
"dataset_access",
"space_integration",
"progress_tracking"
],
"connected_tools": [
"Hugging Face Hub API",
"Spaces Semantic Search",
"Papers Search",
"Model Repository",
"Dataset Repository"
],
"endpoints": {
"search": "/api/search",
"models": "/api/models",
"datasets": "/api/datasets",
"chat": "/api/chat"
}
}
@app.get("/api/search")
async def semantic_search(query: str):
"""Search Hugging Face resources"""
try:
# Search models
models = list(hf_api.list_models(search=query, limit=5))
# Search datasets
datasets = list(hf_api.list_datasets(search=query, limit=5))
return {
"query": query,
"models": [{"id": m.id, "tags": m.tags[:5]} for m in models],
"datasets": [{"id": d.id, "tags": d.tags[:5]} for d in datasets],
"total_results": len(models) + len(datasets)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/models")
async def list_models(task: str = None, limit: int = 10):
"""List available models"""
try:
models = list(hf_api.list_models(filter=task, limit=limit))
return {
"task": task,
"count": len(models),
"models": [
{
"id": m.id,
"tags": m.tags[:5],
"downloads": getattr(m, 'downloads', 0)
} for m in models
]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/datasets")
async def list_datasets(task: str = None, limit: int = 10):
"""List available datasets"""
try:
datasets = list(hf_api.list_datasets(filter=task, limit=limit))
return {
"task": task,
"count": len(datasets),
"datasets": [
{
"id": d.id,
"tags": d.tags[:5],
} for d in datasets
]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.post("/api/chat")
async def chat_endpoint(request: Request):
"""AI chat endpoint for recovery support"""
data = await request.json()
message = data.get("message", "")
return {
"response": f"Thank you for reaching out. I'm here to support your recovery journey. You said: {message}",
"suggestions": [
"Tell me about your recovery goals",
"Find support resources",
"Track my progress",
"Connect with community"
],
"resources": [
"Crisis support: Available 24/7",
"Community forums: Join discussions",
"Progress tracking: Start your journal"
]
}
@app.get("/api/spaces")
async def list_recovery_spaces():
"""List relevant recovery and support Spaces"""
try:
# Search for relevant spaces
spaces_to_recommend = [
"mental health support",
"crisis counseling",
"wellness tracking",
"mindfulness meditation"
]
all_spaces = []
for search_term in spaces_to_recommend:
spaces = list(hf_api.list_spaces(search=search_term, limit=3))
all_spaces.extend([{
"id": s.id,
"author": s.author,
"category": search_term
} for s in spaces])
return {
"recommended_spaces": all_spaces,
"total": len(all_spaces)
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
|