Spaces:
Running
Running
ming
commited on
Commit
Β·
bd3417a
1
Parent(s):
c153455
Fix Ollama permissions and model configuration for Hugging Face deployment
Browse files- Dockerfile +13 -9
- README_HF.md +170 -0
- app/core/config.py +2 -2
Dockerfile
CHANGED
|
@@ -21,6 +21,9 @@ RUN apt-get update \
|
|
| 21 |
# Install Ollama
|
| 22 |
RUN curl -fsSL https://ollama.ai/install.sh | sh
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
# Copy requirements first for better caching
|
| 25 |
COPY requirements.txt .
|
| 26 |
|
|
@@ -32,30 +35,31 @@ RUN pip install --no-cache-dir --upgrade pip \
|
|
| 32 |
COPY app/ ./app/
|
| 33 |
COPY pytest.ini .
|
| 34 |
|
| 35 |
-
# Create non-root user for security
|
| 36 |
-
RUN groupadd -r appuser && useradd -r -g appuser appuser \
|
| 37 |
-
&& chown -R appuser:appuser /app
|
| 38 |
-
|
| 39 |
# Create startup script
|
| 40 |
RUN echo '#!/bin/bash\n\
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
# Start Ollama in background\n\
|
|
|
|
| 42 |
ollama serve &\n\
|
| 43 |
\n\
|
| 44 |
# Wait for Ollama to be ready\n\
|
| 45 |
echo "Waiting for Ollama to start..."\n\
|
| 46 |
-
sleep
|
| 47 |
\n\
|
| 48 |
# Pull the model (this will take a few minutes on first run)\n\
|
| 49 |
-
echo "Pulling model..."\n\
|
| 50 |
ollama pull mistral:7b\n\
|
| 51 |
\n\
|
| 52 |
# Start the FastAPI app\n\
|
| 53 |
echo "Starting FastAPI app..."\n\
|
| 54 |
exec uvicorn app.main:app --host 0.0.0.0 --port 7860' > /app/start.sh \
|
| 55 |
-
&& chmod +x /app/start.sh
|
| 56 |
-
&& chown appuser:appuser /app/start.sh
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
|
| 60 |
# Expose port (Hugging Face Spaces uses port 7860)
|
| 61 |
EXPOSE 7860
|
|
|
|
| 21 |
# Install Ollama
|
| 22 |
RUN curl -fsSL https://ollama.ai/install.sh | sh
|
| 23 |
|
| 24 |
+
# Create Ollama directory with proper permissions
|
| 25 |
+
RUN mkdir -p /root/.ollama && chmod 755 /root/.ollama
|
| 26 |
+
|
| 27 |
# Copy requirements first for better caching
|
| 28 |
COPY requirements.txt .
|
| 29 |
|
|
|
|
| 35 |
COPY app/ ./app/
|
| 36 |
COPY pytest.ini .
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Create startup script
|
| 39 |
RUN echo '#!/bin/bash\n\
|
| 40 |
+
# Set Ollama environment\n\
|
| 41 |
+
export OLLAMA_HOST=0.0.0.0:11434\n\
|
| 42 |
+
export OLLAMA_ORIGINS=*\n\
|
| 43 |
+
\n\
|
| 44 |
# Start Ollama in background\n\
|
| 45 |
+
echo "Starting Ollama server..."\n\
|
| 46 |
ollama serve &\n\
|
| 47 |
\n\
|
| 48 |
# Wait for Ollama to be ready\n\
|
| 49 |
echo "Waiting for Ollama to start..."\n\
|
| 50 |
+
sleep 15\n\
|
| 51 |
\n\
|
| 52 |
# Pull the model (this will take a few minutes on first run)\n\
|
| 53 |
+
echo "Pulling model mistral:7b..."\n\
|
| 54 |
ollama pull mistral:7b\n\
|
| 55 |
\n\
|
| 56 |
# Start the FastAPI app\n\
|
| 57 |
echo "Starting FastAPI app..."\n\
|
| 58 |
exec uvicorn app.main:app --host 0.0.0.0 --port 7860' > /app/start.sh \
|
| 59 |
+
&& chmod +x /app/start.sh
|
|
|
|
| 60 |
|
| 61 |
+
# Run as root to avoid permission issues with Ollama
|
| 62 |
+
# USER appuser
|
| 63 |
|
| 64 |
# Expose port (Hugging Face Spaces uses port 7860)
|
| 65 |
EXPOSE 7860
|
README_HF.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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false
|
| 8 |
+
license: mit
|
| 9 |
+
app_port: 7860
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Text Summarizer API
|
| 13 |
+
|
| 14 |
+
A FastAPI-based text summarization service powered by Ollama and Mistral 7B model.
|
| 15 |
+
|
| 16 |
+
## π Features
|
| 17 |
+
|
| 18 |
+
- **Fast text summarization** using local LLM inference
|
| 19 |
+
- **RESTful API** with FastAPI
|
| 20 |
+
- **Health monitoring** and logging
|
| 21 |
+
- **Docker containerized** for easy deployment
|
| 22 |
+
- **Free deployment** on Hugging Face Spaces
|
| 23 |
+
|
| 24 |
+
## π‘ API Endpoints
|
| 25 |
+
|
| 26 |
+
### Health Check
|
| 27 |
+
```
|
| 28 |
+
GET /health
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
### Summarize Text
|
| 32 |
+
```
|
| 33 |
+
POST /api/v1/summarize
|
| 34 |
+
Content-Type: application/json
|
| 35 |
+
|
| 36 |
+
{
|
| 37 |
+
"text": "Your long text to summarize here...",
|
| 38 |
+
"max_tokens": 256,
|
| 39 |
+
"temperature": 0.7
|
| 40 |
+
}
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### API Documentation
|
| 44 |
+
- **Swagger UI**: `/docs`
|
| 45 |
+
- **ReDoc**: `/redoc`
|
| 46 |
+
|
| 47 |
+
## π§ Configuration
|
| 48 |
+
|
| 49 |
+
The service uses the following environment variables:
|
| 50 |
+
|
| 51 |
+
- `OLLAMA_MODEL`: Model to use (default: `mistral:7b`)
|
| 52 |
+
- `OLLAMA_HOST`: Ollama service host (default: `http://localhost:11434`)
|
| 53 |
+
- `OLLAMA_TIMEOUT`: Request timeout in seconds (default: `30`)
|
| 54 |
+
- `SERVER_HOST`: Server host (default: `0.0.0.0`)
|
| 55 |
+
- `SERVER_PORT`: Server port (default: `7860`)
|
| 56 |
+
- `LOG_LEVEL`: Logging level (default: `INFO`)
|
| 57 |
+
|
| 58 |
+
## π³ Docker Deployment
|
| 59 |
+
|
| 60 |
+
### Local Development
|
| 61 |
+
```bash
|
| 62 |
+
# Build and run with docker-compose
|
| 63 |
+
docker-compose up --build
|
| 64 |
+
|
| 65 |
+
# Or run directly
|
| 66 |
+
docker build -f Dockerfile.hf -t summarizer-app .
|
| 67 |
+
docker run -p 7860:7860 summarizer-app
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
### Hugging Face Spaces
|
| 71 |
+
This app is configured for deployment on Hugging Face Spaces using Docker SDK.
|
| 72 |
+
|
| 73 |
+
## π Performance
|
| 74 |
+
|
| 75 |
+
- **Model**: Mistral 7B (7GB RAM requirement)
|
| 76 |
+
- **Startup time**: ~2-3 minutes (includes model download)
|
| 77 |
+
- **Inference speed**: ~2-5 seconds per request
|
| 78 |
+
- **Memory usage**: ~8GB RAM
|
| 79 |
+
|
| 80 |
+
## π οΈ Development
|
| 81 |
+
|
| 82 |
+
### Setup
|
| 83 |
+
```bash
|
| 84 |
+
# Install dependencies
|
| 85 |
+
pip install -r requirements.txt
|
| 86 |
+
|
| 87 |
+
# Run locally
|
| 88 |
+
uvicorn app.main:app --host 0.0.0.0 --port 7860
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### Testing
|
| 92 |
+
```bash
|
| 93 |
+
# Run tests
|
| 94 |
+
pytest
|
| 95 |
+
|
| 96 |
+
# Run with coverage
|
| 97 |
+
pytest --cov=app
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## π Usage Examples
|
| 101 |
+
|
| 102 |
+
### Python
|
| 103 |
+
```python
|
| 104 |
+
import requests
|
| 105 |
+
|
| 106 |
+
# Summarize text
|
| 107 |
+
response = requests.post(
|
| 108 |
+
"https://your-space.hf.space/api/v1/summarize",
|
| 109 |
+
json={
|
| 110 |
+
"text": "Your long article or text here...",
|
| 111 |
+
"max_tokens": 256
|
| 112 |
+
}
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
result = response.json()
|
| 116 |
+
print(result["summary"])
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
### cURL
|
| 120 |
+
```bash
|
| 121 |
+
curl -X POST "https://your-space.hf.space/api/v1/summarize" \
|
| 122 |
+
-H "Content-Type: application/json" \
|
| 123 |
+
-d '{
|
| 124 |
+
"text": "Your text to summarize...",
|
| 125 |
+
"max_tokens": 256
|
| 126 |
+
}'
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
## π Security
|
| 130 |
+
|
| 131 |
+
- Non-root user execution
|
| 132 |
+
- Input validation and sanitization
|
| 133 |
+
- Rate limiting (configurable)
|
| 134 |
+
- API key authentication (optional)
|
| 135 |
+
|
| 136 |
+
## π Monitoring
|
| 137 |
+
|
| 138 |
+
The service includes:
|
| 139 |
+
- Health check endpoint
|
| 140 |
+
- Request logging
|
| 141 |
+
- Error tracking
|
| 142 |
+
- Performance metrics
|
| 143 |
+
|
| 144 |
+
## π Troubleshooting
|
| 145 |
+
|
| 146 |
+
### Common Issues
|
| 147 |
+
|
| 148 |
+
1. **Model not loading**: Check if Ollama is running and model is pulled
|
| 149 |
+
2. **Out of memory**: Ensure sufficient RAM (8GB+) for Mistral 7B
|
| 150 |
+
3. **Slow startup**: Normal on first run due to model download
|
| 151 |
+
4. **API errors**: Check logs via `/docs` endpoint
|
| 152 |
+
|
| 153 |
+
### Logs
|
| 154 |
+
View application logs in the Hugging Face Spaces interface or check the health endpoint for service status.
|
| 155 |
+
|
| 156 |
+
## π License
|
| 157 |
+
|
| 158 |
+
MIT License - see LICENSE file for details.
|
| 159 |
+
|
| 160 |
+
## π€ Contributing
|
| 161 |
+
|
| 162 |
+
1. Fork the repository
|
| 163 |
+
2. Create a feature branch
|
| 164 |
+
3. Make your changes
|
| 165 |
+
4. Add tests
|
| 166 |
+
5. Submit a pull request
|
| 167 |
+
|
| 168 |
+
---
|
| 169 |
+
|
| 170 |
+
**Deployed on Hugging Face Spaces** π
|
app/core/config.py
CHANGED
|
@@ -11,8 +11,8 @@ class Settings(BaseSettings):
|
|
| 11 |
"""Application settings loaded from environment variables."""
|
| 12 |
|
| 13 |
# Ollama Configuration
|
| 14 |
-
ollama_model: str = Field(default="
|
| 15 |
-
ollama_host: str = Field(default="http://
|
| 16 |
ollama_timeout: int = Field(default=60, env="OLLAMA_TIMEOUT", ge=1)
|
| 17 |
|
| 18 |
# Server Configuration
|
|
|
|
| 11 |
"""Application settings loaded from environment variables."""
|
| 12 |
|
| 13 |
# Ollama Configuration
|
| 14 |
+
ollama_model: str = Field(default="mistral:7b", env="OLLAMA_MODEL")
|
| 15 |
+
ollama_host: str = Field(default="http://localhost:11434", env="OLLAMA_HOST")
|
| 16 |
ollama_timeout: int = Field(default=60, env="OLLAMA_TIMEOUT", ge=1)
|
| 17 |
|
| 18 |
# Server Configuration
|