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 = """
Recovery Compass MCP Hub
🧭 Recovery Compass
AI-Powered Recovery Support with MCP Integration
🤖 MCP Server Integration
Connect to Hugging Face models, datasets, and Spaces
🔍 Semantic Search
Find recovery resources, research papers, and support tools
💬 AI Chat Support
24/7 AI-powered recovery guidance and support
📊 Progress Tracking
Monitor your recovery journey with AI insights
🚀 Login with Hugging Face
"""
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))