Welcome to AI Video Boilerplate Pro! A free template to run scalable, cheap AI websites.
Hosted on Heroku for a live demo here: https://proAI.darefail.com
A single VPS running this codebase has so far proven to handle over 10,000 concurrent users, 1,000,000 views and 50 websites without issue.
RateLoaf.com - Rate how well cats sit like bread with OpenAI GPT4v, Roboflow workflows, and images uploaded to Amazon S3 in a celery task.
HandLand.lol - Create live AI multiplayer games using WebRTC, Websockets, and Roboflow for AI vision models.
AI Video Boilerplate Simple (quick frontend apps): https://github.com/DareFail/AI-Video-Boilerplate-Simple/
AI Video Boilerplate for Chrome Extensions: https://github.com/DareFail/AI-Video-Boilerplate-Chrome/
- Frontend: minimal HTML and javascript, roll your own
- Backend: Django, Docker, Postgres Database, Redis caching, Celery background tasks, Celery Beat scheduled tasks Django
- Preconfigured Libraries: Django Allauth authentication, AWS storage, Slack alerts and slack bots, Twilio texting, Sendgrid Email, Captcha spam detection, Sesame Magic Link passwordless signin, Hijack user impersonation
- Multiplayer: WebRTC and native Websockets with daphne and channels
- AI: Integrated with Roboflow (sponsored project), includes Stable diffusion, Flux, Roboflow installed.
- Scalable and Cheap to Host: Run hundreds of websites and millions of users into a heroku, digital ocean, or a VPS of your choice
This is a template for putting multiple django websites into a single codebase that can be deployed to any VPS for cheap. It is full of full deployed projects and templates for you to copy and modify.
- XXXXX_basic - Simple django webpage with shared components like favicons, navigation bar, footer, etc
- XXXXX_signedin - Flexible User to Group Sign up flow with django allauth, with teams, administrators, invitations, changing passwords, magic links, and a backend dashboard
- XXXXX_websockets - Live multiplayer django apps. This is a chatroom example.
-
Clone the repo
git clone https://github.com/darefail/AI-Video-Boilerplate-Pro.git cd AI-Video-Boilerplate-Pro
-
Install Docker The easiest way to get up and running is with Docker.
Just install Docker and Docker Compose and then run:
-
Create .env Copy .env.example and rename the file .env Fill out the uncommented values Uncomment and fill out the other values if you want to use them
-
Build the container and run locally
docker compose up docker compose exec web python manage.py makemigrations docker compose exec web python manage.py migrate
-
Go to localhost:8000
-
Make a superuser (optional)
docker compose exec web python manage.py createsuperuser
Check everything works by going to localhost:8000/admin and logging in
On a Mac: fn + f1
Dev Containers Reopen in Container
docker compose up
docker compose exec web python manage.py makemigrations
docker compose exec web python manage.py migrate
# sometimes have to run
docker compose exec web python manage.py makemigrations [app name]
docker compose exec web pip3 install --upgrade pip-tools
docker compose exec web pip3 install --upgrade pip setuptools wheel twine check-wheel-contents
docker compose exec web pip-compile requirements.in
docker compose build
docker compose up
# Simple python formatting
black .
# Python errors
flake8 .
# Django Templates / Javascript / HTML
djlint .
# Django Templates / Javascript / HTML formatting
djlint . --reformat
- Sign up or log in to Vercel.
- Create a new project from the Vercel dashboard.
- Connect your GitHub repository.
- Click "Deploy".
- Sign up or log in to Heroku.
- Create a new app from the Heroku dashboard.
- Fill out all the environment variables from .env.
In production:
DJANGO_SETTINGS_MODULE=main.settings.heroku
-
Add Heroku Data for Redis and Heroku Postgres in the Add-ons
-
Connect your GitHub repository.
-
Click "Deploy Branch".
-
Set the heroku settings
heroku stack:set container
heroku run python manage.py createsuperuser
-
Under settings, Configure SSL and set to Automatic Certificate Management (ACM)
-
If an external domain and using cloudflare, point to heroku and change the SSL to Full
(If you don't, the url will have an infinite redirect)
app_directory = 'XXXXX'
XxxxxConfig
name = XXXXX
'XXXXX.apps.XxxxxConfig',
docker compose exec web python manage.py makemigrations XXXXX
docker compose exec web python manage.py migrate
9. Add new logo files from https://realfavicongenerator.net into static/favicons
XXXXX.com
XXXXX.urls
(If you don't, the url will have an infinite redirect)
Pre commit hooks for linters is not working
pre-commit install
Ignore precommmit hook
git commit --no-verify -m ""
List all docker containers
docker ps
Log into docker database
docker exec -it main-db-1 psql -U postgres
Inside database - (\l show schema, \c connect to schema, \dt list all tables)
\l
\c main
\dt
Run Python in Heroku Shell
heroku run python
#when loaded
import django
django.setup()
Useful repeatable code changer
https://pinetools.com/add-text-each-line
Static files not found
docker compose exec web python manage.py collectstatic
Bucket Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:GetObjectVersion"
],
"Resource": "arn:aws:s3:::PUT_BUCKETNAME_HERE/*"
}
]
}
Cross-origin resource sharing (CORS)
[
{
"AllowedHeaders": [
"PUT_DOMAIN_HERE.com",
"PUT_SUBDOMAIN_HERE.DOMAIN.com"
],
"AllowedMethods": [
"GET",
"HEAD",
"POST",
"PUT"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": []
}
]
Query Optimization
# Bad: N+1 queries
for user in Users.objects.all():
print(user.profile.bio) # One query per user
# Good: Single query with select_related
users = User.objects.select_related('profile').all()
for user in users:
print(user.profile.bio) # No additional queries
Database Indexing
class Order(models.Model):
user = models.ForeignKey(User)
created_at = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=20)
class Meta:
indexes = [
models.Index(fields=['created_at', 'status']),
models.Index(fields=['user', 'status']),
]
Queryset Optimization
# Bad: Loading entire objects
users = User.objects.all()
# Good: Only loading needed fields
users = User.objects.values('id', 'email')
# Better: Using iterator() for large querysets
for user in User.objects.iterator():
process_user(user)
View-Level Caching
from django.views.decorators.cache import cache_page
@cache_page(60 * 15) # Cache for 15 minutes
def product_list(request):
products = Product.objects.all()
return render(request, 'products/list.html', {'products': products})
Template Fragment Caching
{% load cache %}
{% cache 500 sidebar request.user.id %}
{% for item in expensive_query %}
{{ item }}
{% endfor %}
{% endcache %}
Low-Level Cache API
from django.core.cache import cache
def get_expensive_result(user_id):
cache_key = f'expensive_result_{user_id}'
result = cache.get(cache_key)
if result is None:
result = expensive_computation(user_id)
cache.set(cache_key, result, timeout=3600)
return result
Async: When You Need Concurrent Connections
# views.py
async def async_view(request):
async with aiohttp.ClientSession() as session:
async with session.get('http://api.example.com/data') as response:
data = await response.json()
return JsonResponse(data)
# urls.py
path('async-data/', async_view)
Background Tasks: Don’t Block the Request-Response Cycle
from django.core.mail import send_mail
from celery import shared_task
@shared_task
def send_welcome_email(user_id):
user = User.objects.get(id=user_id)
send_mail(
'Welcome!',
'Thanks for joining.',
'[email protected]',
[user.email],
)
# In your view
def signup(request):
user = User.objects.create_user(...)
send_welcome_email.delay(user.id)
return redirect('home')
Load Balancing: When Single Server Isn’t Enough
# settings.py for multiple servers
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'HOST': 'primary.database.host',
'CONN_MAX_AGE': 60,
},
'read_replica': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydb',
'HOST': 'replica.database.host',
'CONN_MAX_AGE': 60,
}
}
Media Files: Move to CDN Early
# settings.py
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_ACCESS_KEY_ID = 'your-access-key'
AWS_SECRET_ACCESS_KEY = 'your-secret-key'
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
Thanks to Roboflow for sponsoring this project. Get your free API key at: Roboflow
Distributed under the APACHE 2.0 License. See LICENSE
for more information.
Twitter: @darefailed
Youtube: How to Video coming soon
Project Link: https://github.com/darefail/AI-Video-Boilerplate-Pro