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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Text Summarizer Backend API
|
| 2 |
+
|
| 3 |
+
A FastAPI-based backend service for text summarization using Ollama's local language models. Designed for Android app integration with cloud deployment capabilities.
|
| 4 |
+
|
| 5 |
+
## Features
|
| 6 |
+
|
| 7 |
+
- π **FastAPI** - Modern, fast web framework for building APIs
|
| 8 |
+
- π€ **Ollama Integration** - Local LLM inference with privacy-first approach
|
| 9 |
+
- π± **Android Ready** - RESTful API optimized for mobile consumption
|
| 10 |
+
- π **Request Tracking** - Unique request IDs and structured logging
|
| 11 |
+
- β
**Comprehensive Testing** - 30+ tests with >90% coverage
|
| 12 |
+
- π³ **Docker Ready** - Containerized deployment support
|
| 13 |
+
- βοΈ **Cloud Extensible** - Easy migration to cloud hosting
|
| 14 |
+
|
| 15 |
+
## Quick Start
|
| 16 |
+
|
| 17 |
+
### Prerequisites
|
| 18 |
+
|
| 19 |
+
- Python 3.7+
|
| 20 |
+
- [Ollama](https://ollama.ai) installed and running
|
| 21 |
+
- A compatible language model (e.g., `llama3.1:8b`)
|
| 22 |
+
|
| 23 |
+
### Installation
|
| 24 |
+
|
| 25 |
+
1. **Clone the repository**
|
| 26 |
+
```bash
|
| 27 |
+
git clone https://github.com/MingLu0/SummarizerBackend.git
|
| 28 |
+
cd SummarizerBackend
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
2. **Set up Ollama**
|
| 32 |
+
```bash
|
| 33 |
+
# Install Ollama (macOS)
|
| 34 |
+
brew install ollama
|
| 35 |
+
|
| 36 |
+
# Start Ollama service
|
| 37 |
+
ollama serve
|
| 38 |
+
|
| 39 |
+
# Pull a model (in another terminal)
|
| 40 |
+
ollama pull llama3.1:8b
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
3. **Set up Python environment**
|
| 44 |
+
```bash
|
| 45 |
+
# Create virtual environment
|
| 46 |
+
python3 -m venv .venv
|
| 47 |
+
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
| 48 |
+
|
| 49 |
+
# Install dependencies
|
| 50 |
+
pip install -r requirements.txt
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
4. **Run the API**
|
| 54 |
+
```bash
|
| 55 |
+
uvicorn app.main:app --host 127.0.0.1 --port 8000 --reload
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
5. **Test the API**
|
| 59 |
+
```bash
|
| 60 |
+
# Health check
|
| 61 |
+
curl http://127.0.0.1:8000/health
|
| 62 |
+
|
| 63 |
+
# Summarize text
|
| 64 |
+
curl -X POST http://127.0.0.1:8000/api/v1/summarize/ \
|
| 65 |
+
-H "Content-Type: application/json" \
|
| 66 |
+
-d '{"text": "Your long text to summarize here..."}'
|
| 67 |
+
```
|
| 68 |
+
|
| 69 |
+
## API Documentation
|
| 70 |
+
|
| 71 |
+
### Interactive Docs
|
| 72 |
+
- **Swagger UI**: http://127.0.0.1:8000/docs
|
| 73 |
+
- **ReDoc**: http://127.0.0.1:8000/redoc
|
| 74 |
+
|
| 75 |
+
### Endpoints
|
| 76 |
+
|
| 77 |
+
#### `GET /health`
|
| 78 |
+
Health check endpoint.
|
| 79 |
+
|
| 80 |
+
**Response:**
|
| 81 |
+
```json
|
| 82 |
+
{
|
| 83 |
+
"status": "ok",
|
| 84 |
+
"service": "text-summarizer-api",
|
| 85 |
+
"version": "1.0.0"
|
| 86 |
+
}
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
#### `POST /api/v1/summarize/`
|
| 90 |
+
Summarize text using Ollama.
|
| 91 |
+
|
| 92 |
+
**Request:**
|
| 93 |
+
```json
|
| 94 |
+
{
|
| 95 |
+
"text": "Your text to summarize...",
|
| 96 |
+
"max_tokens": 256,
|
| 97 |
+
"prompt": "Summarize the following text concisely:"
|
| 98 |
+
}
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
**Response:**
|
| 102 |
+
```json
|
| 103 |
+
{
|
| 104 |
+
"summary": "Generated summary text",
|
| 105 |
+
"model": "llama3.1:8b",
|
| 106 |
+
"tokens_used": 150,
|
| 107 |
+
"latency_ms": 1234.5
|
| 108 |
+
}
|
| 109 |
+
```
|
| 110 |
+
|
| 111 |
+
**Error Response:**
|
| 112 |
+
```json
|
| 113 |
+
{
|
| 114 |
+
"detail": "Summarization failed: Connection error",
|
| 115 |
+
"code": "OLLAMA_ERROR",
|
| 116 |
+
"request_id": "req-12345"
|
| 117 |
+
}
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
## Configuration
|
| 121 |
+
|
| 122 |
+
Configure the API using environment variables:
|
| 123 |
+
|
| 124 |
+
```bash
|
| 125 |
+
# Ollama Configuration
|
| 126 |
+
export OLLAMA_MODEL=llama3.1:8b
|
| 127 |
+
export OLLAMA_HOST=http://127.0.0.1:11434
|
| 128 |
+
export OLLAMA_TIMEOUT=30
|
| 129 |
+
|
| 130 |
+
# Server Configuration
|
| 131 |
+
export SERVER_HOST=127.0.0.1
|
| 132 |
+
export SERVER_PORT=8000
|
| 133 |
+
export LOG_LEVEL=INFO
|
| 134 |
+
|
| 135 |
+
# Optional: API Security
|
| 136 |
+
export API_KEY_ENABLED=false
|
| 137 |
+
export API_KEY=your-secret-key
|
| 138 |
+
|
| 139 |
+
# Optional: Rate Limiting
|
| 140 |
+
export RATE_LIMIT_ENABLED=false
|
| 141 |
+
export RATE_LIMIT_REQUESTS=60
|
| 142 |
+
export RATE_LIMIT_WINDOW=60
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
## Android Integration
|
| 146 |
+
|
| 147 |
+
### Retrofit Example
|
| 148 |
+
|
| 149 |
+
```kotlin
|
| 150 |
+
// API Interface
|
| 151 |
+
interface SummarizerApi {
|
| 152 |
+
@POST("api/v1/summarize/")
|
| 153 |
+
suspend fun summarize(@Body request: SummarizeRequest): SummarizeResponse
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
// Data Classes
|
| 157 |
+
data class SummarizeRequest(
|
| 158 |
+
val text: String,
|
| 159 |
+
val max_tokens: Int = 256,
|
| 160 |
+
val prompt: String = "Summarize the following text concisely:"
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
data class SummarizeResponse(
|
| 164 |
+
val summary: String,
|
| 165 |
+
val model: String,
|
| 166 |
+
val tokens_used: Int?,
|
| 167 |
+
val latency_ms: Double?
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
// Usage
|
| 171 |
+
val retrofit = Retrofit.Builder()
|
| 172 |
+
.baseUrl("http://127.0.0.1:8000/")
|
| 173 |
+
.addConverterFactory(GsonConverterFactory.create())
|
| 174 |
+
.build()
|
| 175 |
+
|
| 176 |
+
val api = retrofit.create(SummarizerApi::class.java)
|
| 177 |
+
val response = api.summarize(SummarizeRequest(text = "Your text here"))
|
| 178 |
+
```
|
| 179 |
+
|
| 180 |
+
### OkHttp Example
|
| 181 |
+
|
| 182 |
+
```kotlin
|
| 183 |
+
val client = OkHttpClient()
|
| 184 |
+
val json = JSONObject().apply {
|
| 185 |
+
put("text", "Your text to summarize")
|
| 186 |
+
put("max_tokens", 256)
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
val request = Request.Builder()
|
| 190 |
+
.url("http://127.0.0.1:8000/api/v1/summarize/")
|
| 191 |
+
.post(json.toString().toRequestBody("application/json".toMediaType()))
|
| 192 |
+
.build()
|
| 193 |
+
|
| 194 |
+
client.newCall(request).execute().use { response ->
|
| 195 |
+
val result = response.body?.string()
|
| 196 |
+
// Handle response
|
| 197 |
+
}
|
| 198 |
+
```
|
| 199 |
+
|
| 200 |
+
## Development
|
| 201 |
+
|
| 202 |
+
### Running Tests
|
| 203 |
+
|
| 204 |
+
```bash
|
| 205 |
+
# Run all tests
|
| 206 |
+
pytest
|
| 207 |
+
|
| 208 |
+
# Run with coverage
|
| 209 |
+
pytest --cov=app --cov-report=html --cov-report=term
|
| 210 |
+
|
| 211 |
+
# Run specific test file
|
| 212 |
+
pytest tests/test_api.py -v
|
| 213 |
+
|
| 214 |
+
# Run tests and stop on first failure
|
| 215 |
+
pytest -x
|
| 216 |
+
```
|
| 217 |
+
|
| 218 |
+
### Code Quality
|
| 219 |
+
|
| 220 |
+
```bash
|
| 221 |
+
# Format code
|
| 222 |
+
black app/ tests/
|
| 223 |
+
|
| 224 |
+
# Sort imports
|
| 225 |
+
isort app/ tests/
|
| 226 |
+
|
| 227 |
+
# Lint code
|
| 228 |
+
flake8 app/ tests/
|
| 229 |
+
```
|
| 230 |
+
|
| 231 |
+
### Project Structure
|
| 232 |
+
|
| 233 |
+
```
|
| 234 |
+
app/
|
| 235 |
+
βββ main.py # FastAPI app entry point
|
| 236 |
+
βββ api/
|
| 237 |
+
β βββ v1/
|
| 238 |
+
β βββ routes.py # API route definitions
|
| 239 |
+
β βββ schemas.py # Pydantic models
|
| 240 |
+
β βββ summarize.py # Summarization endpoint
|
| 241 |
+
βββ services/
|
| 242 |
+
β βββ summarizer.py # Ollama integration
|
| 243 |
+
βββ core/
|
| 244 |
+
βββ config.py # Configuration management
|
| 245 |
+
βββ logging.py # Logging setup
|
| 246 |
+
βββ middleware.py # Request middleware
|
| 247 |
+
βββ errors.py # Error handling
|
| 248 |
+
tests/
|
| 249 |
+
βββ test_api.py # API endpoint tests
|
| 250 |
+
βββ test_services.py # Service layer tests
|
| 251 |
+
βββ test_schemas.py # Pydantic model tests
|
| 252 |
+
βββ test_config.py # Configuration tests
|
| 253 |
+
βββ conftest.py # Test configuration
|
| 254 |
+
```
|
| 255 |
+
|
| 256 |
+
## Docker Deployment
|
| 257 |
+
|
| 258 |
+
### Local Development with Docker
|
| 259 |
+
|
| 260 |
+
```bash
|
| 261 |
+
# Build and run with docker-compose
|
| 262 |
+
docker-compose up --build
|
| 263 |
+
|
| 264 |
+
# Or build manually
|
| 265 |
+
docker build -t summarizer-backend .
|
| 266 |
+
docker run -p 8000:8000 summarizer-backend
|
| 267 |
+
```
|
| 268 |
+
|
| 269 |
+
### Production Deployment
|
| 270 |
+
|
| 271 |
+
1. **Build the image**
|
| 272 |
+
```bash
|
| 273 |
+
docker build -t your-registry/summarizer-backend:latest .
|
| 274 |
+
```
|
| 275 |
+
|
| 276 |
+
2. **Deploy to cloud**
|
| 277 |
+
```bash
|
| 278 |
+
# Push to registry
|
| 279 |
+
docker push your-registry/summarizer-backend:latest
|
| 280 |
+
|
| 281 |
+
# Deploy to your cloud provider
|
| 282 |
+
# (AWS ECS, Google Cloud Run, Azure Container Instances, etc.)
|
| 283 |
+
```
|
| 284 |
+
|
| 285 |
+
## Cloud Deployment Options
|
| 286 |
+
|
| 287 |
+
### Railway
|
| 288 |
+
```bash
|
| 289 |
+
# Install Railway CLI
|
| 290 |
+
npm install -g @railway/cli
|
| 291 |
+
|
| 292 |
+
# Deploy
|
| 293 |
+
railway login
|
| 294 |
+
railway init
|
| 295 |
+
railway up
|
| 296 |
+
```
|
| 297 |
+
|
| 298 |
+
### Render
|
| 299 |
+
1. Connect your GitHub repository
|
| 300 |
+
2. Set environment variables
|
| 301 |
+
3. Deploy automatically on push
|
| 302 |
+
|
| 303 |
+
### AWS ECS
|
| 304 |
+
```bash
|
| 305 |
+
# Build and push to ECR
|
| 306 |
+
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin your-account.dkr.ecr.us-east-1.amazonaws.com
|
| 307 |
+
docker tag summarizer-backend:latest your-account.dkr.ecr.us-east-1.amazonaws.com/summarizer-backend:latest
|
| 308 |
+
docker push your-account.dkr.ecr.us-east-1.amazonaws.com/summarizer-backend:latest
|
| 309 |
+
```
|
| 310 |
+
|
| 311 |
+
## Monitoring and Logging
|
| 312 |
+
|
| 313 |
+
### Request Tracking
|
| 314 |
+
Every request gets a unique ID for tracking:
|
| 315 |
+
```bash
|
| 316 |
+
curl -H "X-Request-ID: my-custom-id" http://127.0.0.1:8000/api/v1/summarize/ \
|
| 317 |
+
-d '{"text": "test"}'
|
| 318 |
+
```
|
| 319 |
+
|
| 320 |
+
### Log Format
|
| 321 |
+
```
|
| 322 |
+
2025-09-29 20:47:46,949 - app.core.middleware - INFO - Request abc123: POST /api/v1/summarize/
|
| 323 |
+
2025-09-29 20:47:46,987 - app.core.middleware - INFO - Response abc123: 200 (38.48ms)
|
| 324 |
+
```
|
| 325 |
+
|
| 326 |
+
## Performance Considerations
|
| 327 |
+
|
| 328 |
+
### Model Selection
|
| 329 |
+
- **llama3.1:8b** - Good balance of speed and quality
|
| 330 |
+
- **mistral:7b** - Faster, good for real-time apps
|
| 331 |
+
- **llama3.1:70b** - Higher quality, slower inference
|
| 332 |
+
|
| 333 |
+
### Optimization Tips
|
| 334 |
+
1. **Batch requests** when possible
|
| 335 |
+
2. **Cache summaries** for repeated content
|
| 336 |
+
3. **Use appropriate max_tokens** (256-512 for most use cases)
|
| 337 |
+
4. **Monitor latency** and adjust timeout settings
|
| 338 |
+
|
| 339 |
+
## Troubleshooting
|
| 340 |
+
|
| 341 |
+
### Common Issues
|
| 342 |
+
|
| 343 |
+
**Ollama connection failed**
|
| 344 |
+
```bash
|
| 345 |
+
# Check if Ollama is running
|
| 346 |
+
curl http://127.0.0.1:11434/api/tags
|
| 347 |
+
|
| 348 |
+
# Restart Ollama
|
| 349 |
+
ollama serve
|
| 350 |
+
```
|
| 351 |
+
|
| 352 |
+
**Model not found**
|
| 353 |
+
```bash
|
| 354 |
+
# List available models
|
| 355 |
+
ollama list
|
| 356 |
+
|
| 357 |
+
# Pull the required model
|
| 358 |
+
ollama pull llama3.1:8b
|
| 359 |
+
```
|
| 360 |
+
|
| 361 |
+
**Port already in use**
|
| 362 |
+
```bash
|
| 363 |
+
# Use a different port
|
| 364 |
+
uvicorn app.main:app --port 8001
|
| 365 |
+
```
|
| 366 |
+
|
| 367 |
+
### Debug Mode
|
| 368 |
+
```bash
|
| 369 |
+
# Enable debug logging
|
| 370 |
+
export LOG_LEVEL=DEBUG
|
| 371 |
+
uvicorn app.main:app --reload
|
| 372 |
+
```
|
| 373 |
+
|
| 374 |
+
## Contributing
|
| 375 |
+
|
| 376 |
+
1. Fork the repository
|
| 377 |
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
| 378 |
+
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
| 379 |
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
| 380 |
+
5. Open a Pull Request
|
| 381 |
+
|
| 382 |
+
## License
|
| 383 |
+
|
| 384 |
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
| 385 |
+
|
| 386 |
+
## Support
|
| 387 |
+
|
| 388 |
+
- π§ **Email**: [your-email@example.com]
|
| 389 |
+
- π **Issues**: [GitHub Issues](https://github.com/MingLu0/SummarizerBackend/issues)
|
| 390 |
+
- π **Documentation**: [API Docs](http://127.0.0.1:8000/docs)
|
| 391 |
+
|
| 392 |
+
---
|
| 393 |
+
|
| 394 |
+
**Built with β€οΈ for privacy-first text summarization**
|