File size: 6,112 Bytes
824bf31 10c3c16 824bf31 10c3c16 824bf31 10c3c16 824bf31 |
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 |
#!/bin/bash
# =========================================
# ๐ Update HuggingFace with Monitoring
# =========================================
# Script to update HF Spaces with monitoring
# =========================================
set -e
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN}๐ Updating HuggingFace Spaces with Monitoring${NC}"
echo -e "${GREEN}================================================${NC}"
# Check if we're in the right directory
if [ ! -f "app.py" ]; then
echo -e "${RED}โ Error: app.py not found. Run from project root.${NC}"
exit 1
fi
# Check current branch
CURRENT_BRANCH=$(git branch --show-current)
echo -e "${YELLOW}๐ Current branch: $CURRENT_BRANCH${NC}"
# Stash any local changes
echo -e "${YELLOW}๐พ Stashing local changes...${NC}"
git stash
# Stay on main branch
echo -e "${YELLOW}๐ Staying on main branch...${NC}"
# Pull latest changes
echo -e "${YELLOW}๐ฅ Pulling latest changes...${NC}"
git pull origin main
# Apply the monitoring updates
echo -e "${YELLOW}๐ Applying monitoring updates...${NC}"
# Create the embedded monitoring HTML in app.py
cat >> app.py << 'EOF'
# Embedded monitoring HTML for HuggingFace Spaces
MONITORING_HTML_EMBEDDED = """<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>๐ CIDADรO.AI - Monitoring Dashboard</title>
<style>
:root { --primary: #10B981; --dark: #1F2937; }
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: Arial, sans-serif; background: #111827; color: #E5E7EB; }
.container { max-width: 1200px; margin: 0 auto; padding: 20px; }
header { background: #1F2937; padding: 20px; margin-bottom: 30px; border-radius: 10px; }
h1 { color: var(--primary); margin-bottom: 10px; }
.metrics-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; margin-bottom: 30px; }
.metric-card { background: #1F2937; padding: 20px; border-radius: 10px; border: 1px solid #374151; }
.metric-value { font-size: 2.5rem; font-weight: bold; color: #fff; margin: 10px 0; }
.metric-label { color: #9CA3AF; font-size: 0.9rem; }
.refresh-btn { background: var(--primary); color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; }
.links { text-align: center; margin: 20px 0; }
.links a { color: var(--primary); margin: 0 10px; text-decoration: none; }
</style>
</head>
<body>
<div class="container">
<header>
<h1>๐ CIDADรO.AI - Monitoring Dashboard</h1>
<p>Monitoramento em tempo real - HuggingFace Spaces</p>
</header>
<div class="links">
<a href="/">๐ Home</a> |
<a href="/docs">๐ API Docs</a> |
<a href="/metrics">๐ Mรฉtricas Raw</a>
</div>
<div class="metrics-grid">
<div class="metric-card">
<h3>๐๏ธ Status</h3>
<div class="metric-value">โ
Online</div>
<div class="metric-label">HuggingFace Spaces</div>
</div>
<div class="metric-card">
<h3>๐ Investigaรงรตes</h3>
<div class="metric-value" id="investigations">--</div>
<div class="metric-label">Total realizado</div>
</div>
<div class="metric-card">
<h3>๐จ Anomalias</h3>
<div class="metric-value" id="anomalies">--</div>
<div class="metric-label">Detectadas</div>
</div>
<div class="metric-card">
<h3>๐ค Agentes</h3>
<div class="metric-value">1</div>
<div class="metric-label">Zumbi Ativo</div>
</div>
</div>
<center>
<button class="refresh-btn" onclick="location.reload()">๐ Atualizar</button>
</center>
</div>
<script>
fetch('/metrics').then(r => r.text()).then(text => {
const inv = text.match(/cidadao_investigations_total.*\s+(\d+)/);
const anom = text.match(/cidadao_anomalies_detected_total.*\s+(\d+)/);
if (inv) document.getElementById('investigations').textContent = inv[1];
if (anom) document.getElementById('anomalies').textContent = anom[1];
});
</script>
</body>
</html>"""
EOF
# Update the monitoring endpoint to use embedded HTML
echo -e "${YELLOW}๐ Updating monitoring endpoint...${NC}"
sed -i 's/from monitoring_embedded import MONITORING_HTML/# Use embedded HTML/g' app.py
sed -i 's/return HTMLResponse(content=MONITORING_HTML)/return HTMLResponse(content=MONITORING_HTML_EMBEDDED)/g' app.py
# Remove the import line if it exists
sed -i '/import monitoring_embedded/d' app.py
# Commit changes
echo -e "${YELLOW}๐พ Committing changes...${NC}"
git add app.py
git commit -m "feat: add embedded monitoring dashboard for HF Spaces
- Add /monitoring endpoint with visual dashboard
- Embedded HTML to avoid import issues
- Real-time metrics visualization
- Auto-refresh functionality"
# Push to HuggingFace
echo -e "${YELLOW}๐ Pushing to HuggingFace...${NC}"
git push huggingface main
# Return to original branch
echo -e "${YELLOW}๐ Returning to $CURRENT_BRANCH branch...${NC}"
git checkout $CURRENT_BRANCH
# Restore stashed changes
echo -e "${YELLOW}๐พ Restoring stashed changes...${NC}"
git stash pop || true
echo -e "${GREEN}================================================${NC}"
echo -e "${GREEN}โ
Monitoring update complete!${NC}"
echo -e "${GREEN}================================================${NC}"
echo -e "\n${YELLOW}๐ Check the monitoring at:${NC}"
echo -e "${GREEN}https://neural-thinker-cidadao-ai-backend.hf.space/monitoring${NC}"
echo -e "\n${YELLOW}๐ Raw metrics at:${NC}"
echo -e "${GREEN}https://neural-thinker-cidadao-ai-backend.hf.space/metrics${NC}" |