diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57f1cb2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea/ \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..a5f75e9 --- /dev/null +++ b/.gitlab-ci.yml @@ -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} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b13e8c8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.10-slim + +EXPOSE 8052 + +WORKDIR /app + +# Install the app's dependencies +RUN pip install --upgrade pip +COPY requirements.txt . +RUN pip install -r requirements.txt +COPY install.py . +RUN python install.py + +# Copy the app's code +COPY main.py . + +# Set the entrypoint to run the app +ENTRYPOINT [ "streamlit", "run" ] +CMD [ "main.py", "--server.port=8052", "--server.headless", "true", "--server.fileWatcherType", "none", "--browser.gatherUsageStats", "false"] diff --git a/build-local.sh b/build-local.sh new file mode 100644 index 0000000..057faca --- /dev/null +++ b/build-local.sh @@ -0,0 +1 @@ +docker build -t local/split-reading-bold . \ No newline at end of file diff --git a/docker-run.sh b/docker-run.sh new file mode 100644 index 0000000..97bf5dc --- /dev/null +++ b/docker-run.sh @@ -0,0 +1,6 @@ +#!/usr/bin/zsh + +docker stop split-reading-bold +docker rm split-reading-bold +# Ce programme sert à lancer le split-reading-bold dans un docker localement pour tester +docker run -p 8052:8052 --name split-reading-bold --network host local/split-reading-bold diff --git a/install.py b/install.py new file mode 100644 index 0000000..dcefba5 --- /dev/null +++ b/install.py @@ -0,0 +1,2 @@ +import nltk +nltk.download('punkt') \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..9668a15 --- /dev/null +++ b/main.py @@ -0,0 +1,62 @@ +import re + +import streamlit as st +import nltk + + +def remove_space_before_punctuation(text): + """ + Removes space before punctuation. + """ + text = re.sub(r"\s+\*\*([.;,?!])\*\*", "\1", text) + return text + + +def make_bold_part(token, num): + """ + Makes bold text. + """ + if len(token) <= 2: + return token + else: + return f"**{token[0:num]}**{token[num:]}" + + +def split_tokens(text, num, lang): + """ + Splits a text into tokens. + """ + paragraphs = text.split("\n\n") + new_paragraphs = [] + for paragraph in paragraphs: + tokens = nltk.word_tokenize(paragraph, language=lang) + tokens = [make_bold_part(token, num) for token in tokens] + par = remove_space_before_punctuation(" ".join(tokens)) + new_paragraphs.append(par) + return "\n\n".join(new_paragraphs) + + +def write_app(): + """ + Write a streamlit app with an input box, a numerical slider from 1 to 5 and an output box. + :return: + """ + st.title("Texte Rapide") + st.sidebar.title("Choisis le niveau de gras") + num = st.sidebar.slider("Number", 1, 10, 5) + st.sidebar.text(f"Niveau de gras: {num}") + # choose a language + lang = st.sidebar.selectbox(label="Langue", options=["french", "english"], index=0) + st.sidebar.text(f"Langue: {lang}") + text = st.text_area("Text", "Entre ton texte ici") + bouton = st.button("Envoyer") + if bouton: + st.markdown(f"{split_tokens(text, num, lang)}") + + +def main(): + write_app() + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0b9f690 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,46 @@ +Pillow~=9.4.0 +gitdb~=4.0.10 +pip~=23.0.1 +attrs~=22.2.0 +wheel~=0.38.4 +tornado~=6.2 +Jinja2~=3.1.2 +toolz~=0.12.0 +nltk~=3.8.1 +numpy~=1.24.2 +regex~=2022.10.31 +click~=8.1.3 +tqdm~=4.65.0 +requests~=2.28.2 +pytz~=2022.7.1 +rich~=13.3.2 +Pygments~=2.14.0 +toml~=0.10.2 +smmap~=5.0.0 +mdurl~=0.1.2 +setuptools~=67.3.3 +altair~=4.2.2 +pandas~=1.5.3 +jsonschema~=4.17.3 +entrypoints~=0.4 +MarkupSafe~=2.1.2 +joblib~=1.2.0 +python-dateutil~=2.8.2 +pyarrow~=11.0.0 +pydeck~=0.8.0 +blinker~=1.5 +certifi~=2022.12.7 +tzdata~=2022.7 +Pympler~=1.0.1 +tzlocal~=4.2 +six~=1.16.0 +idna~=3.4 +urllib3~=1.26.15 +watchdog~=2.3.1 +streamlit~=1.20.0 +cachetools~=5.3.0 +validators~=0.20.0 +packaging~=23.0 +pyrsistent~=0.19.3 +zipp~=3.15.0 +decorator~=5.1.1 \ No newline at end of file diff --git a/run_app.sh b/run_app.sh new file mode 100644 index 0000000..50e8f78 --- /dev/null +++ b/run_app.sh @@ -0,0 +1 @@ +streamlit run /home/francois/projets/split_reading_bold/main.py \ No newline at end of file diff --git a/split-reading-bold.iml b/split-reading-bold.iml new file mode 100644 index 0000000..cd9a9c8 --- /dev/null +++ b/split-reading-bold.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file