| | #!/bin/bash |
| |
|
| | |
| | |
| |
|
| | set -e |
| |
|
| | echo "π Sema Chat API - HuggingFace Spaces Setup" |
| | echo "==========================================" |
| |
|
| | |
| | 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" |
| |
|
| | |
| | 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 |
| |
|
| | |
| | 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 |
| |
|
| | |
| | DEPLOY_DIR="../sema-chat-deploy" |
| | echo "π Creating deployment directory: $DEPLOY_DIR" |
| | rm -rf "$DEPLOY_DIR" |
| | mkdir -p "$DEPLOY_DIR" |
| |
|
| | |
| | echo "π Copying files..." |
| | cp -r . "$DEPLOY_DIR/" |
| | cd "$DEPLOY_DIR" |
| |
|
| | |
| | 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 |
| |
|
| | |
| | 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 |
| |
|
| | |
| | echo "π§ Initializing git repository..." |
| | git init |
| | git remote add origin "$SPACE_REPO" |
| |
|
| | |
| | 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! π¬β¨" |
| |
|