Spaces:
Sleeping
Sleeping
Tripagra commited on
Commit ·
a8772c4
1
Parent(s): 7ffb269
Add Hugging Face Spaces deployment configuration
Browse files- Dockerfile +34 -0
- HUGGINGFACE_DEPLOYMENT.md +340 -0
- README.md +54 -786
- README_SPACES.md +50 -0
- app.py +13 -0
Dockerfile
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hugging Face Spaces Dockerfile for Smart Escalation API
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install system dependencies
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
build-essential \
|
| 10 |
+
curl \
|
| 11 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
# Copy requirements first for better caching
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
|
| 16 |
+
# Install Python dependencies
|
| 17 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 18 |
+
|
| 19 |
+
# Copy application code
|
| 20 |
+
COPY . .
|
| 21 |
+
|
| 22 |
+
# Expose port 7860 (Hugging Face Spaces default)
|
| 23 |
+
EXPOSE 7860
|
| 24 |
+
|
| 25 |
+
# Set environment variables
|
| 26 |
+
ENV PORT=7860
|
| 27 |
+
ENV PYTHONUNBUFFERED=1
|
| 28 |
+
|
| 29 |
+
# Health check
|
| 30 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
| 31 |
+
CMD curl -f http://localhost:7860/health || exit 1
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 🤗 Deploy to Hugging Face Spaces (FREE)
|
| 2 |
+
|
| 3 |
+
Complete step-by-step guide to deploy your Smart Escalation API to Hugging Face Spaces for FREE.
|
| 4 |
+
|
| 5 |
+
## Why Hugging Face Spaces?
|
| 6 |
+
|
| 7 |
+
- ✅ **FREE** - No credit card required
|
| 8 |
+
- ✅ **2GB RAM** - Enough for ML models (vs Render's 512MB free tier)
|
| 9 |
+
- ✅ **Optimized for ML** - Built for apps like yours
|
| 10 |
+
- ✅ **Easy deployment** - Push code via Git
|
| 11 |
+
- ✅ **Automatic HTTPS** - Secure by default
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
## Step 1: Create Hugging Face Account (2 minutes)
|
| 16 |
+
|
| 17 |
+
1. **Go to Hugging Face**
|
| 18 |
+
- Visit: https://huggingface.co/join
|
| 19 |
+
|
| 20 |
+
2. **Sign Up**
|
| 21 |
+
- Enter your email
|
| 22 |
+
- Choose a username (e.g., `yourname`)
|
| 23 |
+
- Create password
|
| 24 |
+
- Verify email
|
| 25 |
+
|
| 26 |
+
3. **Get Access Token**
|
| 27 |
+
- Go to: https://huggingface.co/settings/tokens
|
| 28 |
+
- Click **"New token"**
|
| 29 |
+
- Name: `escalation-api-deploy`
|
| 30 |
+
- Role: **Write**
|
| 31 |
+
- Click **"Generate a token"**
|
| 32 |
+
- **Copy the token** - you'll need it soon!
|
| 33 |
+
|
| 34 |
+
---
|
| 35 |
+
|
| 36 |
+
## Step 2: Create a New Space (3 minutes)
|
| 37 |
+
|
| 38 |
+
1. **Create Space**
|
| 39 |
+
- Go to: https://huggingface.co/new-space
|
| 40 |
+
- Or click your profile → **"New Space"**
|
| 41 |
+
|
| 42 |
+
2. **Configure Space**
|
| 43 |
+
- **Owner**: Your username
|
| 44 |
+
- **Space name**: `escalation-api` (or any name you like)
|
| 45 |
+
- **License**: MIT
|
| 46 |
+
- **Select the Space SDK**: Choose **"Docker"** (important!)
|
| 47 |
+
- **Space hardware**: CPU basic (free)
|
| 48 |
+
- **Visibility**: Public
|
| 49 |
+
- Click **"Create Space"**
|
| 50 |
+
|
| 51 |
+
3. **Wait for Space Creation**
|
| 52 |
+
- You'll see a page with instructions
|
| 53 |
+
- Keep this page open
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## Step 3: Add Required Files (5 minutes)
|
| 58 |
+
|
| 59 |
+
We need to add a few files for Hugging Face Spaces:
|
| 60 |
+
|
| 61 |
+
### 3.1: Create Dockerfile
|
| 62 |
+
|
| 63 |
+
I'll create a Dockerfile for you:
|
| 64 |
+
|
| 65 |
+
```bash
|
| 66 |
+
# This will be created automatically in the next step
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
### 3.2: Create .env.example for Spaces
|
| 70 |
+
|
| 71 |
+
```bash
|
| 72 |
+
# This will be created automatically in the next step
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
Let me create these files now:
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
## Step 4: Push Code to Hugging Face (5 minutes)
|
| 80 |
+
|
| 81 |
+
### 4.1: Add Hugging Face Remote
|
| 82 |
+
|
| 83 |
+
Open your terminal and run:
|
| 84 |
+
|
| 85 |
+
```bash
|
| 86 |
+
# Add Hugging Face as a git remote
|
| 87 |
+
# Replace YOUR_USERNAME with your actual Hugging Face username
|
| 88 |
+
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/escalation-api
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
Example:
|
| 92 |
+
```bash
|
| 93 |
+
git remote add hf https://huggingface.co/spaces/john/escalation-api
|
| 94 |
+
```
|
| 95 |
+
|
| 96 |
+
### 4.2: Commit New Files
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
# Add the new Hugging Face files
|
| 100 |
+
git add Dockerfile README_SPACES.md app.py
|
| 101 |
+
|
| 102 |
+
# Commit
|
| 103 |
+
git commit -m "Add Hugging Face Spaces deployment files"
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
### 4.3: Push to Hugging Face
|
| 107 |
+
|
| 108 |
+
```bash
|
| 109 |
+
# Push to Hugging Face Spaces
|
| 110 |
+
git push hf main
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
**You'll be prompted for credentials:**
|
| 114 |
+
- **Username**: Your Hugging Face username
|
| 115 |
+
- **Password**: Use the **access token** you created in Step 1 (NOT your password!)
|
| 116 |
+
|
| 117 |
+
---
|
| 118 |
+
|
| 119 |
+
## Step 5: Configure Environment Variables (3 minutes)
|
| 120 |
+
|
| 121 |
+
1. **Go to Your Space**
|
| 122 |
+
- Visit: `https://huggingface.co/spaces/YOUR_USERNAME/escalation-api`
|
| 123 |
+
|
| 124 |
+
2. **Open Settings**
|
| 125 |
+
- Click the **"Settings"** tab at the top
|
| 126 |
+
|
| 127 |
+
3. **Add Secrets (Environment Variables)**
|
| 128 |
+
- Scroll down to **"Repository secrets"**
|
| 129 |
+
- Click **"New secret"** for each variable:
|
| 130 |
+
|
| 131 |
+
| Name | Value |
|
| 132 |
+
|------|-------|
|
| 133 |
+
| `GOOGLE_API_KEY` | Your Gemini API key from https://makersuite.google.com/app/apikey |
|
| 134 |
+
| `ARTICLES_DIR` | `data/articles` |
|
| 135 |
+
| `EMBEDDING_MODEL` | `all-MiniLM-L6-v2` |
|
| 136 |
+
| `RELEVANCE_THRESHOLD` | `0.5` |
|
| 137 |
+
| `TOP_K_CHUNKS` | `3` |
|
| 138 |
+
| `CHUNK_SIZE` | `500` |
|
| 139 |
+
| `CHUNK_OVERLAP` | `50` |
|
| 140 |
+
| `LLM_MODEL` | `gemini-1.5-flash` |
|
| 141 |
+
| `LLM_TEMPERATURE` | `0.3` |
|
| 142 |
+
| `CORS_ORIGINS` | `https://escalation-api-frontend.vercel.app,http://localhost:5173` |
|
| 143 |
+
|
| 144 |
+
4. **Save Each Secret**
|
| 145 |
+
- Click **"Add secret"** after entering each one
|
| 146 |
+
|
| 147 |
+
---
|
| 148 |
+
|
| 149 |
+
## Step 6: Wait for Build (10-15 minutes)
|
| 150 |
+
|
| 151 |
+
1. **Go to "App" Tab**
|
| 152 |
+
- Click the **"App"** tab at the top
|
| 153 |
+
|
| 154 |
+
2. **Watch Build Logs**
|
| 155 |
+
- You'll see the Docker build progress
|
| 156 |
+
- It will download ML models (~2GB)
|
| 157 |
+
- This takes 10-15 minutes on first build
|
| 158 |
+
|
| 159 |
+
3. **Build Complete**
|
| 160 |
+
- When done, you'll see: **"Running"** status
|
| 161 |
+
- Your API is now live! 🎉
|
| 162 |
+
|
| 163 |
+
---
|
| 164 |
+
|
| 165 |
+
## Step 7: Test Your API (2 minutes)
|
| 166 |
+
|
| 167 |
+
### 7.1: Get Your API URL
|
| 168 |
+
|
| 169 |
+
Your API is available at:
|
| 170 |
+
```
|
| 171 |
+
https://YOUR_USERNAME-escalation-api.hf.space
|
| 172 |
+
```
|
| 173 |
+
|
| 174 |
+
Example:
|
| 175 |
+
```
|
| 176 |
+
https://john-escalation-api.hf.space
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
### 7.2: Test with curl
|
| 180 |
+
|
| 181 |
+
```bash
|
| 182 |
+
curl -X POST https://YOUR_USERNAME-escalation-api.hf.space/ask \
|
| 183 |
+
-H "Content-Type: application/json" \
|
| 184 |
+
-d '{"question": "How do I reset my password?"}'
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
### 7.3: Test in Browser
|
| 188 |
+
|
| 189 |
+
Visit:
|
| 190 |
+
```
|
| 191 |
+
https://YOUR_USERNAME-escalation-api.hf.space/docs
|
| 192 |
+
```
|
| 193 |
+
|
| 194 |
+
You'll see interactive API documentation where you can test the `/ask` endpoint.
|
| 195 |
+
|
| 196 |
+
---
|
| 197 |
+
|
| 198 |
+
## Step 8: Connect Frontend to Backend (5 minutes)
|
| 199 |
+
|
| 200 |
+
Now connect your Vercel frontend to the Hugging Face backend:
|
| 201 |
+
|
| 202 |
+
### 8.1: Copy Your Backend URL
|
| 203 |
+
|
| 204 |
+
```
|
| 205 |
+
https://YOUR_USERNAME-escalation-api.hf.space
|
| 206 |
+
```
|
| 207 |
+
|
| 208 |
+
### 8.2: Add to Vercel
|
| 209 |
+
|
| 210 |
+
1. Go to https://vercel.com/dashboard
|
| 211 |
+
2. Open your project: `escalation-api-frontend`
|
| 212 |
+
3. Go to **Settings** → **Environment Variables**
|
| 213 |
+
4. Click **"Add New"**
|
| 214 |
+
- **Key**: `VITE_API_URL`
|
| 215 |
+
- **Value**: `https://YOUR_USERNAME-escalation-api.hf.space`
|
| 216 |
+
- **Environments**: Check all (Production, Preview, Development)
|
| 217 |
+
5. Click **"Save"**
|
| 218 |
+
|
| 219 |
+
### 8.3: Redeploy Frontend
|
| 220 |
+
|
| 221 |
+
1. Go to **Deployments** tab
|
| 222 |
+
2. Find the latest deployment
|
| 223 |
+
3. Click **"⋯"** (three dots) → **"Redeploy"**
|
| 224 |
+
4. Click **"Redeploy"** to confirm
|
| 225 |
+
5. Wait 1-2 minutes
|
| 226 |
+
|
| 227 |
+
---
|
| 228 |
+
|
| 229 |
+
## Step 9: Test End-to-End (1 minute)
|
| 230 |
+
|
| 231 |
+
1. **Visit Your Frontend**
|
| 232 |
+
- Go to: https://escalation-api-frontend.vercel.app/
|
| 233 |
+
|
| 234 |
+
2. **Ask a Question**
|
| 235 |
+
- Type: "How do I reset my password?"
|
| 236 |
+
- Click **"Send"**
|
| 237 |
+
|
| 238 |
+
3. **Success!** 🎉
|
| 239 |
+
- You should get an answer from your Hugging Face backend
|
| 240 |
+
- Your full-stack ML app is now live!
|
| 241 |
+
|
| 242 |
+
---
|
| 243 |
+
|
| 244 |
+
## 🎉 Deployment Complete!
|
| 245 |
+
|
| 246 |
+
Your app is now fully deployed:
|
| 247 |
+
|
| 248 |
+
- **Frontend**: https://escalation-api-frontend.vercel.app/ (Vercel - FREE)
|
| 249 |
+
- **Backend**: https://YOUR_USERNAME-escalation-api.hf.space (Hugging Face - FREE)
|
| 250 |
+
- **Total Cost**: $0/month 🎊
|
| 251 |
+
|
| 252 |
+
---
|
| 253 |
+
|
| 254 |
+
## Troubleshooting
|
| 255 |
+
|
| 256 |
+
### Build fails with "Out of memory"
|
| 257 |
+
|
| 258 |
+
**Solution**: Upgrade to Hugging Face Pro ($9/month) for more resources, or:
|
| 259 |
+
- Use a smaller embedding model: `all-MiniLM-L6-v2` → `paraphrase-MiniLM-L3-v2`
|
| 260 |
+
- Reduce dependencies in `requirements.txt`
|
| 261 |
+
|
| 262 |
+
### "Application startup failed"
|
| 263 |
+
|
| 264 |
+
**Solution**: Check the logs in the "App" tab:
|
| 265 |
+
- Verify `GOOGLE_API_KEY` is set correctly
|
| 266 |
+
- Check that all environment variables are set
|
| 267 |
+
- Look for Python errors in the logs
|
| 268 |
+
|
| 269 |
+
### API returns 404
|
| 270 |
+
|
| 271 |
+
**Solution**:
|
| 272 |
+
- Make sure the Space is "Running" (not "Building" or "Sleeping")
|
| 273 |
+
- Check the URL is correct: `https://YOUR_USERNAME-escalation-api.hf.space`
|
| 274 |
+
- Visit `/docs` to see available endpoints
|
| 275 |
+
|
| 276 |
+
### Frontend shows "Failed to fetch"
|
| 277 |
+
|
| 278 |
+
**Solution**:
|
| 279 |
+
- Verify `VITE_API_URL` is set in Vercel
|
| 280 |
+
- Check backend URL is correct (no trailing slash)
|
| 281 |
+
- Make sure Hugging Face Space is running
|
| 282 |
+
- Check CORS_ORIGINS includes your Vercel frontend URL
|
| 283 |
+
|
| 284 |
+
### Space goes to sleep
|
| 285 |
+
|
| 286 |
+
**Solution**: Hugging Face Spaces on free tier sleep after inactivity:
|
| 287 |
+
- First request after sleep takes 30-60 seconds (cold start)
|
| 288 |
+
- Subsequent requests are fast
|
| 289 |
+
- Upgrade to Pro ($9/month) for always-on instances
|
| 290 |
+
|
| 291 |
+
---
|
| 292 |
+
|
| 293 |
+
## Updating Your App
|
| 294 |
+
|
| 295 |
+
To update your code:
|
| 296 |
+
|
| 297 |
+
```bash
|
| 298 |
+
# Make changes to your code
|
| 299 |
+
git add .
|
| 300 |
+
git commit -m "Update feature"
|
| 301 |
+
|
| 302 |
+
# Push to GitHub (for version control)
|
| 303 |
+
git push origin main
|
| 304 |
+
|
| 305 |
+
# Push to Hugging Face (to deploy)
|
| 306 |
+
git push hf main
|
| 307 |
+
```
|
| 308 |
+
|
| 309 |
+
Hugging Face will automatically rebuild and redeploy.
|
| 310 |
+
|
| 311 |
+
---
|
| 312 |
+
|
| 313 |
+
## Cost Comparison
|
| 314 |
+
|
| 315 |
+
| Platform | Free Tier | Paid Tier | RAM | Notes |
|
| 316 |
+
|----------|-----------|-----------|-----|-------|
|
| 317 |
+
| **Hugging Face** | FREE | $9/month | 2GB | Best for ML apps |
|
| 318 |
+
| **Render** | 512MB (too small) | $7/month | 512MB+ | Good for web apps |
|
| 319 |
+
| **Railway** | $5 credit | $5-20/month | 512MB+ | Pay as you go |
|
| 320 |
+
| **Vercel** | ❌ Can't deploy ML | ❌ Can't deploy ML | 500MB limit | Frontend only |
|
| 321 |
+
|
| 322 |
+
---
|
| 323 |
+
|
| 324 |
+
## Next Steps
|
| 325 |
+
|
| 326 |
+
- ✅ Your app is live and working!
|
| 327 |
+
- 📊 Monitor usage in Hugging Face dashboard
|
| 328 |
+
- 🔧 Add more help articles in `data/articles/`
|
| 329 |
+
- 🎨 Customize the frontend
|
| 330 |
+
- 📈 Track API usage and performance
|
| 331 |
+
|
| 332 |
+
---
|
| 333 |
+
|
| 334 |
+
## Need Help?
|
| 335 |
+
|
| 336 |
+
- **Hugging Face Docs**: https://huggingface.co/docs/hub/spaces
|
| 337 |
+
- **Hugging Face Discord**: https://discord.gg/hugging-face
|
| 338 |
+
- **Your Space Settings**: https://huggingface.co/spaces/YOUR_USERNAME/escalation-api/settings
|
| 339 |
+
|
| 340 |
+
Enjoy your FREE ML-powered API! 🚀
|
README.md
CHANGED
|
@@ -1,767 +1,93 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
2. Import this repository
|
| 11 |
-
3. Add environment variable: `GOOGLE_API_KEY` (get from [Google AI Studio](https://makersuite.google.com/app/apikey))
|
| 12 |
-
4. Deploy!
|
| 13 |
|
| 14 |
-
|
| 15 |
|
| 16 |
-
## Features
|
| 17 |
|
| 18 |
- **RAG-Based Retrieval**: Uses sentence-transformers and FAISS for semantic search over help articles
|
| 19 |
- **Intelligent Escalation**: Automatically escalates to human agents when confidence is low
|
| 20 |
- **FastAPI Backend**: Fast, async API with automatic OpenAPI documentation
|
| 21 |
-
- **
|
| 22 |
-
- **Free Hosting Compatible**: No external databases required, deployable to free tiers
|
| 23 |
-
|
| 24 |
-
## Architecture
|
| 25 |
-
|
| 26 |
-
- **Backend**: Python serverless functions on Vercel
|
| 27 |
-
- **Vector Store**: FAISS (in-memory) with sentence-transformers embeddings
|
| 28 |
-
- **LLM**: Google Gemini API (gemini-1.5-flash)
|
| 29 |
-
- **Frontend**: React + Vite + TypeScript
|
| 30 |
-
- **Deployment**: Vercel (monorepo with frontend + backend)
|
| 31 |
-
|
| 32 |
-
## Quick Start
|
| 33 |
-
|
| 34 |
-
### Prerequisites
|
| 35 |
-
|
| 36 |
-
- Python 3.9+
|
| 37 |
-
- Node.js 18+ (for frontend)
|
| 38 |
-
- Google Gemini API key
|
| 39 |
-
|
| 40 |
-
### Backend Setup
|
| 41 |
-
|
| 42 |
-
1. **Clone the repository**
|
| 43 |
-
```bash
|
| 44 |
-
git clone <repository-url>
|
| 45 |
-
cd smart-escalation-api
|
| 46 |
-
```
|
| 47 |
-
|
| 48 |
-
2. **Create virtual environment**
|
| 49 |
-
```bash
|
| 50 |
-
python -m venv venv
|
| 51 |
-
source venv/bin/activate # On Windows: venv\Scripts\activate
|
| 52 |
-
```
|
| 53 |
-
|
| 54 |
-
3. **Install dependencies**
|
| 55 |
-
```bash
|
| 56 |
-
pip install -r requirements.txt
|
| 57 |
-
```
|
| 58 |
-
|
| 59 |
-
4. **Configure environment variables**
|
| 60 |
-
```bash
|
| 61 |
-
cp .env.example .env
|
| 62 |
-
# Edit .env and add your API key
|
| 63 |
-
```
|
| 64 |
-
|
| 65 |
-
5. **Run the server**
|
| 66 |
-
```bash
|
| 67 |
-
uvicorn src.main:app --reload
|
| 68 |
-
```
|
| 69 |
-
|
| 70 |
-
The API will be available at `http://localhost:8000`
|
| 71 |
-
|
| 72 |
-
API documentation: `http://localhost:8000/docs`
|
| 73 |
-
|
| 74 |
-
### Frontend Setup
|
| 75 |
-
|
| 76 |
-
1. **Navigate to frontend directory**
|
| 77 |
-
```bash
|
| 78 |
-
cd frontend
|
| 79 |
-
```
|
| 80 |
-
|
| 81 |
-
2. **Install dependencies**
|
| 82 |
-
```bash
|
| 83 |
-
npm install
|
| 84 |
-
```
|
| 85 |
-
|
| 86 |
-
3. **Configure environment**
|
| 87 |
-
```bash
|
| 88 |
-
cp .env.example .env
|
| 89 |
-
# Edit .env and set VITE_API_URL to your backend URL
|
| 90 |
-
```
|
| 91 |
-
|
| 92 |
-
4. **Run development server**
|
| 93 |
-
```bash
|
| 94 |
-
npm run dev
|
| 95 |
-
```
|
| 96 |
-
|
| 97 |
-
The UI will be available at `http://localhost:5173`
|
| 98 |
|
| 99 |
-
##
|
| 100 |
|
| 101 |
-
``
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
│ └── articles/ # Help article knowledge base (markdown files)
|
| 105 |
-
├── src/
|
| 106 |
-
│ ├── main.py # FastAPI application
|
| 107 |
-
│ ├── rag.py # RAG system with FAISS vector store
|
| 108 |
-
│ ├── escalation.py # Escalation decision engine
|
| 109 |
-
│ ├── llm_client.py # LLM API client (Google Gemini)
|
| 110 |
-
│ ├── chunking.py # Text chunking logic
|
| 111 |
-
│ └── config.py # Configuration management
|
| 112 |
-
├── tests/
|
| 113 |
-
│ ├── unit/ # Unit tests
|
| 114 |
-
│ ├── integration/ # Integration tests
|
| 115 |
-
│ └── property/ # Property-based tests (Hypothesis)
|
| 116 |
-
├── frontend/
|
| 117 |
-
│ └── src/ # React chat interface
|
| 118 |
-
├── requirements.txt # Python dependencies
|
| 119 |
-
├── .env.example # Environment variable template
|
| 120 |
-
└── README.md # This file
|
| 121 |
-
```
|
| 122 |
-
|
| 123 |
-
## API Usage
|
| 124 |
-
|
| 125 |
-
### POST /ask
|
| 126 |
-
|
| 127 |
-
Submit a customer question and receive an answer or escalation.
|
| 128 |
-
|
| 129 |
-
**Request:**
|
| 130 |
-
```json
|
| 131 |
-
{
|
| 132 |
-
"question": "How do I add a team member to my project?"
|
| 133 |
-
}
|
| 134 |
-
```
|
| 135 |
-
|
| 136 |
-
**Response (Answer):**
|
| 137 |
-
```json
|
| 138 |
-
{
|
| 139 |
-
"response_type": "answer",
|
| 140 |
-
"message": "To add a team member to your project in TaskFlow, go to Project Settings > Team Members and click 'Invite Member'. You can set their role as Viewer, Editor, or Admin.",
|
| 141 |
-
"confidence_explanation": "High confidence - retrieved relevant content from 'Managing Team Permissions' article with similarity score 0.82",
|
| 142 |
-
"sources": ["permissions.md"]
|
| 143 |
-
}
|
| 144 |
-
```
|
| 145 |
-
|
| 146 |
-
**Response (Escalation):**
|
| 147 |
-
```json
|
| 148 |
-
{
|
| 149 |
-
"response_type": "escalation",
|
| 150 |
-
"message": "I'm not certain I can answer this accurately. Let me connect you with a human support agent who can help.",
|
| 151 |
-
"confidence_explanation": "Low confidence - best retrieval score was 0.42, indicating question may be outside help article scope",
|
| 152 |
-
"sources": null
|
| 153 |
-
}
|
| 154 |
-
```
|
| 155 |
|
| 156 |
-
##
|
| 157 |
|
| 158 |
-
|
| 159 |
|
| 160 |
| Variable | Description | Default |
|
| 161 |
|----------|-------------|---------|
|
| 162 |
| `GOOGLE_API_KEY` | Google Gemini API key (required) | - |
|
| 163 |
-
| `
|
| 164 |
-
| `
|
|
|
|
| 165 |
| `TOP_K_CHUNKS` | Number of chunks to retrieve | `3` |
|
| 166 |
| `CHUNK_SIZE` | Characters per chunk | `500` |
|
| 167 |
| `CHUNK_OVERLAP` | Overlap between chunks | `50` |
|
| 168 |
-
| `LLM_MODEL` |
|
| 169 |
| `LLM_TEMPERATURE` | LLM temperature (0-1) | `0.3` |
|
|
|
|
| 170 |
|
| 171 |
-
##
|
| 172 |
|
| 173 |
-
|
| 174 |
-
- Best chunk relevance score < `RELEVANCE_THRESHOLD` (default 0.5)
|
| 175 |
-
- LLM signals uncertainty in its response
|
| 176 |
-
- No relevant chunks are retrieved
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
### Why Vercel for Everything?
|
| 183 |
-
|
| 184 |
-
Vercel supports both:
|
| 185 |
-
- **Frontend**: Static React apps (traditional use case)
|
| 186 |
-
- **Backend**: Python serverless functions via API routes
|
| 187 |
-
|
| 188 |
-
This means you can deploy the entire application to a single platform with:
|
| 189 |
-
- ✅ Single deployment workflow
|
| 190 |
-
- ✅ Unified environment variables
|
| 191 |
-
- ✅ Automatic HTTPS
|
| 192 |
-
- ✅ Global CDN
|
| 193 |
-
- ✅ Free tier for both frontend and backend
|
| 194 |
-
|
| 195 |
-
### Prerequisites
|
| 196 |
-
|
| 197 |
-
- GitHub account with your repository
|
| 198 |
-
- Google Gemini API key
|
| 199 |
-
- Vercel account (free tier available at [vercel.com](https://vercel.com))
|
| 200 |
-
|
| 201 |
-
### Project Structure for Vercel
|
| 202 |
-
|
| 203 |
-
First, we need to restructure the project to work with Vercel's serverless functions:
|
| 204 |
-
|
| 205 |
-
```
|
| 206 |
-
smart-escalation-api/
|
| 207 |
-
├── api/ # Backend API (Vercel Serverless Functions)
|
| 208 |
-
│ └── ask.py # POST /api/ask endpoint
|
| 209 |
-
├── frontend/ # React frontend
|
| 210 |
-
│ ├── src/
|
| 211 |
-
│ ├── package.json
|
| 212 |
-
│ └── vite.config.ts
|
| 213 |
-
├── src/ # Shared Python modules
|
| 214 |
-
│ ├── rag.py
|
| 215 |
-
│ ├── escalation.py
|
| 216 |
-
│ ├── llm_client.py
|
| 217 |
-
│ ├── chunking.py
|
| 218 |
-
│ └── config.py
|
| 219 |
-
├── data/
|
| 220 |
-
│ └── articles/ # Help articles
|
| 221 |
-
├── requirements.txt # Python dependencies
|
| 222 |
-
├── vercel.json # Vercel configuration
|
| 223 |
-
└── package.json # Root package.json for monorepo
|
| 224 |
```
|
| 225 |
|
| 226 |
-
###
|
| 227 |
-
|
| 228 |
-
Create `vercel.json` in the project root:
|
| 229 |
|
| 230 |
```json
|
| 231 |
{
|
| 232 |
-
"
|
| 233 |
-
"
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
"use": "@vercel/python"
|
| 237 |
-
},
|
| 238 |
-
{
|
| 239 |
-
"src": "frontend/package.json",
|
| 240 |
-
"use": "@vercel/static-build",
|
| 241 |
-
"config": {
|
| 242 |
-
"distDir": "frontend/dist"
|
| 243 |
-
}
|
| 244 |
-
}
|
| 245 |
-
],
|
| 246 |
-
"routes": [
|
| 247 |
-
{
|
| 248 |
-
"src": "/api/(.*)",
|
| 249 |
-
"dest": "/api/$1"
|
| 250 |
-
},
|
| 251 |
-
{
|
| 252 |
-
"src": "/(.*)",
|
| 253 |
-
"dest": "/frontend/$1"
|
| 254 |
-
}
|
| 255 |
-
],
|
| 256 |
-
"env": {
|
| 257 |
-
"PYTHON_VERSION": "3.9"
|
| 258 |
-
}
|
| 259 |
-
}
|
| 260 |
-
```
|
| 261 |
-
|
| 262 |
-
### Step 2: Create API Endpoint
|
| 263 |
-
|
| 264 |
-
Create `api/ask.py` to handle the `/api/ask` endpoint:
|
| 265 |
-
|
| 266 |
-
```python
|
| 267 |
-
from fastapi import FastAPI, Request
|
| 268 |
-
from fastapi.responses import JSONResponse
|
| 269 |
-
from pydantic import BaseModel
|
| 270 |
-
import sys
|
| 271 |
-
import os
|
| 272 |
-
|
| 273 |
-
# Add parent directory to path to import src modules
|
| 274 |
-
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 275 |
-
|
| 276 |
-
from src.rag import RAGSystem
|
| 277 |
-
from src.escalation import EscalationEngine
|
| 278 |
-
from src.llm_client import LLMClient
|
| 279 |
-
from src.config import Settings
|
| 280 |
-
|
| 281 |
-
# Initialize components (cached across invocations)
|
| 282 |
-
settings = Settings()
|
| 283 |
-
rag_system = RAGSystem(articles_dir="data/articles")
|
| 284 |
-
llm_client = LLMClient(api_key=settings.google_api_key)
|
| 285 |
-
escalation_engine = EscalationEngine(llm_client=llm_client)
|
| 286 |
-
|
| 287 |
-
app = FastAPI()
|
| 288 |
-
|
| 289 |
-
class QuestionRequest(BaseModel):
|
| 290 |
-
question: str
|
| 291 |
-
|
| 292 |
-
@app.post("/api/ask")
|
| 293 |
-
async def ask_question(request: QuestionRequest):
|
| 294 |
-
"""Process customer question and return answer or escalation."""
|
| 295 |
-
try:
|
| 296 |
-
# Retrieve relevant chunks
|
| 297 |
-
chunks = rag_system.retrieve(request.question, top_k=settings.top_k_chunks)
|
| 298 |
-
|
| 299 |
-
# Decide whether to answer or escalate
|
| 300 |
-
decision = escalation_engine.process_question(request.question, chunks)
|
| 301 |
-
|
| 302 |
-
return JSONResponse(content={
|
| 303 |
-
"response_type": decision.action,
|
| 304 |
-
"message": decision.message,
|
| 305 |
-
"confidence_explanation": decision.confidence_explanation,
|
| 306 |
-
"sources": decision.sources
|
| 307 |
-
})
|
| 308 |
-
except Exception as e:
|
| 309 |
-
return JSONResponse(
|
| 310 |
-
status_code=500,
|
| 311 |
-
content={
|
| 312 |
-
"response_type": "escalation",
|
| 313 |
-
"message": "I'm experiencing technical difficulties. Let me connect you with a human agent.",
|
| 314 |
-
"confidence_explanation": f"Error: {str(e)}",
|
| 315 |
-
"sources": None
|
| 316 |
-
}
|
| 317 |
-
)
|
| 318 |
-
|
| 319 |
-
# Vercel serverless function handler
|
| 320 |
-
async def handler(request: Request):
|
| 321 |
-
return await app(request)
|
| 322 |
-
```
|
| 323 |
-
|
| 324 |
-
### Step 3: Update Frontend API URL
|
| 325 |
-
|
| 326 |
-
Update `frontend/src/services/api.ts` to use relative API path:
|
| 327 |
-
|
| 328 |
-
```typescript
|
| 329 |
-
const API_URL = import.meta.env.VITE_API_URL || '/api';
|
| 330 |
-
|
| 331 |
-
export async function askQuestion(question: string) {
|
| 332 |
-
const response = await fetch(`${API_URL}/ask`, {
|
| 333 |
-
method: 'POST',
|
| 334 |
-
headers: {
|
| 335 |
-
'Content-Type': 'application/json',
|
| 336 |
-
},
|
| 337 |
-
body: JSON.stringify({ question }),
|
| 338 |
-
});
|
| 339 |
-
|
| 340 |
-
if (!response.ok) {
|
| 341 |
-
throw new Error('Failed to get response');
|
| 342 |
-
}
|
| 343 |
-
|
| 344 |
-
return response.json();
|
| 345 |
}
|
| 346 |
```
|
| 347 |
|
| 348 |
-
###
|
| 349 |
-
|
| 350 |
-
#### Option A: Deploy via Vercel Dashboard (Recommended)
|
| 351 |
-
|
| 352 |
-
1. **Create a Vercel Account**
|
| 353 |
-
- Go to [vercel.com](https://vercel.com)
|
| 354 |
-
- Sign up with your GitHub account
|
| 355 |
-
|
| 356 |
-
2. **Import Project**
|
| 357 |
-
- Click "Add New..." → "Project"
|
| 358 |
-
- Import your GitHub repository
|
| 359 |
-
- Vercel will auto-detect the configuration
|
| 360 |
-
|
| 361 |
-
3. **Configure Environment Variables**
|
| 362 |
-
|
| 363 |
-
In the "Environment Variables" section, add:
|
| 364 |
-
|
| 365 |
-
| Variable | Value | Notes |
|
| 366 |
-
|----------|-------|-------|
|
| 367 |
-
| `GOOGLE_API_KEY` | `AIza...` | **Required** - Your Gemini API key |
|
| 368 |
-
| `EMBEDDING_MODEL` | `all-MiniLM-L6-v2` | Optional - defaults to this |
|
| 369 |
-
| `RELEVANCE_THRESHOLD` | `0.5` | Optional - adjust escalation sensitivity |
|
| 370 |
-
| `TOP_K_CHUNKS` | `3` | Optional - number of chunks to retrieve |
|
| 371 |
-
| `LLM_MODEL` | `gemini-1.5-flash` | Optional - use flash for speed |
|
| 372 |
-
| `LLM_TEMPERATURE` | `0.3` | Optional - lower = more deterministic |
|
| 373 |
-
|
| 374 |
-
**Important**: Set these for all environments (Production, Preview, Development)
|
| 375 |
-
|
| 376 |
-
4. **Deploy**
|
| 377 |
-
- Click "Deploy"
|
| 378 |
-
- Wait 2-3 minutes for build and deployment
|
| 379 |
-
- Monitor build logs for any errors
|
| 380 |
-
|
| 381 |
-
5. **Verify Deployment**
|
| 382 |
-
- Visit your Vercel URL (e.g., `https://smart-escalation-api.vercel.app`)
|
| 383 |
-
- Test the chat interface
|
| 384 |
-
- Submit a test question
|
| 385 |
-
- Check browser console for any errors
|
| 386 |
-
|
| 387 |
-
#### Option B: Deploy via Vercel CLI
|
| 388 |
-
|
| 389 |
-
1. **Install Vercel CLI**
|
| 390 |
-
```bash
|
| 391 |
-
npm install -g vercel
|
| 392 |
-
```
|
| 393 |
-
|
| 394 |
-
2. **Login to Vercel**
|
| 395 |
-
```bash
|
| 396 |
-
vercel login
|
| 397 |
-
```
|
| 398 |
-
|
| 399 |
-
3. **Deploy**
|
| 400 |
-
```bash
|
| 401 |
-
vercel
|
| 402 |
-
```
|
| 403 |
-
|
| 404 |
-
Follow the prompts:
|
| 405 |
-
- Set up and deploy? **Y**
|
| 406 |
-
- Which scope? Select your account
|
| 407 |
-
- Link to existing project? **N**
|
| 408 |
-
- Project name? `smart-escalation-api`
|
| 409 |
-
- In which directory is your code located? `./`
|
| 410 |
-
|
| 411 |
-
4. **Add Environment Variables**
|
| 412 |
-
```bash
|
| 413 |
-
vercel env add GOOGLE_API_KEY
|
| 414 |
-
vercel env add EMBEDDING_MODEL
|
| 415 |
-
vercel env add RELEVANCE_THRESHOLD
|
| 416 |
-
# ... add other variables
|
| 417 |
-
```
|
| 418 |
-
|
| 419 |
-
5. **Deploy to Production**
|
| 420 |
-
```bash
|
| 421 |
-
vercel --prod
|
| 422 |
-
```
|
| 423 |
-
|
| 424 |
-
### Step 5: Configure Custom Domain (Optional)
|
| 425 |
-
|
| 426 |
-
1. Go to your project in Vercel Dashboard
|
| 427 |
-
2. Navigate to Settings → Domains
|
| 428 |
-
3. Add your custom domain (e.g., `support.yourdomain.com`)
|
| 429 |
-
4. Follow DNS configuration instructions:
|
| 430 |
-
- Add CNAME record pointing to `cname.vercel-dns.com`
|
| 431 |
-
- Or add A record pointing to Vercel's IP
|
| 432 |
-
5. Wait for DNS propagation (5-30 minutes)
|
| 433 |
-
6. Vercel automatically provisions SSL certificate
|
| 434 |
-
|
| 435 |
-
### Deployment Troubleshooting
|
| 436 |
-
|
| 437 |
-
#### Vercel Serverless Function Issues
|
| 438 |
-
|
| 439 |
-
**Problem: "Module not found" in API functions**
|
| 440 |
-
- **Solution**: Ensure `requirements.txt` is in project root
|
| 441 |
-
- Verify Python version is 3.9+ in `vercel.json`
|
| 442 |
-
- Check that `src/` modules are importable from `api/` directory
|
| 443 |
-
- Add `sys.path.append()` in `api/ask.py` if needed
|
| 444 |
-
|
| 445 |
-
**Problem: API function times out (10 second limit)**
|
| 446 |
-
- **Solution**: Vercel serverless functions have 10s timeout on free tier
|
| 447 |
-
- Optimize embedding model loading (cache between invocations)
|
| 448 |
-
- Consider upgrading to Pro plan for 60s timeout
|
| 449 |
-
- Use lighter embedding model if needed
|
| 450 |
-
|
| 451 |
-
**Problem: "Function size exceeds limit"**
|
| 452 |
-
- **Solution**: Vercel has 250MB limit for serverless functions
|
| 453 |
-
- Reduce dependencies in `requirements.txt`
|
| 454 |
-
- Use `.vercelignore` to exclude unnecessary files:
|
| 455 |
-
```
|
| 456 |
-
tests/
|
| 457 |
-
.pytest_cache/
|
| 458 |
-
.hypothesis/
|
| 459 |
-
*.pyc
|
| 460 |
-
__pycache__/
|
| 461 |
-
```
|
| 462 |
-
|
| 463 |
-
**Problem: Cold start takes too long**
|
| 464 |
-
- **Solution**: First request after inactivity loads embedding model (~2-3s)
|
| 465 |
-
- This is normal for serverless architecture
|
| 466 |
-
- Subsequent requests are fast (cached)
|
| 467 |
-
- Consider keeping function warm with periodic pings
|
| 468 |
-
|
| 469 |
-
**Problem: Environment variables not accessible**
|
| 470 |
-
- **Solution**: Ensure variables are set in Vercel dashboard
|
| 471 |
-
- Redeploy after adding new environment variables
|
| 472 |
-
- Check variable names match exactly (case-sensitive)
|
| 473 |
-
- Verify variables are set for correct environment (Production/Preview)
|
| 474 |
-
|
| 475 |
-
#### Frontend Issues
|
| 476 |
-
|
| 477 |
-
**Problem: Build fails with "Cannot find module"**
|
| 478 |
-
- **Solution**: Ensure `frontend/package.json` includes all dependencies
|
| 479 |
-
- Verify Node.js version (requires 18+)
|
| 480 |
-
- Run `npm install` locally to test
|
| 481 |
-
- Check `frontend/` is set as root directory in build settings
|
| 482 |
-
|
| 483 |
-
**Problem: Frontend loads but API calls fail with 404**
|
| 484 |
-
- **Solution**: Verify API routes are configured in `vercel.json`
|
| 485 |
-
- Check that `api/ask.py` exists and is properly structured
|
| 486 |
-
- Test API endpoint directly: `https://your-app.vercel.app/api/ask`
|
| 487 |
-
- Ensure frontend is calling `/api/ask` (not `/ask`)
|
| 488 |
-
|
| 489 |
-
**Problem: CORS errors in browser console**
|
| 490 |
-
- **Solution**: With Vercel monorepo, CORS shouldn't be needed (same origin)
|
| 491 |
-
- If using separate deployments, add CORS headers to API function
|
| 492 |
-
- Verify frontend and API are on same domain
|
| 493 |
-
|
| 494 |
-
**Problem: "Failed to fetch" errors**
|
| 495 |
-
- **Solution**: Check browser console for detailed error
|
| 496 |
-
- Verify API endpoint is accessible
|
| 497 |
-
- Test with curl: `curl -X POST https://your-app.vercel.app/api/ask -H "Content-Type: application/json" -d '{"question":"test"}'`
|
| 498 |
-
- Check network tab in browser dev tools
|
| 499 |
-
|
| 500 |
-
#### Build and Deployment Issues
|
| 501 |
-
|
| 502 |
-
**Problem: Build fails with Python errors**
|
| 503 |
-
- **Solution**: Check build logs in Vercel dashboard
|
| 504 |
-
- Verify all Python dependencies are in `requirements.txt`
|
| 505 |
-
- Ensure Python version compatibility (3.9+)
|
| 506 |
-
- Test locally: `pip install -r requirements.txt`
|
| 507 |
-
|
| 508 |
-
**Problem: Build succeeds but deployment fails**
|
| 509 |
-
- **Solution**: Check deployment logs for runtime errors
|
| 510 |
-
- Verify `vercel.json` configuration is correct
|
| 511 |
-
- Ensure all required files are committed to git
|
| 512 |
-
- Check that `data/articles/` directory exists with help articles
|
| 513 |
-
|
| 514 |
-
**Problem: Changes not reflecting after deployment**
|
| 515 |
-
- **Solution**: Clear browser cache (Ctrl+Shift+R or Cmd+Shift+R)
|
| 516 |
-
- Check that git changes are committed and pushed
|
| 517 |
-
- Verify Vercel deployed the correct branch
|
| 518 |
-
- Check deployment logs to confirm new version deployed
|
| 519 |
-
|
| 520 |
-
**Problem: Preview deployments work but production fails**
|
| 521 |
-
- **Solution**: Check environment variables are set for Production
|
| 522 |
-
- Verify production branch is correct (usually `main`)
|
| 523 |
-
- Compare environment variables between Preview and Production
|
| 524 |
-
- Check for hardcoded URLs or environment-specific code
|
| 525 |
-
|
| 526 |
-
#### Performance Issues
|
| 527 |
-
|
| 528 |
-
**Problem: Slow response times (>10 seconds)**
|
| 529 |
-
- **Solution**: Vercel free tier has 10s timeout for serverless functions
|
| 530 |
-
- Check LLM API latency in function logs
|
| 531 |
-
- Use `gemini-1.5-flash` instead of `gemini-1.5-pro`
|
| 532 |
-
- Optimize embedding model loading
|
| 533 |
-
- Consider upgrading to Pro plan for better performance
|
| 534 |
-
|
| 535 |
-
**Problem: Out of memory errors**
|
| 536 |
-
- **Solution**: Vercel serverless functions have 1GB memory limit (free tier)
|
| 537 |
-
- Reduce `TOP_K_CHUNKS` to decrease memory usage
|
| 538 |
-
- Use smaller embedding model
|
| 539 |
-
- Optimize chunk storage and retrieval
|
| 540 |
-
- Upgrade to Pro plan for 3GB memory
|
| 541 |
-
|
| 542 |
-
**Problem: Rate limiting from Vercel**
|
| 543 |
-
- **Solution**: Free tier has limits on function invocations
|
| 544 |
-
- Monitor usage in Vercel dashboard
|
| 545 |
-
- Implement client-side rate limiting
|
| 546 |
-
- Upgrade to Pro plan for higher limits
|
| 547 |
-
|
| 548 |
-
#### API Key and Authentication Issues
|
| 549 |
-
|
| 550 |
-
**Problem: "Invalid API key" errors**
|
| 551 |
-
- **Solution**: Verify `GOOGLE_API_KEY` is set correctly in Vercel
|
| 552 |
-
- Check API key has proper permissions
|
| 553 |
-
- Ensure quota is not exceeded
|
| 554 |
-
- Test API key with direct Gemini API call
|
| 555 |
-
|
| 556 |
-
**Problem: API key works locally but not on Vercel**
|
| 557 |
-
- **Solution**: Ensure environment variable is set in Vercel dashboard
|
| 558 |
-
- Redeploy after adding environment variables
|
| 559 |
-
- Check variable name matches exactly (case-sensitive)
|
| 560 |
-
- Verify no extra spaces or quotes in variable value
|
| 561 |
-
|
| 562 |
-
#### Data and File Issues
|
| 563 |
-
|
| 564 |
-
**Problem: Help articles not found**
|
| 565 |
-
- **Solution**: Ensure `data/articles/` directory is committed to git
|
| 566 |
-
- Verify file paths are correct (case-sensitive)
|
| 567 |
-
- Check that markdown files exist and are readable
|
| 568 |
-
- Test locally first: `ls data/articles/`
|
| 569 |
-
|
| 570 |
-
**Problem: Vector store fails to build**
|
| 571 |
-
- **Solution**: Check that all help articles are valid markdown
|
| 572 |
-
- Verify embedding model downloads successfully
|
| 573 |
-
- Check function logs for specific error messages
|
| 574 |
-
- Ensure sufficient memory for vector store creation
|
| 575 |
-
|
| 576 |
-
### Monitoring and Maintenance
|
| 577 |
-
|
| 578 |
-
#### Vercel Analytics
|
| 579 |
-
|
| 580 |
-
Enable Vercel Analytics to monitor:
|
| 581 |
-
- Page views and user traffic
|
| 582 |
-
- API function invocations
|
| 583 |
-
- Response times and errors
|
| 584 |
-
- Bandwidth usage
|
| 585 |
-
|
| 586 |
-
**To enable:**
|
| 587 |
-
1. Go to your project in Vercel Dashboard
|
| 588 |
-
2. Navigate to Analytics tab
|
| 589 |
-
3. Enable Web Analytics (free)
|
| 590 |
-
4. Enable Speed Insights for performance monitoring
|
| 591 |
-
|
| 592 |
-
#### Function Logs
|
| 593 |
-
|
| 594 |
-
Monitor serverless function logs:
|
| 595 |
-
1. Go to Vercel Dashboard → Your Project
|
| 596 |
-
2. Click on Deployments → Select deployment
|
| 597 |
-
3. View Function Logs for real-time monitoring
|
| 598 |
-
4. Check for errors, warnings, and performance metrics
|
| 599 |
-
|
| 600 |
-
**Key metrics to watch:**
|
| 601 |
-
- Function execution time (should be <5s)
|
| 602 |
-
- Error rate (should be <5%)
|
| 603 |
-
- Cold start frequency
|
| 604 |
-
- Memory usage
|
| 605 |
-
|
| 606 |
-
#### Updating the Application
|
| 607 |
-
|
| 608 |
-
**To update help articles:**
|
| 609 |
-
1. Edit markdown files in `data/articles/`
|
| 610 |
-
2. Commit and push to GitHub
|
| 611 |
-
3. Vercel automatically redeploys
|
| 612 |
-
4. Vector store rebuilds on next function invocation
|
| 613 |
-
|
| 614 |
-
**To update code:**
|
| 615 |
-
1. Make changes to Python or React code
|
| 616 |
-
2. Test locally first
|
| 617 |
-
3. Commit and push to GitHub
|
| 618 |
-
4. Vercel automatically builds and deploys
|
| 619 |
-
5. Monitor deployment logs for errors
|
| 620 |
-
|
| 621 |
-
**To update dependencies:**
|
| 622 |
-
1. Update `requirements.txt` (Python) or `package.json` (Node)
|
| 623 |
-
2. Test locally: `pip install -r requirements.txt` or `npm install`
|
| 624 |
-
3. Commit and push
|
| 625 |
-
4. Vercel rebuilds with new dependencies
|
| 626 |
-
|
| 627 |
-
### Cost Estimates
|
| 628 |
-
|
| 629 |
-
**Vercel Free Tier Limits:**
|
| 630 |
-
- 100GB bandwidth per month
|
| 631 |
-
- 100 hours serverless function execution per month
|
| 632 |
-
- 6,000 function invocations per day
|
| 633 |
-
- 1GB function memory
|
| 634 |
-
- 10s function timeout
|
| 635 |
-
- Unlimited deployments
|
| 636 |
-
|
| 637 |
-
**Expected Usage for Low-Medium Traffic:**
|
| 638 |
-
- ~1-2s per API request
|
| 639 |
-
- ~100-500 requests/day = 100-500s function time/day
|
| 640 |
-
- ~3,000-15,000s function time/month = 0.8-4.2 hours/month
|
| 641 |
-
- Well within free tier limits
|
| 642 |
-
|
| 643 |
-
**When to Upgrade to Pro ($20/month):**
|
| 644 |
-
- Need >100 hours function execution
|
| 645 |
-
- Need >6,000 requests/day
|
| 646 |
-
- Need 60s timeout (vs 10s)
|
| 647 |
-
- Need 3GB memory (vs 1GB)
|
| 648 |
-
- Need priority support
|
| 649 |
-
|
| 650 |
-
**Additional Costs:**
|
| 651 |
-
- **Google Gemini API**: $0-10/month (free tier: 15 req/min, 1500 req/day)
|
| 652 |
-
- **Total**: $0-30/month depending on traffic
|
| 653 |
-
|
| 654 |
-
### Security Best Practices
|
| 655 |
-
|
| 656 |
-
1. **Environment Variables**
|
| 657 |
-
- Never commit API keys to git
|
| 658 |
-
- Use Vercel environment variables for all secrets
|
| 659 |
-
- Rotate API keys periodically
|
| 660 |
-
|
| 661 |
-
2. **CORS Configuration**
|
| 662 |
-
- With Vercel monorepo, CORS is handled automatically (same origin)
|
| 663 |
-
- If using separate deployments, restrict CORS to specific domains
|
| 664 |
-
|
| 665 |
-
3. **Input Validation**
|
| 666 |
-
- Already handled by Pydantic models
|
| 667 |
-
- Limit question length (max 500 characters)
|
| 668 |
-
- Sanitize user input
|
| 669 |
-
|
| 670 |
-
4. **Rate Limiting**
|
| 671 |
-
- Implement client-side throttling
|
| 672 |
-
- Consider adding server-side rate limiting for production
|
| 673 |
-
- Monitor for abuse in Vercel logs
|
| 674 |
-
|
| 675 |
-
5. **HTTPS**
|
| 676 |
-
- Vercel provides automatic HTTPS
|
| 677 |
-
- All traffic is encrypted by default
|
| 678 |
-
- SSL certificates auto-renewed
|
| 679 |
-
|
| 680 |
-
6. **Monitoring**
|
| 681 |
-
- Enable Vercel Analytics
|
| 682 |
-
- Monitor function logs regularly
|
| 683 |
-
- Set up alerts for errors or unusual traffic
|
| 684 |
-
|
| 685 |
-
### Advanced Configuration
|
| 686 |
-
|
| 687 |
-
#### Custom Build Configuration
|
| 688 |
-
|
| 689 |
-
Create `package.json` in project root for monorepo setup:
|
| 690 |
|
| 691 |
```json
|
| 692 |
{
|
| 693 |
-
"
|
| 694 |
-
"
|
| 695 |
-
"
|
| 696 |
-
|
| 697 |
-
},
|
| 698 |
-
"workspaces": [
|
| 699 |
-
"frontend"
|
| 700 |
-
]
|
| 701 |
}
|
| 702 |
```
|
| 703 |
|
| 704 |
-
##
|
| 705 |
-
|
| 706 |
-
Add warming function to keep serverless functions active:
|
| 707 |
-
|
| 708 |
-
```python
|
| 709 |
-
# api/health.py
|
| 710 |
-
def handler(request):
|
| 711 |
-
return {
|
| 712 |
-
"statusCode": 200,
|
| 713 |
-
"body": {"status": "healthy"}
|
| 714 |
-
}
|
| 715 |
-
```
|
| 716 |
-
|
| 717 |
-
Set up external monitoring (e.g., UptimeRobot) to ping `/api/health` every 5 minutes.
|
| 718 |
-
|
| 719 |
-
#### Environment-Specific Configuration
|
| 720 |
-
|
| 721 |
-
Use Vercel's environment system:
|
| 722 |
-
- **Production**: Live environment with production API keys
|
| 723 |
-
- **Preview**: Staging environment for testing (auto-created for PRs)
|
| 724 |
-
- **Development**: Local development environment
|
| 725 |
-
|
| 726 |
-
Set different values for each environment in Vercel dashboard.
|
| 727 |
-
|
| 728 |
-
### Getting Help
|
| 729 |
-
|
| 730 |
-
**Vercel Support:**
|
| 731 |
-
- Documentation: [vercel.com/docs](https://vercel.com/docs)
|
| 732 |
-
- Community: [github.com/vercel/vercel/discussions](https://github.com/vercel/vercel/discussions)
|
| 733 |
-
- Support: Available for Pro plan users
|
| 734 |
-
|
| 735 |
-
**Common Resources:**
|
| 736 |
-
- [Vercel Python Runtime](https://vercel.com/docs/functions/serverless-functions/runtimes/python)
|
| 737 |
-
- [Vercel Environment Variables](https://vercel.com/docs/projects/environment-variables)
|
| 738 |
-
- [Vercel Monorepo Guide](https://vercel.com/docs/monorepos)
|
| 739 |
|
| 740 |
-
|
| 741 |
|
| 742 |
-
##
|
| 743 |
-
```bash
|
| 744 |
-
pytest tests/unit -v
|
| 745 |
-
```
|
| 746 |
-
|
| 747 |
-
### Run Property-Based Tests
|
| 748 |
-
```bash
|
| 749 |
-
pytest tests/property -v
|
| 750 |
-
```
|
| 751 |
-
|
| 752 |
-
### Run Integration Tests
|
| 753 |
-
```bash
|
| 754 |
-
pytest tests/integration -v
|
| 755 |
-
```
|
| 756 |
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
|
| 762 |
-
## Help Articles
|
| 763 |
|
| 764 |
-
The system includes
|
| 765 |
|
| 766 |
1. **Getting Started** - TaskFlow basics, account setup, first project
|
| 767 |
2. **Billing and Subscriptions** - Plans, payment methods, billing cycles
|
|
@@ -769,70 +95,12 @@ The system includes 5 help articles about TaskFlow:
|
|
| 769 |
4. **Team Permissions** - Roles, access control, admin settings
|
| 770 |
5. **Troubleshooting** - Common errors, login issues, performance tips
|
| 771 |
|
| 772 |
-
|
| 773 |
|
| 774 |
-
|
|
|
|
|
|
|
| 775 |
|
| 776 |
-
##
|
| 777 |
-
|
| 778 |
-
1. Create a new markdown file in `data/articles/`
|
| 779 |
-
2. Restart the server to rebuild the vector store
|
| 780 |
-
3. Test retrieval with relevant questions
|
| 781 |
-
|
| 782 |
-
### Adjusting Escalation Threshold
|
| 783 |
-
|
| 784 |
-
Edit `RELEVANCE_THRESHOLD` in `.env`:
|
| 785 |
-
- Lower values (e.g., 0.3): Answer more questions, risk lower quality
|
| 786 |
-
- Higher values (e.g., 0.7): Escalate more often, ensure high quality
|
| 787 |
-
|
| 788 |
-
### Switching LLM Models
|
| 789 |
-
|
| 790 |
-
**Gemini Flash (faster, cheaper):**
|
| 791 |
-
```bash
|
| 792 |
-
GOOGLE_API_KEY=AIza...
|
| 793 |
-
LLM_MODEL=gemini-1.5-flash
|
| 794 |
-
```
|
| 795 |
-
|
| 796 |
-
**Gemini Pro (more capable):**
|
| 797 |
-
```bash
|
| 798 |
-
GOOGLE_API_KEY=AIza...
|
| 799 |
-
LLM_MODEL=gemini-1.5-pro
|
| 800 |
-
```
|
| 801 |
-
|
| 802 |
-
## Troubleshooting
|
| 803 |
-
|
| 804 |
-
### "Module not found" errors
|
| 805 |
-
```bash
|
| 806 |
-
# Ensure you're in the virtual environment
|
| 807 |
-
source venv/bin/activate
|
| 808 |
-
pip install -r requirements.txt
|
| 809 |
-
```
|
| 810 |
-
|
| 811 |
-
### "API key not found" errors
|
| 812 |
-
```bash
|
| 813 |
-
# Check .env file exists and contains valid API key
|
| 814 |
-
cat .env
|
| 815 |
-
```
|
| 816 |
-
|
| 817 |
-
### Slow first request
|
| 818 |
-
The first request loads the embedding model (~100MB), which takes 2-3 seconds. Subsequent requests are fast.
|
| 819 |
-
|
| 820 |
-
### CORS errors in frontend
|
| 821 |
-
Ensure `VITE_API_URL` in frontend `.env` matches your backend URL exactly (no trailing slash).
|
| 822 |
-
|
| 823 |
-
## Performance
|
| 824 |
-
|
| 825 |
-
- **Startup time**: 2-3 seconds (loading articles and embedding model)
|
| 826 |
-
- **Response time**: 3-5 seconds (95th percentile)
|
| 827 |
-
- Embedding generation: ~100ms
|
| 828 |
-
- Vector search: ~50ms
|
| 829 |
-
- LLM API call: 2-4 seconds
|
| 830 |
-
- **Memory usage**: ~150MB (embedding model + FAISS index)
|
| 831 |
-
|
| 832 |
-
## License
|
| 833 |
|
| 834 |
MIT
|
| 835 |
-
|
| 836 |
-
## Support
|
| 837 |
-
|
| 838 |
-
For questions or issues, please open a GitHub issue or contact the development team.
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Smart Escalation API
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
---
|
| 10 |
|
| 11 |
+
# Smart Escalation API
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
AI-powered L1 customer support system that intelligently answers customer questions or escalates to human agents when uncertain.
|
| 14 |
|
| 15 |
+
## 🚀 Features
|
| 16 |
|
| 17 |
- **RAG-Based Retrieval**: Uses sentence-transformers and FAISS for semantic search over help articles
|
| 18 |
- **Intelligent Escalation**: Automatically escalates to human agents when confidence is low
|
| 19 |
- **FastAPI Backend**: Fast, async API with automatic OpenAPI documentation
|
| 20 |
+
- **Google Gemini LLM**: Powered by gemini-1.5-flash for fast, accurate responses
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
## 📡 API Endpoints
|
| 23 |
|
| 24 |
+
- `GET /` - API information
|
| 25 |
+
- `GET /health` - Health check
|
| 26 |
+
- `POST /ask` - Submit customer questions and get answers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
## 🔧 Environment Variables
|
| 29 |
|
| 30 |
+
Configure these in your Space settings under "Repository secrets":
|
| 31 |
|
| 32 |
| Variable | Description | Default |
|
| 33 |
|----------|-------------|---------|
|
| 34 |
| `GOOGLE_API_KEY` | Google Gemini API key (required) | - |
|
| 35 |
+
| `ARTICLES_DIR` | Path to help articles | `data/articles` |
|
| 36 |
+
| `EMBEDDING_MODEL` | Sentence transformer model | `all-MiniLM-L6-v2` |
|
| 37 |
+
| `RELEVANCE_THRESHOLD` | Minimum similarity score (0-1) | `0.5` |
|
| 38 |
| `TOP_K_CHUNKS` | Number of chunks to retrieve | `3` |
|
| 39 |
| `CHUNK_SIZE` | Characters per chunk | `500` |
|
| 40 |
| `CHUNK_OVERLAP` | Overlap between chunks | `50` |
|
| 41 |
+
| `LLM_MODEL` | Gemini model name | `gemini-1.5-flash` |
|
| 42 |
| `LLM_TEMPERATURE` | LLM temperature (0-1) | `0.3` |
|
| 43 |
+
| `CORS_ORIGINS` | Allowed CORS origins | `*` |
|
| 44 |
|
| 45 |
+
## 📖 Usage
|
| 46 |
|
| 47 |
+
### Example Request
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
```bash
|
| 50 |
+
curl -X POST https://webflow97-deployment-taskflow.hf.space/ask \
|
| 51 |
+
-H "Content-Type: application/json" \
|
| 52 |
+
-d '{"question": "How do I reset my password?"}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
```
|
| 54 |
|
| 55 |
+
### Example Response (Answer)
|
|
|
|
|
|
|
| 56 |
|
| 57 |
```json
|
| 58 |
{
|
| 59 |
+
"response_type": "answer",
|
| 60 |
+
"message": "To reset your password, go to the login page and click 'Forgot Password'. Enter your email address and you'll receive a reset link.",
|
| 61 |
+
"confidence_explanation": "High confidence - retrieved relevant content with similarity score 0.82",
|
| 62 |
+
"sources": ["getting-started.md"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
```
|
| 65 |
|
| 66 |
+
###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
|
|
|
|
|
| 67 |
|
| 68 |
```json
|
| 69 |
{
|
| 70 |
+
"response_type": "escalation",
|
| 71 |
+
"message": "I'm not certain I can answer this accurately. Let me connect you with a human support agent who can help.",
|
| 72 |
+
"confidence_explanation": "Low confidence - best retrieval score was 0.42",
|
| 73 |
+
"sources": null
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
}
|
| 75 |
```
|
| 76 |
|
| 77 |
+
## 📚 Interactive Documentation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
Visit `/docs` for interactive API documentation powered by Swagger UI.
|
| 80 |
|
| 81 |
+
## 🏗️ Architecture
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
- **Backend**: Python FastAPI
|
| 84 |
+
- **Vector Store**: FAISS (in-memory) with sentence-transformers embeddings
|
| 85 |
+
- **LLM**: Google Gemini API (gemini-1.5-flash)
|
| 86 |
+
- **Deployment**: Hugging Face Spaces (Docker)
|
| 87 |
|
| 88 |
+
## 📝 Help Articles
|
| 89 |
|
| 90 |
+
The system includes help articles about TaskFlow (a fictional project management SaaS):
|
| 91 |
|
| 92 |
1. **Getting Started** - TaskFlow basics, account setup, first project
|
| 93 |
2. **Billing and Subscriptions** - Plans, payment methods, billing cycles
|
|
|
|
| 95 |
4. **Team Permissions** - Roles, access control, admin settings
|
| 96 |
5. **Troubleshooting** - Common errors, login issues, performance tips
|
| 97 |
|
| 98 |
+
## 🔗 Links
|
| 99 |
|
| 100 |
+
- **Frontend**: https://escalation-api-frontend.vercel.app/
|
| 101 |
+
- **GitHub**: https://github.com/webflow-ai/Escalation-api
|
| 102 |
+
- **API Docs**: https://webflow97-deployment-taskflow.hf.space/docs
|
| 103 |
|
| 104 |
+
## 📄 License
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
MIT
|
|
|
|
|
|
|
|
|
|
|
|
README_SPACES.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Smart Escalation API
|
| 3 |
+
emoji: 🤖
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Smart Escalation API
|
| 12 |
+
|
| 13 |
+
AI-powered L1 customer support system with intelligent escalation.
|
| 14 |
+
|
| 15 |
+
## Features
|
| 16 |
+
|
| 17 |
+
- RAG-based retrieval using sentence-transformers and FAISS
|
| 18 |
+
- Intelligent escalation to human agents
|
| 19 |
+
- Google Gemini LLM integration
|
| 20 |
+
- FastAPI backend with automatic documentation
|
| 21 |
+
|
| 22 |
+
## API Endpoints
|
| 23 |
+
|
| 24 |
+
- `GET /` - API information
|
| 25 |
+
- `GET /health` - Health check
|
| 26 |
+
- `POST /ask` - Submit customer questions
|
| 27 |
+
|
| 28 |
+
## Environment Variables
|
| 29 |
+
|
| 30 |
+
Configure these in your Space settings:
|
| 31 |
+
|
| 32 |
+
- `GOOGLE_API_KEY` - Your Google Gemini API key (required)
|
| 33 |
+
- `ARTICLES_DIR` - Path to help articles (default: data/articles)
|
| 34 |
+
- `EMBEDDING_MODEL` - Sentence transformer model (default: all-MiniLM-L6-v2)
|
| 35 |
+
- `RELEVANCE_THRESHOLD` - Minimum similarity score (default: 0.5)
|
| 36 |
+
- `TOP_K_CHUNKS` - Number of chunks to retrieve (default: 3)
|
| 37 |
+
- `LLM_MODEL` - Gemini model name (default: gemini-1.5-flash)
|
| 38 |
+
- `CORS_ORIGINS` - Allowed CORS origins
|
| 39 |
+
|
| 40 |
+
## Usage
|
| 41 |
+
|
| 42 |
+
```bash
|
| 43 |
+
curl -X POST https://YOUR_USERNAME-escalation-api.hf.space/ask \
|
| 44 |
+
-H "Content-Type: application/json" \
|
| 45 |
+
-d '{"question": "How do I reset my password?"}'
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## Documentation
|
| 49 |
+
|
| 50 |
+
Visit `/docs` for interactive API documentation.
|
app.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hugging Face Spaces entry point for Smart Escalation API.
|
| 3 |
+
This file is required for Hugging Face Spaces deployment.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import uvicorn
|
| 8 |
+
from src.main import app
|
| 9 |
+
|
| 10 |
+
if __name__ == "__main__":
|
| 11 |
+
# Hugging Face Spaces uses port 7860 by default
|
| 12 |
+
port = int(os.getenv("PORT", 7860))
|
| 13 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|