Initial commit

This commit is contained in:
Francois Pelletier 2023-05-07 13:49:00 -04:00
commit 94fe4e9746
7 changed files with 253 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/.idea/
/venv/

38
.gitlab-ci.yml Normal file
View file

@ -0,0 +1,38 @@
# .gitlab-ci.yml
stages:
- dockerize
- deployment
build-push-docker-image-job:
stage: dockerize
# Specify a Docker image to run the job in.
image: docker:20-dind
# Specify an additional image 'docker:dind' ("Docker-in-Docker") that
# will start up the Docker daemon when it is brought up by a runner.
before_script:
- docker login -u "$DOCKER_REGISTRY_USER" -p "$DOCKER_REGISTRY_PASSWORD" $DOCKER_REGISTRY_URL # Instructs GitLab to login to its registry
services:
- name: docker:20-dind
alias: docker
command: ["--tls=false"]
script:
- echo "Building..." # MAKE SURE NO SPACE ON EITHER SIDE OF = IN THE FOLLOWING LINE
- export CONTAINER_FULL_IMAGE_NAME_WITH_TAG=$IMAGE_NAME_WITH_REGISTRY_PREFIX/my-build-image:$COMMIT_HASH
- docker build -f ./Dockerfile --pull -t built-image-name .
- docker tag built-image-name "$CONTAINER_FULL_IMAGE_NAME_WITH_TAG"
- docker push "$CONTAINER_FULL_IMAGE_NAME_WITH_TAG"
- echo "$CONTAINER_FULL_IMAGE_NAME_WITH_TAG"
- echo "Deploying on CapRover..."
- docker run caprover/cli-caprover:2.2.3 caprover deploy --caproverUrl "$CAPROVER_URL" --caproverPassword "$CAPROVER_PASSWORD" -a "$CAPROVER_APP" -i "$CONTAINER_FULL_IMAGE_NAME_WITH_TAG"
only:
- main
variables:
DOCKER_REGISTRY_USER: ${CI_REGISTRY_USER}
DOCKER_REGISTRY_PASSWORD: ${CI_REGISTRY_PASSWORD}
DOCKER_REGISTRY_URL: ${CI_REGISTRY}
IMAGE_NAME_WITH_REGISTRY_PREFIX: ${CI_REGISTRY_IMAGE}
COMMIT_HASH: ${CI_COMMIT_SHA}
CAPROVER_URL: ${CAPROVER_URL}
CAPROVER_PASSWORD: ${CAPROVER_PASSWORD}
CAPROVER_APP: ${CAPROVER_APP}

23
Dockerfile Normal file
View file

@ -0,0 +1,23 @@
# Set base image
FROM python:3.9-slim-buster
# Set work directory
WORKDIR /app
# Copy requirements file
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy app file
COPY main.py .
# Set environment variables
ENV PORT 8501
# Expose port
EXPOSE 8501
# Run Streamlit app
CMD ["streamlit", "run", "--server.port=8501", "main.py"]

11
build-local.sh Normal file
View file

@ -0,0 +1,11 @@
#!/bin/bash
# Build the Docker image with the "local/centrer-image-frontend" tag
docker build -t local/centrer-image-frontend .
# Check if the Docker image was built successfully
if [ $? -eq 0 ]; then
echo "Docker image built successfully."
else
echo "Error: Docker image build failed."
fi

1
install.sh Normal file
View file

@ -0,0 +1 @@
pip install -r requirements.txt

134
main.py Normal file
View file

