|
| 1 | +# Complete Deployment Guide: FastAPI Backend + Streamlit Frontend |
| 2 | + |
| 3 | +## Architecture Overview |
| 4 | +- **FastAPI Backend** (`rhl_fastapi_deploy.py`): Provides `/chat` and `/chat-stream` API endpoints |
| 5 | +- **Streamlit Frontend** (`streamlit_stream_demo.py`): Makes HTTP requests to FastAPI backend |
| 6 | + |
| 7 | +Both components MUST be deployed separately and accessible via URLs. |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## Option 1: Railway.app (Recommended - Easy) |
| 12 | + |
| 13 | +### Part A: Deploy FastAPI Backend |
| 14 | + |
| 15 | +1. **Install Railway CLI:** |
| 16 | + ```bash |
| 17 | + npm i -g @railway/cli |
| 18 | + ``` |
| 19 | + |
| 20 | +2. **Login to Railway:** |
| 21 | + ```bash |
| 22 | + railway login |
| 23 | + ``` |
| 24 | + |
| 25 | +3. **Initialize Railway project:** |
| 26 | + ```bash |
| 27 | + cd FASTAPI-DEPLOYMENT |
| 28 | + railway init |
| 29 | + ``` |
| 30 | + |
| 31 | +4. **Set environment variables in Railway dashboard:** |
| 32 | + - `OPENAI_API_KEY` = your OpenAI key |
| 33 | + - `GOOGLE_API_KEY` = your Google API key |
| 34 | + |
| 35 | +5. **Deploy:** |
| 36 | + ```bash |
| 37 | + railway up |
| 38 | + ``` |
| 39 | + |
| 40 | +6. **Get your FastAPI URL:** |
| 41 | + - Railway will provide a URL like: `https://your-app-name.railway.app` |
| 42 | + - **Save this URL!** You'll need it for Streamlit |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +### Part B: Deploy Streamlit Frontend |
| 47 | + |
| 48 | +**Option B1: Streamlit Cloud (Free)** |
| 49 | + |
| 50 | +1. **Push your code to GitHub** (if not already) |
| 51 | + |
| 52 | +2. **Go to:** https://share.streamlit.io |
| 53 | + |
| 54 | +3. **Click "New app"** |
| 55 | + |
| 56 | +4. **Configure:** |
| 57 | + - Repository: Your GitHub repo |
| 58 | + - Branch: `main` |
| 59 | + - Main file path: `FASTAPI-DEPLOYMENT/streamlit_stream_demo.py` |
| 60 | + |
| 61 | +5. **Advanced Settings → Secrets:** |
| 62 | + ``` |
| 63 | + FASTAPI_URL=https://your-app-name.railway.app |
| 64 | + ``` |
| 65 | + (Use the FastAPI URL from Part A) |
| 66 | + |
| 67 | +6. **Click "Deploy!"** |
| 68 | + |
| 69 | +7. **Share the Streamlit URL** with users! |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +**Option B2: Railway for Streamlit (Paid)** |
| 74 | + |
| 75 | +1. **Create new service in Railway** |
| 76 | + |
| 77 | +2. **Set environment variable:** |
| 78 | + ``` |
| 79 | + FASTAPI_URL=https://your-fastapi-url.railway.app |
| 80 | + ``` |
| 81 | + |
| 82 | +3. **Start command:** |
| 83 | + ``` |
| 84 | + streamlit run FASTAPI-DEPLOYMENT/streamlit_stream_demo.py --server.port $PORT --server.address 0.0.0.0 |
| 85 | + ``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Option 2: Render.com (Free Tier) |
| 90 | + |
| 91 | +### Part A: Deploy FastAPI Backend |
| 92 | + |
| 93 | +1. **Create `render.yaml` in project root:** |
| 94 | + ```yaml |
| 95 | + services: |
| 96 | + - type: web |
| 97 | + name: medical-chatbot-api |
| 98 | + env: python |
| 99 | + buildCommand: pip install -r requirements.txt |
| 100 | + startCommand: cd FASTAPI-DEPLOYMENT && uvicorn rhl_fastapi_deploy:app --host 0.0.0.0 --port $PORT |
| 101 | + envVars: |
| 102 | + - key: OPENAI_API_KEY |
| 103 | + sync: false |
| 104 | + - key: GOOGLE_API_KEY |
| 105 | + sync: false |
| 106 | + ``` |
| 107 | +
|
| 108 | +2. **Push to GitHub** |
| 109 | +
|
| 110 | +3. **Go to render.com → New Web Service** |
| 111 | +
|
| 112 | +4. **Connect repo → Render auto-detects `render.yaml`** |
| 113 | + |
| 114 | +5. **Get FastAPI URL:** `https://medical-chatbot-api.onrender.com` |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +### Part B: Deploy Streamlit on Streamlit Cloud |
| 119 | + |
| 120 | +1. **Deploy on Streamlit Cloud** (same as Option 1 Part B) |
| 121 | + |
| 122 | +2. **Set secret:** |
| 123 | + ``` |
| 124 | + FASTAPI_URL=https://medical-chatbot-api.onrender.com |
| 125 | + ``` |
| 126 | +
|
| 127 | +--- |
| 128 | +
|
| 129 | +## Option 3: Quick Testing with ngrok |
| 130 | +
|
| 131 | +### Part A: Start FastAPI Locally |
| 132 | +
|
| 133 | +1. **Terminal 1 - Start FastAPI:** |
| 134 | + ```bash |
| 135 | + cd FASTAPI-DEPLOYMENT |
| 136 | + python rhl_fastapi_deploy.py |
| 137 | + ``` |
| 138 | + |
| 139 | +2. **Terminal 2 - Expose with ngrok:** |
| 140 | + ```bash |
| 141 | + ngrok http 8000 |
| 142 | + ``` |
| 143 | + - Copy the HTTPS URL: `https://abc123.ngrok.io` |
| 144 | + - **This is your FastAPI URL!** |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +### Part B: Deploy Streamlit |
| 149 | + |
| 150 | +**Option 1: Streamlit Cloud** |
| 151 | +1. Deploy on Streamlit Cloud |
| 152 | +2. Set secret: `FASTAPI_URL=https://abc123.ngrok.io` |
| 153 | + |
| 154 | +**Option 2: Local + ngrok** |
| 155 | +1. **Terminal 3 - Start Streamlit:** |
| 156 | + ```bash |
| 157 | + cd FASTAPI-DEPLOYMENT |
| 158 | + streamlit run streamlit_stream_demo.py |
| 159 | + ``` |
| 160 | + |
| 161 | +2. **Terminal 4 - Expose Streamlit:** |
| 162 | + ```bash |
| 163 | + ngrok http 8501 |
| 164 | + ``` |
| 165 | + - Copy URL: `https://xyz789.ngrok.io` |
| 166 | + - **Share this with users!** |
| 167 | + - Users set API URL in sidebar: `https://abc123.ngrok.io` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Option 4: Local Network (Same WiFi) |
| 172 | + |
| 173 | +### Part A: Start FastAPI |
| 174 | + |
| 175 | +```bash |
| 176 | +cd FASTAPI-DEPLOYMENT |
| 177 | +python rhl_fastapi_deploy.py |
| 178 | +``` |
| 179 | + |
| 180 | +- FastAPI runs on: `http://YOUR_IP:8000` |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +### Part B: Start Streamlit |
| 185 | + |
| 186 | +```bash |
| 187 | +cd FASTAPI-DEPLOYMENT |
| 188 | +streamlit run streamlit_stream_demo.py --server.address 0.0.0.0 |
| 189 | +``` |
| 190 | + |
| 191 | +- Streamlit runs on: `http://YOUR_IP:8501` |
| 192 | +- Share this URL with others on same WiFi |
| 193 | +- They set API URL in sidebar: `http://YOUR_IP:8000` |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +## Quick Reference |
| 198 | + |
| 199 | +### FastAPI Deployment Commands |
| 200 | + |
| 201 | +**Railway:** |
| 202 | +```bash |
| 203 | +railway login |
| 204 | +railway init |
| 205 | +railway up |
| 206 | +``` |
| 207 | + |
| 208 | +**Render:** |
| 209 | +- Push to GitHub, connect to Render, auto-deploy |
| 210 | + |
| 211 | +**Local:** |
| 212 | +```bash |
| 213 | +python rhl_fastapi_deploy.py |
| 214 | +``` |
| 215 | + |
| 216 | +--- |
| 217 | + |
| 218 | +### Streamlit Deployment Commands |
| 219 | + |
| 220 | +**Streamlit Cloud:** |
| 221 | +- Push to GitHub → share.streamlit.io → Deploy |
| 222 | + |
| 223 | +**Local:** |
| 224 | +```bash |
| 225 | +streamlit run streamlit_stream_demo.py |
| 226 | +``` |
| 227 | + |
| 228 | +**Local (accessible on network):** |
| 229 | +```bash |
| 230 | +streamlit run streamlit_stream_demo.py --server.address 0.0.0.0 |
| 231 | +``` |
| 232 | + |
| 233 | +--- |
| 234 | + |
| 235 | +## Important Notes |
| 236 | + |
| 237 | +1. **FastAPI must be deployed FIRST** - Streamlit needs its URL |
| 238 | +2. **Set `FASTAPI_URL` environment variable** in Streamlit deployment |
| 239 | +3. **Or users can manually enter** FastAPI URL in Streamlit sidebar |
| 240 | +4. **Both services must be accessible** from the internet (or same network) |
| 241 | + |
| 242 | +--- |
| 243 | + |
| 244 | +## Troubleshooting |
| 245 | + |
| 246 | +### "Connection refused" error in Streamlit |
| 247 | +- ✅ Check FastAPI is running |
| 248 | +- ✅ Verify FastAPI URL is correct |
| 249 | +- ✅ Test FastAPI endpoint directly: `https://your-api-url.com/test-debug` |
| 250 | + |
| 251 | +### Streamlit can't find FastAPI |
| 252 | +- ✅ Check `FASTAPI_URL` environment variable is set |
| 253 | +- ✅ Or enter FastAPI URL manually in Streamlit sidebar |
| 254 | +- ✅ Ensure FastAPI URL is accessible (no firewall blocking) |
| 255 | + |
| 256 | +--- |
| 257 | + |
| 258 | +## Recommended Setup |
| 259 | + |
| 260 | +**For Quick Testing:** |
| 261 | +- FastAPI: Local + ngrok |
| 262 | +- Streamlit: Streamlit Cloud (set ngrok URL as secret) |
| 263 | + |
| 264 | +**For Production:** |
| 265 | +- FastAPI: Railway.app or Render.com |
| 266 | +- Streamlit: Streamlit Cloud (set production API URL as secret) |
| 267 | + |
0 commit comments