README / app.py
Compass79's picture
Add MCP integration, OAuth support, and comprehensive API endpoints
5849687
Raw History Blame Contribute Delete
8.76 kB
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))