#!/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 = """
π CIDADΓO.AI - Monitoring Dashboard
ποΈ Status
β
Online
HuggingFace Spaces
π InvestigaΓ§Γ΅es
--
Total realizado
π¨ Anomalias
--
Detectadas
π€ Agentes
1
Zumbi Ativo
"""
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}"