Flask app with PostgreSQL, Docker Compose, and Ollama
This project allows a user to upload an image, send it to a local Ollama model, and receive a generated story.
Each story is stored in a PostgreSQL database, using the image filename as an identifier.
If the same image is uploaded again, the app returns the previously saved story instead of generating a new one.
- Upload an image through the Flask API
- Generate a story from the image using an Ollama model
- Save stories in PostgreSQL (image name = identifier)
- Return existing story if the image was already processed
- Fully containerized using Docker Compose
- Ability to map a local Ollama model into the container so you don’t have to re-pull it every time
git clone <your-repo-url>
cd <project-folder>To avoid pulling the model every time the container is rebuilt, we will map the local Ollama model directory into the Docker container.
- Install Ollama according to the official instructions:
curl -fsSL https://ollama.com/install.sh | sh - Start the Ollama server
ollama serve
- In another terminal, pull the model you want to use:
Make sure the pulled model name matches the model used inside your Flask app.
ollama pull llava-llama3
- On your host machine:
This typically outputs something like:
sudo find / -name models | grep "ollama"
Use the directory that contains your pulled model(s)./usr/share/ollama/models /home/<user>/.ollama/models
- You need to know the location of the model path inside the container to map it correctly.
Start the container once:
In another terminal, find the container ID:
docker compose up --build
And open a shell inside the container:docker ps
Find the internal model directory:docker exec -it <container_id> /bin/bash
This will get you a path such as:find / -name models | grep "ollama"
/root/.ollama/models
In your docker-compose.yml, map the local models folder into the container:
volumes:
- /path/to/local/ollama/models:/root/.ollama/modelsThis ensures:
- The container uses your local model files
- You do not need to pull large models again when rebuilding the container
- The model remains persistent across rebuilds
Copy the .env.example file and rename it:
cp .env.example .envUpdate the variables if needed (database credentials, model name, etc.).
You can configure optional downscaling settings in your .env file to reduce image size before sending it to the vision model.
This can speed up processing, especially for very large uploads.
However, note that many vision models internally downscale images anyway, so the performance gain may be limited depending on the model you use.
docker compose up --buildOnce running:
-
Open the Flask app in your browser at: http://localhost:5000
-
Open Adminer (database UI) at: http://localhost:8080
You are now ready to upload images and generate stories!