Spaces:
Sleeping
Sleeping
dhruv575 commited on
Commit ·
ada5d3f
1
Parent(s): be84080
Setup and more markdown
Browse files- README.md +29 -14
- REDIS_SETUP.md +85 -0
- env.example +9 -2
- setup_env.py +44 -6
README.md
CHANGED
|
@@ -13,6 +13,11 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
| 13 |
|
| 14 |
This is the backend for the Enflow application, which allows law enforcement agencies to build, maintain, and use specific workflows that automate tasks based on officers' daily logs.
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
## Environment Setup
|
| 17 |
|
| 18 |
The backend requires several environment variables to function properly. These can be set up in a `.env` file in the root of the backend directory.
|
|
@@ -32,10 +37,17 @@ Then edit the `.env` file with your actual credentials:
|
|
| 32 |
- `CLOUDINARY_CLOUD_NAME`: Cloudinary cloud name for file storage
|
| 33 |
- `CLOUDINARY_API_KEY`: Cloudinary API key
|
| 34 |
- `CLOUDINARY_API_SECRET`: Cloudinary API secret
|
| 35 |
-
- `
|
|
|
|
|
|
|
| 36 |
- `OPENAI_API_KEY`: OpenAI API key for LLM processing
|
| 37 |
- `FLASK_ENV`: Set to "development" or "production"
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
### Important Security Notes
|
| 40 |
|
| 41 |
- **Never commit the `.env` file to version control**
|
|
@@ -67,7 +79,7 @@ The API endpoints are organized by resource type:
|
|
| 67 |
- `/api/logs`: Log upload and management
|
| 68 |
- `/api/incidents`: Incident management
|
| 69 |
|
| 70 |
-
For detailed API documentation, see the
|
| 71 |
|
| 72 |
## Setup Instructions
|
| 73 |
|
|
@@ -76,6 +88,7 @@ For detailed API documentation, see the API documentation section.
|
|
| 76 |
- Python 3.10 or newer
|
| 77 |
- Docker and Docker Compose (optional, for containerized deployment)
|
| 78 |
- MongoDB (we're using MongoDB Atlas in the current setup)
|
|
|
|
| 79 |
|
| 80 |
### Environment Setup
|
| 81 |
|
|
@@ -93,6 +106,9 @@ For detailed API documentation, see the API documentation section.
|
|
| 93 |
CLOUDINARY_API_KEY=your_cloudinary_api_key
|
| 94 |
CLOUDINARY_API_SECRET=your_cloudinary_api_secret
|
| 95 |
OPENAI_API_KEY=your_openai_api_key
|
|
|
|
|
|
|
|
|
|
| 96 |
FLASK_ENV=development
|
| 97 |
```
|
| 98 |
|
|
@@ -127,20 +143,15 @@ For detailed API documentation, see the API documentation section.
|
|
| 127 |
|
| 128 |
### HuggingFace Deployment
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
2. Update the environment variables in your HuggingFace Space settings:
|
| 133 |
-
- Go to the Space settings
|
| 134 |
-
- Add all the required environment variables (MONGO_URI, JWT_SECRET, etc.)
|
| 135 |
-
|
| 136 |
-
3. Before deploying, you can test if the deployment will work:
|
| 137 |
-
```
|
| 138 |
-
python test_hf_deployment.py
|
| 139 |
-
```
|
| 140 |
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
## Project Structure
|
| 146 |
|
|
@@ -150,9 +161,13 @@ For detailed API documentation, see the API documentation section.
|
|
| 150 |
- `controllers/` - Controller functions
|
| 151 |
- `routes/` - API route definitions
|
| 152 |
- `utils/` - Utility functions and middleware
|
|
|
|
|
|
|
| 153 |
- `setup_env.py` - Script to set up environment variables
|
| 154 |
- `test_department.py` - Test script for department creation
|
| 155 |
- `test_auth.py` - Test script for authentication
|
| 156 |
- `test_hf_deployment.py` - Test script for HuggingFace deployment
|
| 157 |
- `Dockerfile` - Docker configuration for containerized deployment
|
| 158 |
- `docker-compose.yml` - Docker Compose configuration for local development
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
This is the backend for the Enflow application, which allows law enforcement agencies to build, maintain, and use specific workflows that automate tasks based on officers' daily logs.
|
| 15 |
|
| 16 |
+
## Deployment Status
|
| 17 |
+
|
| 18 |
+
The backend is currently deployed and running at:
|
| 19 |
+
- **API Endpoint**: [https://huggingface.co/spaces/droov/enflow-api](https://huggingface.co/spaces/droov/enflow-api)
|
| 20 |
+
|
| 21 |
## Environment Setup
|
| 22 |
|
| 23 |
The backend requires several environment variables to function properly. These can be set up in a `.env` file in the root of the backend directory.
|
|
|
|
| 37 |
- `CLOUDINARY_CLOUD_NAME`: Cloudinary cloud name for file storage
|
| 38 |
- `CLOUDINARY_API_KEY`: Cloudinary API key
|
| 39 |
- `CLOUDINARY_API_SECRET`: Cloudinary API secret
|
| 40 |
+
- `REDIS_HOST`: Hostname or IP address of your Redis server
|
| 41 |
+
- `REDIS_PORT`: Redis port (default: 6379)
|
| 42 |
+
- `REDIS_PASSWORD`: Password for Redis authentication
|
| 43 |
- `OPENAI_API_KEY`: OpenAI API key for LLM processing
|
| 44 |
- `FLASK_ENV`: Set to "development" or "production"
|
| 45 |
|
| 46 |
+
Alternatively, you can set `REDIS_URL` directly as:
|
| 47 |
+
```
|
| 48 |
+
REDIS_URL=redis://:{password}@{host}:{port}/0
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
### Important Security Notes
|
| 52 |
|
| 53 |
- **Never commit the `.env` file to version control**
|
|
|
|
| 79 |
- `/api/logs`: Log upload and management
|
| 80 |
- `/api/incidents`: Incident management
|
| 81 |
|
| 82 |
+
For detailed API documentation, see the `API_DOCUMENTATION.md` file.
|
| 83 |
|
| 84 |
## Setup Instructions
|
| 85 |
|
|
|
|
| 88 |
- Python 3.10 or newer
|
| 89 |
- Docker and Docker Compose (optional, for containerized deployment)
|
| 90 |
- MongoDB (we're using MongoDB Atlas in the current setup)
|
| 91 |
+
- Redis server (for Celery task queue)
|
| 92 |
|
| 93 |
### Environment Setup
|
| 94 |
|
|
|
|
| 106 |
CLOUDINARY_API_KEY=your_cloudinary_api_key
|
| 107 |
CLOUDINARY_API_SECRET=your_cloudinary_api_secret
|
| 108 |
OPENAI_API_KEY=your_openai_api_key
|
| 109 |
+
REDIS_HOST=your_redis_host
|
| 110 |
+
REDIS_PORT=your_redis_port
|
| 111 |
+
REDIS_PASSWORD=your_redis_password
|
| 112 |
FLASK_ENV=development
|
| 113 |
```
|
| 114 |
|
|
|
|
| 143 |
|
| 144 |
### HuggingFace Deployment
|
| 145 |
|
| 146 |
+
The backend is already deployed to HuggingFace Spaces at: [https://huggingface.co/spaces/droov/enflow-api](https://huggingface.co/spaces/droov/enflow-api)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
For detailed deployment instructions, see the `hugginface_setup.md` file.
|
| 149 |
|
| 150 |
+
Key points for HuggingFace deployment:
|
| 151 |
+
1. Set your Space to use the Docker SDK
|
| 152 |
+
2. Add all environment variables in the Space settings
|
| 153 |
+
3. Make sure the Redis configuration is properly set up
|
| 154 |
+
4. The API will be available at your HuggingFace Space URL
|
| 155 |
|
| 156 |
## Project Structure
|
| 157 |
|
|
|
|
| 161 |
- `controllers/` - Controller functions
|
| 162 |
- `routes/` - API route definitions
|
| 163 |
- `utils/` - Utility functions and middleware
|
| 164 |
+
- `celery_tasks.py` - Celery task definitions
|
| 165 |
+
- `celery_config.py` - Celery configuration for task management
|
| 166 |
- `setup_env.py` - Script to set up environment variables
|
| 167 |
- `test_department.py` - Test script for department creation
|
| 168 |
- `test_auth.py` - Test script for authentication
|
| 169 |
- `test_hf_deployment.py` - Test script for HuggingFace deployment
|
| 170 |
- `Dockerfile` - Docker configuration for containerized deployment
|
| 171 |
- `docker-compose.yml` - Docker Compose configuration for local development
|
| 172 |
+
- `API_DOCUMENTATION.md` - Detailed API documentation
|
| 173 |
+
- `hugginface_setup.md` - Guide for deploying to HuggingFace Spaces
|
REDIS_SETUP.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Redis Configuration for Enflow
|
| 2 |
+
|
| 3 |
+
This document explains how to configure Redis for Enflow's background task processing with Celery.
|
| 4 |
+
|
| 5 |
+
## Overview
|
| 6 |
+
|
| 7 |
+
Enflow uses Redis as a message broker and result backend for Celery, which handles background tasks like:
|
| 8 |
+
- Processing PDF logs with OCR
|
| 9 |
+
- Running LLM analysis on log content
|
| 10 |
+
- Generating incident forms
|
| 11 |
+
|
| 12 |
+
## Configuration Options
|
| 13 |
+
|
| 14 |
+
You can configure Redis connection in two ways:
|
| 15 |
+
|
| 16 |
+
### Option 1: Using Individual Environment Variables
|
| 17 |
+
|
| 18 |
+
Set these three environment variables:
|
| 19 |
+
```
|
| 20 |
+
REDIS_HOST=your-redis-hostname-or-ip
|
| 21 |
+
REDIS_PORT=your-redis-port (default: 6379)
|
| 22 |
+
REDIS_PASSWORD=your-redis-password
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
### Option 2: Using a Single REDIS_URL
|
| 26 |
+
|
| 27 |
+
Alternatively, set a single `REDIS_URL` environment variable:
|
| 28 |
+
```
|
| 29 |
+
REDIS_URL=redis://:{password}@{host}:{port}/0
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
Example:
|
| 33 |
+
```
|
| 34 |
+
REDIS_URL=redis://:Nsdg@2314@2314@redis-12345.example.com:6379/0
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
## Configuration in Celery
|
| 38 |
+
|
| 39 |
+
The configuration is handled in `celery_config.py`, which constructs the Redis URL from individual components if they're available, or uses the provided `REDIS_URL` directly.
|
| 40 |
+
|
| 41 |
+
## Monitoring Celery Tasks
|
| 42 |
+
|
| 43 |
+
You can monitor Celery tasks in several ways:
|
| 44 |
+
|
| 45 |
+
1. **Celery Logs**: Run the Celery worker with higher verbosity:
|
| 46 |
+
```
|
| 47 |
+
celery -A utils.celery_tasks.celery_app worker --loglevel=info
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
2. **Flower (Optional)**: You can install and run Flower for a web-based monitoring UI:
|
| 51 |
+
```
|
| 52 |
+
pip install flower
|
| 53 |
+
celery -A utils.celery_tasks.celery_app flower
|
| 54 |
+
```
|
| 55 |
+
Access the Flower dashboard at http://localhost:5555
|
| 56 |
+
|
| 57 |
+
## Celery Task Configuration
|
| 58 |
+
|
| 59 |
+
Task-specific configurations are defined in `celery_config.py`, including:
|
| 60 |
+
|
| 61 |
+
- Rate limits for various tasks
|
| 62 |
+
- Task timeouts
|
| 63 |
+
- Queue routing
|
| 64 |
+
- Serialization formats
|
| 65 |
+
|
| 66 |
+
## Troubleshooting Redis Connection
|
| 67 |
+
|
| 68 |
+
If you encounter issues connecting to Redis:
|
| 69 |
+
|
| 70 |
+
1. Check if your Redis server is running
|
| 71 |
+
2. Verify the host, port, and password are correct
|
| 72 |
+
3. Check if any firewall is blocking the Redis port
|
| 73 |
+
4. Ensure Redis is configured to accept remote connections (if connecting remotely)
|
| 74 |
+
5. Try connecting using the redis-cli tool:
|
| 75 |
+
```
|
| 76 |
+
redis-cli -h your-host -p your-port -a your-password ping
|
| 77 |
+
```
|
| 78 |
+
You should receive "PONG" as the response if the connection is successful
|
| 79 |
+
|
| 80 |
+
## Security Considerations
|
| 81 |
+
|
| 82 |
+
1. Use a strong password for Redis authentication
|
| 83 |
+
2. Don't expose your Redis server directly to the internet
|
| 84 |
+
3. Consider using SSL/TLS if connecting to Redis over untrusted networks
|
| 85 |
+
4. Avoid hardcoding Redis credentials in your application code
|
env.example
CHANGED
|
@@ -9,8 +9,15 @@ CLOUDINARY_CLOUD_NAME=your_cloud_name
|
|
| 9 |
CLOUDINARY_API_KEY=your_api_key
|
| 10 |
CLOUDINARY_API_SECRET=your_api_secret
|
| 11 |
|
| 12 |
-
# Redis
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# OpenAI API key
|
| 16 |
OPENAI_API_KEY=your_openai_api_key
|
|
|
|
| 9 |
CLOUDINARY_API_KEY=your_api_key
|
| 10 |
CLOUDINARY_API_SECRET=your_api_secret
|
| 11 |
|
| 12 |
+
# Redis configuration
|
| 13 |
+
# Option 1: Individual components (preferred for better flexibility)
|
| 14 |
+
REDIS_HOST=your_redis_host
|
| 15 |
+
REDIS_PORT=6379
|
| 16 |
+
REDIS_PASSWORD=your_redis_password
|
| 17 |
+
|
| 18 |
+
# Option 2: Full Redis URL (alternative)
|
| 19 |
+
# If using this, you can comment out the individual components above
|
| 20 |
+
REDIS_URL=redis://:{password}@{host}:{port}/0
|
| 21 |
|
| 22 |
# OpenAI API key
|
| 23 |
OPENAI_API_KEY=your_openai_api_key
|
setup_env.py
CHANGED
|
@@ -90,12 +90,50 @@ def main():
|
|
| 90 |
elif 'CLOUDINARY_API_SECRET' not in env_vars:
|
| 91 |
env_vars['CLOUDINARY_API_SECRET'] = "your_api_secret"
|
| 92 |
|
| 93 |
-
# Redis
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
# OpenAI API Key
|
| 101 |
openai_key = getpass.getpass(f"OpenAI API Key [leave empty to keep current]: ")
|
|
|
|
| 90 |
elif 'CLOUDINARY_API_SECRET' not in env_vars:
|
| 91 |
env_vars['CLOUDINARY_API_SECRET'] = "your_api_secret"
|
| 92 |
|
| 93 |
+
# Redis Configuration
|
| 94 |
+
print("\nRedis Configuration (for task queue):")
|
| 95 |
+
print("Option 1: Configure individual components (recommended)")
|
| 96 |
+
print("Option 2: Provide full Redis URL")
|
| 97 |
+
redis_option = input("Which option do you prefer? (1/2) [default: 1]: ")
|
| 98 |
+
|
| 99 |
+
if redis_option == "2":
|
| 100 |
+
# Redis URL
|
| 101 |
+
redis_url = input(f"Redis URL [current: {env_vars.get('REDIS_URL', 'redis://localhost:6379/0')}]: ")
|
| 102 |
+
if redis_url:
|
| 103 |
+
env_vars['REDIS_URL'] = redis_url
|
| 104 |
+
elif 'REDIS_URL' not in env_vars:
|
| 105 |
+
env_vars['REDIS_URL'] = "redis://localhost:6379/0"
|
| 106 |
+
|
| 107 |
+
# Remove individual components if they exist
|
| 108 |
+
for key in ['REDIS_HOST', 'REDIS_PORT', 'REDIS_PASSWORD']:
|
| 109 |
+
if key in env_vars:
|
| 110 |
+
del env_vars[key]
|
| 111 |
+
else:
|
| 112 |
+
# Redis Host
|
| 113 |
+
redis_host = input(f"Redis Host [current: {env_vars.get('REDIS_HOST', 'localhost')}]: ")
|
| 114 |
+
if redis_host:
|
| 115 |
+
env_vars['REDIS_HOST'] = redis_host
|
| 116 |
+
elif 'REDIS_HOST' not in env_vars:
|
| 117 |
+
env_vars['REDIS_HOST'] = "localhost"
|
| 118 |
+
|
| 119 |
+
# Redis Port
|
| 120 |
+
redis_port = input(f"Redis Port [current: {env_vars.get('REDIS_PORT', '6379')}]: ")
|
| 121 |
+
if redis_port:
|
| 122 |
+
env_vars['REDIS_PORT'] = redis_port
|
| 123 |
+
elif 'REDIS_PORT' not in env_vars:
|
| 124 |
+
env_vars['REDIS_PORT'] = "6379"
|
| 125 |
+
|
| 126 |
+
# Redis Password
|
| 127 |
+
redis_password = getpass.getpass(f"Redis Password [leave empty to keep current]: ")
|
| 128 |
+
if redis_password:
|
| 129 |
+
env_vars['REDIS_PASSWORD'] = redis_password
|
| 130 |
+
elif 'REDIS_PASSWORD' not in env_vars:
|
| 131 |
+
env_vars['REDIS_PASSWORD'] = ""
|
| 132 |
+
print("Warning: No Redis password set. This should only be used for local development.")
|
| 133 |
+
|
| 134 |
+
# Remove full URL if it exists
|
| 135 |
+
if 'REDIS_URL' in env_vars:
|
| 136 |
+
del env_vars['REDIS_URL']
|
| 137 |
|
| 138 |
# OpenAI API Key
|
| 139 |
openai_key = getpass.getpass(f"OpenAI API Key [leave empty to keep current]: ")
|