Skip to content

Commit

Permalink
🔨 update streamlit cache policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Kawaeee committed Apr 26, 2023
1 parent ce58731 commit ae4bc12
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt

COPY . .

RUN wget https://github.com/Kawaeee/butt_or_bread/releases/download/v1.2/buttbread_resnet152_3.h5
RUN wget https://github.com/Kawaeee/butt_or_bread/releases/download/v1.3/buttbread_resnet152_3.h5

ENTRYPOINT ["streamlit", "run", "/app/streamlit_app.py"]
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Corgi butt or loaf of bread?
[![GitHub Release](https://img.shields.io/github/v/release/Kawaeee/butt_or_bread)](https://github.com/Kawaeee/butt_or_bread/releases/tag/v1.2)
![Implemented in](https://upload.wikimedia.org/wikipedia/commons/f/fc/Blue_Python_3.7_Shield_Badge.svg)
[![GitHub Release](https://img.shields.io/github/v/release/Kawaeee/butt_or_bread)](https://github.com/Kawaeee/butt_or_bread/releases/tag/v1.3)
![Implemented in](https://upload.wikimedia.org/wikipedia/commons/1/1b/Blue_Python_3.9_Shield_Badge.svg)
![Visitor Badge](https://visitor-badge.glitch.me/badge?page_id=Kawaeee.butt_or_bread.visitor-badge)
[![Open in Streamlit](https://static.streamlit.io/badges/streamlit_badge_black_white.svg)](https://share.streamlit.io/kawaeee/butt_or_bread/)

Expand Down Expand Up @@ -41,7 +41,7 @@

* We already know that in order to benchmark our model performance, we can't just use `accuracy` and `validation_loss` value as the only acceptable metrics.

#### You can download our model weight here: [v1.2](https://github.com/Kawaeee/butt_or_bread/releases/download/v1.2/buttbread_resnet152_3.h5)
#### You can download our model weight here: [v1.3](https://github.com/Kawaeee/butt_or_bread/releases/download/v1.3/buttbread_resnet152_3.h5)

## Hyperparameters and configurations

Expand All @@ -55,7 +55,7 @@
* To reproduce the model, requires our datasets. You can send me an e-mail at `kawaekc@gmail.com` if you are interested.

- Initial datasets/ directory structure
```Bash
```bash
└───datasets/
│ butt/
│ bread/
Expand All @@ -72,7 +72,7 @@
```

- Ready-to-go datasets/ directory structure
```Bash
```bash
└───datasets/
│ │
│ └───train
Expand All @@ -94,12 +94,12 @@
```

- Install dependencies
```Bash
```bash
pip install -r requirements.txt
```

- Run the `train.py` python script
```Bash
```bash
python train.py --dataset-path datasets/ --model-path buttbread_resnet152_3.h5
```

Expand All @@ -113,25 +113,25 @@
```

- Install dependencies
```Bash
```bash
pip install -r requirements.txt
```

- Run the streamlit
```Bash
```bash
streamlit run streamlit_app.py
```

- Streamlit web application will be hosted on http://localhost:8501
> Streamlit web application will be hosted on http://localhost:8501
## Streamlit Docker Reproduction

- Build Docker image from Dockerfile
```Bash
docker build -t butt_or_bread -f Dockerfile .
```

- Run Docker with exposed port 8501
```Bash
docker run -p 8501:8501 butt_or_bread
```
- Following instructions below
```bash
# Directly build and run
docker build -t butt-bread-image .
docker run --rm --name=butt-bread-container -p 0.0.0.0:8501:8501 butt-bread-image

# Serve with docker compose
docker-compose build
docker-compose up
```
4 changes: 2 additions & 2 deletions streamlit_app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"icon":"assets/icon/corgi-icon.png",
"markdown":{
"star":"[![GitHub Star](https://img.shields.io/github/stars/Kawaeee/butt_or_bread)](https://github.com/Kawaeee/butt_or_bread)",
"release":"[![GitHub Release](https://img.shields.io/github/v/release/Kawaeee/butt_or_bread)](https://github.com/Kawaeee/butt_or_bread/releases/tag/v1.2)",
"release":"[![GitHub Release](https://img.shields.io/github/v/release/Kawaeee/butt_or_bread)](https://github.com/Kawaeee/butt_or_bread/releases/tag/v1.3)",
"visitor":"![Visitor Badge](https://visitor-badge.glitch.me/badge?page_id=Kawaeee.butt_or_bread.visitor-badge)"
},
"mode":{
Expand All @@ -20,7 +20,7 @@
}
},
"model":{
"url":"https://github.com/Kawaeee/butt_or_bread/releases/download/v1.2/buttbread_resnet152_3.h5",
"url":"https://github.com/Kawaeee/butt_or_bread/releases/download/v1.3/buttbread_resnet152_3.h5",
"label":{
"corgi":"Corgi butt 🐕",
"bread":"Loaf of bread 🍞"
Expand Down
59 changes: 46 additions & 13 deletions streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
with open("streamlit_app.json") as cfg_file:
st_app_cfg = json.load(cfg_file)


ui_cfg = st_app_cfg["ui"]
model_cfg = st_app_cfg["model"]
image_cfg = st_app_cfg["image"]
Expand All @@ -28,9 +27,37 @@
)


@st.cache(allow_output_mutation=True, suppress_st_warning=True, max_entries=3, ttl=300)
@st.cache_resource
def get_classifier():
"""Allow butt_bread model caching"""
"""
Retrieves a cached instance of a ButtBreadClassifier model, or creates a new instance if none exists.
Returns:
A ButtBreadClassifier object that has been downloaded and initialized.
Usage:
To retrieve a cached classifier, call this function without any arguments. The cached classifier will be returned
if it exists, or a new one will be created and cached.
Example:
>>> classifier = get_classifier()
To force the creation of a new instance and bypass the cache, pass a new `model_url` parameter as a keyword
argument.
Example:
>>> new_classifier = get_classifier(model_url='https://new-model-url.com')
Raises:
Any exceptions raised during the initialization of the ButtBreadClassifier object, such as if the download
or initialization fails.
Note:
This function makes use of the `@st.cache_resource` decorator, which allows the resulting classifier object to be
cached and reused across different sessions of the Streamlit app. This can greatly improve performance, but also
means that changes to the underlying model will not be reflected until the cache is cleared.
"""
classifier = ButtBreadClassifier(model_url=model_cfg["url"])
classifier.download()
classifier.initialize()
Expand All @@ -39,28 +66,34 @@ def get_classifier():


if __name__ == "__main__":

image_file, image, prediction = None, None, None
classifier = get_classifier()

st_logger.info("[DEBUG] %s", health_check(), exc_info=0)
classifier = get_classifier()
st_logger.info("[INFO] Initialize %s model successfully", "buttbread_resnet152_3.h5", exc_info=0)
st_logger.info("[DEBUG] %s", health_check(), exc_info=0)

st.title(ui_cfg["title"])
st.markdown(f'{ui_cfg["markdown"]["release"]} {ui_cfg["markdown"]["star"]} {ui_cfg["markdown"]["visitor"]}', unsafe_allow_html=True)
st.title(body=ui_cfg["title"])
st.markdown(
body=f'{ui_cfg["markdown"]["release"]} {ui_cfg["markdown"]["star"]} {ui_cfg["markdown"]["visitor"]}',
unsafe_allow_html=True,
)

mode = st.radio("", [ui_cfg["mode"]["upload"]["main_label"], ui_cfg["mode"]["select"]["main_label"]])
mode = st.radio(
label="options?",
options=[ui_cfg["mode"]["upload"]["main_label"], ui_cfg["mode"]["select"]["main_label"]],
label_visibility="hidden",
)

if mode == ui_cfg["mode"]["upload"]["main_label"]:
image_file = st.file_uploader(mode, accept_multiple_files=False)
image_file = st.file_uploader(label=mode, accept_multiple_files=False)
elif mode == ui_cfg["mode"]["select"]["main_label"]:
class_label = st.selectbox(ui_cfg["mode"]["select"]["class_label"], model_cfg["label"].values())
class_label = st.selectbox(label=ui_cfg["mode"]["select"]["class_label"], options=model_cfg["label"].values())

if class_label == model_cfg["label"]["corgi"]:
image_label = st.selectbox(ui_cfg["mode"]["select"]["corgi_label"], [*image_cfg["corgi"]])
image_label = st.selectbox(label=ui_cfg["mode"]["select"]["corgi_label"], options=[*image_cfg["corgi"]])
image_file = os.path.join(image_cfg["base_path"], image_cfg["corgi"][image_label])
elif class_label == model_cfg["label"]["bread"]:
image_label = st.selectbox(ui_cfg["mode"]["select"]["bread_label"], [*image_cfg["bread"]])
image_label = st.selectbox(label=ui_cfg["mode"]["select"]["bread_label"], options=[*image_cfg["bread"]])
image_file = os.path.join(image_cfg["base_path"], image_cfg["bread"][image_label])

if image_file:
Expand Down

0 comments on commit ae4bc12

Please sign in to comment.