sema-chat / setup_huggingface.sh
kamau1's picture
Initial Commit
639f3bb
#!/bin/bash
# πŸš€ Sema Chat API - HuggingFace Spaces Setup Script
# This script helps you deploy Sema Chat API to HuggingFace Spaces with Gemma
set -e
echo "πŸš€ Sema Chat API - HuggingFace Spaces Setup"
echo "=========================================="
# Check if we're in the right directory
if [ ! -f "app/main.py" ]; then
echo "❌ Error: Please run this script from the backend/sema-chat directory"
echo " Current directory: $(pwd)"
echo " Expected files: app/main.py, requirements.txt, Dockerfile"
exit 1
fi
echo "βœ… Found Sema Chat API files"
# Get user input
read -p "πŸ“ Enter your HuggingFace username: " HF_USERNAME
read -p "πŸ“ Enter your Space name (e.g., sema-chat-gemma): " SPACE_NAME
read -p "πŸ”‘ Enter your Google AI API key (or press Enter to skip): " GOOGLE_API_KEY
# Validate inputs
if [ -z "$HF_USERNAME" ]; then
echo "❌ Error: HuggingFace username is required"
exit 1
fi
if [ -z "$SPACE_NAME" ]; then
echo "❌ Error: Space name is required"
exit 1
fi
SPACE_URL="https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
SPACE_REPO="https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
echo ""
echo "πŸ“‹ Configuration Summary:"
echo " HuggingFace Username: $HF_USERNAME"
echo " Space Name: $SPACE_NAME"
echo " Space URL: $SPACE_URL"
echo " Google AI Key: ${GOOGLE_API_KEY:+[PROVIDED]}${GOOGLE_API_KEY:-[NOT PROVIDED]}"
echo ""
read -p "πŸ€” Continue with deployment? (y/N): " CONFIRM
if [[ ! $CONFIRM =~ ^[Yy]$ ]]; then
echo "❌ Deployment cancelled"
exit 0
fi
# Create deployment directory
DEPLOY_DIR="../sema-chat-deploy"
echo "πŸ“ Creating deployment directory: $DEPLOY_DIR"
rm -rf "$DEPLOY_DIR"
mkdir -p "$DEPLOY_DIR"
# Copy all files
echo "πŸ“‹ Copying files..."
cp -r . "$DEPLOY_DIR/"
cd "$DEPLOY_DIR"
# Create README.md for the Space
echo "πŸ“ Creating Space README..."
cat > README.md << EOF
---
title: Sema Chat API
emoji: πŸ’¬
colorFrom: purple
colorTo: pink
sdk: docker
pinned: false
license: mit
short_description: Modern chatbot API with Gemma integration and streaming capabilities
---
# Sema Chat API πŸ’¬
Modern chatbot API with streaming capabilities, powered by Google's Gemma model.
## πŸš€ Features
- **Real-time Streaming**: Server-Sent Events and WebSocket support
- **Gemma Integration**: Powered by Google's Gemma 2 9B model
- **Session Management**: Persistent conversation contexts
- **RESTful API**: Clean, documented endpoints
- **Interactive UI**: Built-in Swagger documentation
## πŸ”— API Endpoints
- **Chat**: \`POST /api/v1/chat\`
- **Streaming**: \`GET /api/v1/chat/stream\`
- **WebSocket**: \`ws://space-url/api/v1/chat/ws\`
- **Health**: \`GET /api/v1/health\`
- **Docs**: \`GET /\` (Swagger UI)
## πŸ’¬ Quick Test
\`\`\`bash
curl -X POST "https://$HF_USERNAME-$SPACE_NAME.hf.space/api/v1/chat" \\
-H "Content-Type: application/json" \\
-d '{
"message": "Hello! Can you introduce yourself?",
"session_id": "test-session"
}'
\`\`\`
## πŸ”„ Streaming Test
\`\`\`bash
curl -N -H "Accept: text/event-stream" \\
"https://$HF_USERNAME-$SPACE_NAME.hf.space/api/v1/chat/stream?message=Tell%20me%20about%20AI&session_id=test"
\`\`\`
## βš™οΈ Configuration
This Space is configured to use Google's Gemma model via AI Studio.
Set your \`GOOGLE_API_KEY\` in the Space settings to enable the API.
## πŸ› οΈ Built With
- **FastAPI**: Modern Python web framework
- **Google Gemma**: Advanced language model
- **Docker**: Containerized deployment
- **HuggingFace Spaces**: Hosting platform
---
Created by $HF_USERNAME | Powered by Sema AI
EOF
# Create .gitignore
echo "🚫 Creating .gitignore..."
cat > .gitignore << EOF
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
.pytest_cache/
.coverage
htmlcov/
.DS_Store
.vscode/
.idea/
logs/
*.log
EOF
# Initialize git repository
echo "πŸ”§ Initializing git repository..."
git init
git remote add origin "$SPACE_REPO"
# Create initial commit
echo "πŸ“¦ Creating initial commit..."
git add .
git commit -m "Initial deployment of Sema Chat API with Gemma support
Features:
- Google Gemma 2 9B integration
- Real-time streaming responses
- Session management
- RESTful API with Swagger docs
- WebSocket support
- Health monitoring
Configuration:
- MODEL_TYPE=google
- MODEL_NAME=gemma-2-9b-it
- Port: 7860 (HuggingFace standard)
"
echo ""
echo "πŸŽ‰ Setup Complete!"
echo "=================="
echo ""
echo "πŸ“‹ Next Steps:"
echo "1. Create your HuggingFace Space:"
echo " β†’ Go to: https://huggingface.co/spaces"
echo " β†’ Click 'Create new Space'"
echo " β†’ Name: $SPACE_NAME"
echo " β†’ SDK: Docker"
echo " β†’ License: MIT"
echo ""
echo "2. Push your code:"
echo " β†’ cd $DEPLOY_DIR"
echo " β†’ git push origin main"
echo ""
echo "3. Configure environment variables in Space settings:"
if [ -n "$GOOGLE_API_KEY" ]; then
echo " β†’ MODEL_TYPE=google"
echo " β†’ MODEL_NAME=gemma-2-9b-it"
echo " β†’ GOOGLE_API_KEY=$GOOGLE_API_KEY"
else
echo " β†’ MODEL_TYPE=google"
echo " β†’ MODEL_NAME=gemma-2-9b-it"
echo " β†’ GOOGLE_API_KEY=your_google_api_key_here"
echo ""
echo " πŸ”‘ Get your Google AI API key from: https://aistudio.google.com/"
fi
echo " β†’ DEBUG=false"
echo " β†’ ENVIRONMENT=production"
echo ""
echo "4. Wait for build and test:"
echo " β†’ Space URL: $SPACE_URL"
echo " β†’ API Docs: $SPACE_URL/"
echo " β†’ Health Check: $SPACE_URL/api/v1/health"
echo ""
echo "πŸš€ Your Sema Chat API will be live at:"
echo " $SPACE_URL"
echo ""
echo "Happy deploying! πŸ’¬βœ¨"