daniel-de-leon commited on
Commit
5c3f07e
0 Parent(s):

Duplicate from daniel-de-leon/streamlit-docker-poc

Browse files
Files changed (7) hide show
  1. .gitignore +134 -0
  2. .streamlit/config.toml +5 -0
  3. Dockerfile +27 -0
  4. README.md +16 -0
  5. app.py +61 -0
  6. packages.txt +0 -0
  7. requirements.txt +6 -0
.gitignore ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ venv/
2
+ # Byte-compiled / optimized / DLL files
3
+ __pycache__/
4
+ *.py[cod]
5
+ *$py.class
6
+
7
+ # C extensions
8
+ *.so
9
+
10
+ # Distribution / packaging
11
+ .Python
12
+ build/
13
+ develop-eggs/
14
+ dist/
15
+ downloads/
16
+ eggs/
17
+ .eggs/
18
+ lib/
19
+ lib64/
20
+ parts/
21
+ sdist/
22
+ var/
23
+ wheels/
24
+ pip-wheel-metadata/
25
+ share/python-wheels/
26
+ *.egg-info/
27
+ .installed.cfg
28
+ *.egg
29
+ MANIFEST
30
+
31
+ # PyInstaller
32
+ # Usually these files are written by a python script from a template
33
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
34
+ *.manifest
35
+ *.spec
36
+
37
+ # Installer logs
38
+ pip-log.txt
39
+ pip-delete-this-directory.txt
40
+
41
+ # Unit test / coverage reports
42
+ htmlcov/
43
+ .tox/
44
+ .nox/
45
+ .coverage
46
+ .coverage.*
47
+ .cache
48
+ nosetests.xml
49
+ coverage.xml
50
+ *.cover
51
+ *.py,cover
52
+ .hypothesis/
53
+ .pytest_cache/
54
+
55
+ # Translations
56
+ *.mo
57
+ *.pot
58
+
59
+ # Django stuff:
60
+ *.log
61
+ local_settings.py
62
+ db.sqlite3
63
+ db.sqlite3-journal
64
+
65
+ # Flask stuff:
66
+ instance/
67
+ .webassets-cache
68
+
69
+ # Scrapy stuff:
70
+ .scrapy
71
+
72
+ # Sphinx documentation
73
+ docs/_build/
74
+
75
+ # PyBuilder
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ .python-version
87
+
88
+ # pipenv
89
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
90
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
91
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
92
+ # install all needed dependencies.
93
+ #Pipfile.lock
94
+
95
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
96
+ __pypackages__/
97
+
98
+ # Celery stuff
99
+ celerybeat-schedule
100
+ celerybeat.pid
101
+
102
+ # SageMath parsed files
103
+ *.sage.py
104
+
105
+ # Environments
106
+ .env
107
+ .venv
108
+ env/
109
+ venv/
110
+ ENV/
111
+ env.bak/
112
+ venv.bak/
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
131
+
132
+ .vscode/
133
+
134
+ data/audio/
.streamlit/config.toml ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [theme]
2
+ base='light'
3
+
4
+ [server]
5
+ maxUploadSize = 200
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /app
4
+
5
+ COPY ./requirements.txt /app/requirements.txt
6
+ COPY ./packages.txt /app/packages.txt
7
+
8
+ RUN apt-get update && xargs -r -a /app/packages.txt apt-get install -y && rm -rf /var/lib/apt/lists/*
9
+ RUN pip3 install --no-cache-dir -r /app/requirements.txt
10
+
11
+ # User
12
+ RUN useradd -m -u 1000 user
13
+ USER user
14
+ ENV HOME /home/user
15
+ ENV PATH $HOME/.local/bin:$PATH
16
+
17
+ WORKDIR $HOME
18
+ RUN mkdir app
19
+ WORKDIR $HOME/app
20
+ COPY . $HOME/app
21
+
22
+ EXPOSE 8501
23
+ CMD streamlit run app.py \
24
+ --server.headless true \
25
+ --server.enableCORS false \
26
+ --server.enableXsrfProtection false \
27
+ --server.fileWatcherType none
README.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Streamlit Docker Template
3
+ emoji: 📉
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ app_port: 8501
8
+ pinned: false
9
+ duplicated_from: daniel-de-leon/streamlit-docker-poc
10
+ ---
11
+
12
+ ## 🧠 Streamlit Docker Template 🔎
13
+
14
+ Streamlit Docker Template is a template for creating a Streamlit app with Docker and Hugging Face Spaces.
15
+
16
+ Code from https://docs.streamlit.io/library/get-started/create-an-app
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
3
+ from transformers import (AutoModelForSequenceClassification, AutoTokenizer,
4
+ pipeline)
5
+ import shap
6
+ from PIL import Image
7
+
8
+ st.set_option('deprecation.showPyplotGlobalUse', False)
9
+ output_width = 800
10
+ output_height = 300
11
+ rescale_logits = False
12
+
13
+
14
+
15
+ st.set_page_config(page_title='Text Classification with Shap')
16
+ st.title('Interpreting HF Pipeline Text Classification with Shap')
17
+
18
+ form = st.sidebar.form("Model Selection")
19
+ form.header('Model Selection')
20
+
21
+ model_name = form.text_input("Enter the name of the text classification LLM (note: model must be fine-tuned on a text classification task)", value = "Hate-speech-CNERG/bert-base-uncased-hatexplain")
22
+ form.form_submit_button("Submit")
23
+
24
+
25
+ @st.cache_data()
26
+ def load_model(model_name):
27
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
28
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
29
+
30
+ return tokenizer, model
31
+
32
+ tokenizer, model = load_model(model_name)
33
+ pred = pipeline("text-classification", model=model, tokenizer=tokenizer, top_k=None)
34
+ explainer = shap.Explainer(pred, rescale_to_logits = rescale_logits)
35
+
36
+ col1, col2 = st.columns(2)
37
+ text = col1.text_area("Enter text input", value = "Classify me.")
38
+
39
+ result = pred(text)
40
+ top_pred = result[0][0]['label']
41
+ col2.write('')
42
+ for label in result[0]:
43
+ col2.write(f'**{label["label"]}**: {label["score"]: .2f}')
44
+
45
+ shap_values = explainer([text])
46
+
47
+ force_plot = shap.plots.text(shap_values, display=False)
48
+ bar_plot = shap.plots.bar(shap_values[0, :, top_pred], order=shap.Explanation.argsort.flip, show=False)
49
+
50
+ st.markdown("""
51
+ <style>
52
+ .big-font {
53
+ font-size:35px !important;
54
+ }
55
+ </style>
56
+ """, unsafe_allow_html=True)
57
+ st.markdown(f'<center><p class="big-font">Shap Bar Plot for <i>{top_pred}</i> Prediction</p></center>', unsafe_allow_html=True)
58
+ st.pyplot(bar_plot, clear_figure=True)
59
+
60
+ st.markdown('<center><p class="big-font">Shap Interactive Force Plot</p></center>', unsafe_allow_html=True)
61
+ components.html(force_plot, height=output_height, width=output_width, scrolling=True)
packages.txt ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ shap
4
+ torch
5
+ matplotlib
6
+ numpy