@ -0,0 +1,134 @@
import os
import requests
import streamlit as st
from PIL import Image
from io import BytesIO
from typing import List
from pydantic import BaseModel
class MosaicRequest(BaseModel):
rows: int
columns: int
buffer_size: int
keywords: List[str]
background_file: str
class BackgroundRequest(BaseModel):
backgrounds: List[str]
backend_url = os.environ.get('BACKEND')
st.set_page_config(page_title='Mosaic Generator')
st.title('Mosaic Generator')
st.sidebar.title('Navigation')
tabs = st.sidebar.radio('Select task', ['Generate Mosaic', 'Upload Image', 'Upload Background'])
if tabs == 'Generate Mosaic':
st.header('Generate Mosaic')
rows = st.number_input('Rows', min_value=1, value=10)
columns = st.number_input('Columns', min_value=1, value=10)
buffer_size = st.number_input('Buffer Size', min_value=1, value=100)
keywords = st.text_input('Keywords (separated by commas)', 'cat,dog')
# Send a GET request to the endpoint
response = requests.get(backend_url + '/backgrounds')
# Check if the request was successful
if response.status_code == 200:
# Parse the response JSON into a BackgroundRequest object
background_request = BackgroundRequest.parse_raw(response.text)
# Create a Streamlit selectbox with the list of backgrounds
background_file = st.selectbox("Select a background", options=background_request.backgrounds)
else:
# Display an error message if the request was not successful
background_file = ''
st.error("Failed to retrieve background options.")
if st.button('Generate Mosaic'):
if background_file is None:
st.warning('Please select a background image')
else:
# Send request to generate_mosaic endpoint
data = {
'rows': rows,
'columns': columns,
'buffer_size': buffer_size,
'keywords': keywords.split(','),
'background_file': background_file
}
mosaic_request = MosaicRequest(**data)
response = requests.post(backend_url + '/generate_mosaic',
json=mosaic_request.dict(),
headers={"Content-Type": "application/json"}
)
if response.ok:
st.success('Mosaic generated')
st.json(response.json())
else:
st.error('Error generating mosaic: ' + response.reason)
elif tabs == 'Upload Image':
st.header('Upload Image')
uploaded_file = st.file_uploader('Image File', type=['jpg', 'jpeg', 'png'])
if st.button('Upload Image'):
if uploaded_file is None:
st.warning('Please upload an image file')
else:
# Convert uploaded image to bytes
img_bytes = BytesIO()
Image.open(uploaded_file).save(img_bytes, format='PNG')
img_bytes.seek(0)
# Access the filename of the uploaded file
filename = uploaded_file.name
# Change the extension of the filename to 'png'
filename = os.path.splitext(filename)[0] + '.png'
# Send request to upload_file endpoint
files = {'file': (filename, img_bytes, 'image/png')}
response = requests.post(backend_url + '/upload_file', files=files)
if response.ok:
st.success('Image file uploaded')
st.json(response.json())
else:
st.error('Error uploading image file')
elif tabs == 'Upload Background':
st.header('Upload Background')
uploaded_file = st.file_uploader('Background Image', type=['jpg', 'jpeg', 'png'])
if st.button('Upload Background'):
if uploaded_file is None:
st.warning('Please upload a background image')
else:
# Convert uploaded image to bytes
img_bytes = BytesIO()
Image.open(uploaded_file).save(img_bytes, format='PNG')
img_bytes.seek(0)
# Access the filename of the uploaded file
filename = uploaded_file.name
# Change the extension of the filename to 'png'
filename = os.path.splitext(filename)[0] + '.png'
# Send request to upload_file endpoint
files = {'file': (filename, img_bytes, 'image/png')}
response = requests.post(backend_url + '/upload_background', files=files)
if response.ok:
st.success('Background image uploaded')
st.json(response.json())
else:
st.error('Error uploading background image')

44
requirements.txt Normal file
View file

@ -0,0 +1,44 @@
Pillow~=9.5.0
gitdb~=4.0.10
pip==23.1.2
attrs~=23.1.0
wheel~=0.40.0
tornado~=6.3.1
Jinja2~=3.1.2
toolz~=0.12.0
pytz~=2023.3
rich~=13.3.5
Pygments~=2.15.1
toml~=0.10.2
smmap~=5.0.0
mdurl~=0.1.2
numpy~=1.24.3
setuptools==67.7.2
altair~=4.2.2
pandas~=2.0.1
jsonschema~=4.17.3
entrypoints~=0.4
MarkupSafe~=2.1.2
python-dateutil~=2.8.2
pyarrow~=12.0.0
pydeck==0.8.0
blinker~=1.6.2
certifi~=2023.5.7
tzdata~=2023.3
Pympler~=1.0.1
tzlocal~=4.3
six~=1.16.0
pydantic~=1.10.7
validators~=0.20.0
decorator~=5.1.1
idna~=3.4
urllib3~=2.0.2
tenacity~=8.2.2
watchdog~=3.0.0
click~=8.1.3
streamlit~=1.22.0
cachetools~=5.3.0
requests~=2.30.0
packaging~=23.1
pyrsistent~=0.19.3
zipp~=3.15.0