Spaces:
Running
Running
ming
commited on
Commit
Β·
035a845
1
Parent(s):
76c1e68
feat: Add comprehensive configuration management and startup automation
Browse files- Add .env file with correct default configurations
- Create automated startup scripts (start-server.sh, start-server.bat)
- Add Ollama connectivity validation on server startup
- Update README with improved setup instructions and troubleshooting
- Prevent common configuration issues (wrong host, model mismatch, port conflicts)
- Add cross-platform support for macOS/Linux and Windows
Fixes issues with:
- Ollama host resolution (ollama:11434 -> 127.0.0.1:11434)
- Model availability (llama3.1:8b -> llama3.2:latest)
- Port conflicts and startup automation
- README.md +35 -2
- app/main.py +15 -0
- start-server.bat +54 -0
- start-server.sh +71 -0
README.md
CHANGED
|
@@ -50,9 +50,42 @@ A FastAPI-based backend service for text summarization using Ollama's local lang
|
|
| 50 |
pip install -r requirements.txt
|
| 51 |
```
|
| 52 |
|
| 53 |
-
4. **
|
| 54 |
```bash
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
```
|
| 57 |
|
| 58 |
5. **Test the API**
|
|
|
|
| 50 |
pip install -r requirements.txt
|
| 51 |
```
|
| 52 |
|
| 53 |
+
4. **Start the server (Recommended)**
|
| 54 |
```bash
|
| 55 |
+
# Use the automated startup script (checks everything for you)
|
| 56 |
+
./start-server.sh
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
**OR manually:**
|
| 60 |
+
```bash
|
| 61 |
+
# Start the server manually
|
| 62 |
+
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
## Configuration
|
| 66 |
+
|
| 67 |
+
The server uses environment variables for configuration. A `.env` file is automatically created with sensible defaults:
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
# Ollama Configuration
|
| 71 |
+
OLLAMA_HOST=http://127.0.0.1:11434
|
| 72 |
+
OLLAMA_MODEL=llama3.2:latest
|
| 73 |
+
OLLAMA_TIMEOUT=30
|
| 74 |
+
|
| 75 |
+
# Server Configuration
|
| 76 |
+
SERVER_HOST=0.0.0.0
|
| 77 |
+
SERVER_PORT=8000
|
| 78 |
+
LOG_LEVEL=INFO
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
**Common Issues & Solutions:**
|
| 82 |
+
|
| 83 |
+
- **Port already in use**: The startup script automatically handles this
|
| 84 |
+
- **Ollama connection failed**: Ensure Ollama is running (`ollama serve`)
|
| 85 |
+
- **Model not found**: Install the model (`ollama pull llama3.2:latest`)
|
| 86 |
+
- **Wrong host configuration**: The `.env` file ensures correct localhost settings
|
| 87 |
+
|
| 88 |
+
## API Usage
|
| 89 |
```
|
| 90 |
|
| 91 |
5. **Test the API**
|
app/main.py
CHANGED
|
@@ -9,6 +9,7 @@ from app.core.logging import setup_logging, get_logger
|
|
| 9 |
from app.api.v1.routes import api_router
|
| 10 |
from app.core.middleware import request_context_middleware
|
| 11 |
from app.core.errors import init_exception_handlers
|
|
|
|
| 12 |
|
| 13 |
# Set up logging
|
| 14 |
setup_logging()
|
|
@@ -48,6 +49,20 @@ async def startup_event():
|
|
| 48 |
logger.info("Starting Text Summarizer API")
|
| 49 |
logger.info(f"Ollama host: {settings.ollama_host}")
|
| 50 |
logger.info(f"Ollama model: {settings.ollama_model}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
|
| 53 |
@app.on_event("shutdown")
|
|
|
|
| 9 |
from app.api.v1.routes import api_router
|
| 10 |
from app.core.middleware import request_context_middleware
|
| 11 |
from app.core.errors import init_exception_handlers
|
| 12 |
+
from app.services.summarizer import ollama_service
|
| 13 |
|
| 14 |
# Set up logging
|
| 15 |
setup_logging()
|
|
|
|
| 49 |
logger.info("Starting Text Summarizer API")
|
| 50 |
logger.info(f"Ollama host: {settings.ollama_host}")
|
| 51 |
logger.info(f"Ollama model: {settings.ollama_model}")
|
| 52 |
+
|
| 53 |
+
# Validate Ollama connectivity
|
| 54 |
+
try:
|
| 55 |
+
is_healthy = await ollama_service.check_health()
|
| 56 |
+
if is_healthy:
|
| 57 |
+
logger.info("β
Ollama service is accessible and healthy")
|
| 58 |
+
else:
|
| 59 |
+
logger.warning("β οΈ Ollama service is not responding properly")
|
| 60 |
+
logger.warning(f" Please ensure Ollama is running at {settings.ollama_host}")
|
| 61 |
+
logger.warning(f" And that model '{settings.ollama_model}' is available")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logger.error(f"β Failed to connect to Ollama: {e}")
|
| 64 |
+
logger.error(f" Please check that Ollama is running at {settings.ollama_host}")
|
| 65 |
+
logger.error(f" And that model '{settings.ollama_model}' is installed")
|
| 66 |
|
| 67 |
|
| 68 |
@app.on_event("shutdown")
|
start-server.bat
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
@echo off
|
| 2 |
+
REM Text Summarizer API Startup Script for Windows
|
| 3 |
+
REM This script ensures the server starts with the correct configuration
|
| 4 |
+
|
| 5 |
+
echo π Starting Text Summarizer API Server...
|
| 6 |
+
|
| 7 |
+
REM Check if .env file exists, if not create it with defaults
|
| 8 |
+
if not exist .env (
|
| 9 |
+
echo π Creating .env file with default configuration...
|
| 10 |
+
(
|
| 11 |
+
echo # Text Summarizer API Configuration
|
| 12 |
+
echo OLLAMA_HOST=http://127.0.0.1:11434
|
| 13 |
+
echo OLLAMA_MODEL=llama3.2:latest
|
| 14 |
+
echo OLLAMA_TIMEOUT=30
|
| 15 |
+
echo SERVER_HOST=0.0.0.0
|
| 16 |
+
echo SERVER_PORT=8000
|
| 17 |
+
echo LOG_LEVEL=INFO
|
| 18 |
+
echo API_KEY_ENABLED=false
|
| 19 |
+
echo RATE_LIMIT_ENABLED=false
|
| 20 |
+
echo RATE_LIMIT_REQUESTS=60
|
| 21 |
+
echo RATE_LIMIT_WINDOW=60
|
| 22 |
+
echo MAX_TEXT_LENGTH=32000
|
| 23 |
+
echo MAX_TOKENS_DEFAULT=256
|
| 24 |
+
) > .env
|
| 25 |
+
echo β
.env file created with default values
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
REM Check if Ollama is running
|
| 29 |
+
echo π Checking Ollama service...
|
| 30 |
+
curl -s http://127.0.0.1:11434/api/tags >nul 2>&1
|
| 31 |
+
if %errorlevel% equ 0 (
|
| 32 |
+
echo β
Ollama is running and accessible
|
| 33 |
+
) else (
|
| 34 |
+
echo β Ollama is not running or not accessible at http://127.0.0.1:11434
|
| 35 |
+
echo Please start Ollama first:
|
| 36 |
+
echo - Download and install Ollama from https://ollama.ai
|
| 37 |
+
echo - Start the Ollama application
|
| 38 |
+
pause
|
| 39 |
+
exit /b 1
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
REM Start the server
|
| 43 |
+
echo π Starting FastAPI server...
|
| 44 |
+
echo Server will be available at: http://localhost:8000
|
| 45 |
+
echo API docs will be available at: http://localhost:8000/docs
|
| 46 |
+
echo Press Ctrl+C to stop the server
|
| 47 |
+
echo.
|
| 48 |
+
|
| 49 |
+
REM Load environment variables and start uvicorn
|
| 50 |
+
for /f "usebackq tokens=1,2 delims==" %%a in (.env) do (
|
| 51 |
+
if not "%%a"=="" if not "%%a:~0,1%"=="#" set %%a=%%b
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
uvicorn app.main:app --host %SERVER_HOST% --port %SERVER_PORT% --reload
|
start-server.sh
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Text Summarizer API Startup Script
|
| 4 |
+
# This script ensures the server starts with the correct configuration
|
| 5 |
+
|
| 6 |
+
set -e # Exit on any error
|
| 7 |
+
|
| 8 |
+
echo "π Starting Text Summarizer API Server..."
|
| 9 |
+
|
| 10 |
+
# Check if .env file exists, if not create it with defaults
|
| 11 |
+
if [ ! -f .env ]; then
|
| 12 |
+
echo "π Creating .env file with default configuration..."
|
| 13 |
+
cat > .env << 'EOF'
|
| 14 |
+
# Text Summarizer API Configuration
|
| 15 |
+
OLLAMA_HOST=http://127.0.0.1:11434
|
| 16 |
+
OLLAMA_MODEL=llama3.2:latest
|
| 17 |
+
OLLAMA_TIMEOUT=30
|
| 18 |
+
SERVER_HOST=0.0.0.0
|
| 19 |
+
SERVER_PORT=8000
|
| 20 |
+
LOG_LEVEL=INFO
|
| 21 |
+
API_KEY_ENABLED=false
|
| 22 |
+
RATE_LIMIT_ENABLED=false
|
| 23 |
+
RATE_LIMIT_REQUESTS=60
|
| 24 |
+
RATE_LIMIT_WINDOW=60
|
| 25 |
+
MAX_TEXT_LENGTH=32000
|
| 26 |
+
MAX_TOKENS_DEFAULT=256
|
| 27 |
+
EOF
|
| 28 |
+
echo "β
.env file created with default values"
|
| 29 |
+
fi
|
| 30 |
+
|
| 31 |
+
# Check if Ollama is running
|
| 32 |
+
echo "π Checking Ollama service..."
|
| 33 |
+
if curl -s http://127.0.0.1:11434/api/tags > /dev/null 2>&1; then
|
| 34 |
+
echo "β
Ollama is running and accessible"
|
| 35 |
+
|
| 36 |
+
# Check if the configured model is available
|
| 37 |
+
MODEL=$(grep OLLAMA_MODEL .env | cut -d'=' -f2)
|
| 38 |
+
if curl -s http://127.0.0.1:11434/api/tags | grep -q "\"$MODEL\""; then
|
| 39 |
+
echo "β
Model '$MODEL' is available"
|
| 40 |
+
else
|
| 41 |
+
echo "β οΈ Warning: Model '$MODEL' not found in Ollama"
|
| 42 |
+
echo " Available models:"
|
| 43 |
+
curl -s http://127.0.0.1:11434/api/tags | grep -o '"name":"[^"]*"' | sed 's/"name":"//g' | sed 's/"//g' | sed 's/^/ - /'
|
| 44 |
+
fi
|
| 45 |
+
else
|
| 46 |
+
echo "β Ollama is not running or not accessible at http://127.0.0.1:11434"
|
| 47 |
+
echo " Please start Ollama first:"
|
| 48 |
+
echo " - On macOS: Open Ollama app or run 'ollama serve'"
|
| 49 |
+
echo " - On Linux: run 'ollama serve'"
|
| 50 |
+
echo " - On Windows: Open Ollama app"
|
| 51 |
+
exit 1
|
| 52 |
+
fi
|
| 53 |
+
|
| 54 |
+
# Kill any existing server on the configured port
|
| 55 |
+
PORT=$(grep SERVER_PORT .env | cut -d'=' -f2)
|
| 56 |
+
if lsof -i :$PORT > /dev/null 2>&1; then
|
| 57 |
+
echo "π Stopping existing server on port $PORT..."
|
| 58 |
+
pkill -f "uvicorn.*$PORT" || true
|
| 59 |
+
sleep 2
|
| 60 |
+
fi
|
| 61 |
+
|
| 62 |
+
# Start the server
|
| 63 |
+
echo "π Starting FastAPI server..."
|
| 64 |
+
echo " Server will be available at: http://localhost:$PORT"
|
| 65 |
+
echo " API docs will be available at: http://localhost:$PORT/docs"
|
| 66 |
+
echo " Press Ctrl+C to stop the server"
|
| 67 |
+
echo ""
|
| 68 |
+
|
| 69 |
+
# Load environment variables and start uvicorn
|
| 70 |
+
export $(cat .env | grep -v '^#' | xargs)
|
| 71 |
+
uvicorn app.main:app --host $SERVER_HOST --port $SERVER_PORT --reload
|