File size: 5,707 Bytes
639f3bb | 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | #!/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! π¬β¨"
|