Spaces:
Runtime error
Runtime error
Kamanda Wycliffe
commited on
Commit
•
4f7e359
1
Parent(s):
3ecd607
first commit
Browse files- CatDog.ipynb +0 -0
- Model Deployment CatDog.ipynb +108 -0
- Streamlit/app.py +365 -0
- Streamlit/hugginface.py +33 -0
- Untitled.ipynb +358 -0
- Untitled1.ipynb +6 -0
- Untitled2.ipynb +6 -0
- app.py +365 -0
- hugginface.py +33 -0
- media/ann-savchenko-H0h_89iFsWs-unsplash.jpg +0 -0
- media/data science components +0 -0
- media/data science components.png +0 -0
- students.csv +5 -0
- untitled.txt +0 -0
CatDog.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
Model Deployment CatDog.ipynb
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [
|
8 |
+
{
|
9 |
+
"name": "stdout",
|
10 |
+
"output_type": "stream",
|
11 |
+
"text": [
|
12 |
+
"Overwriting app.py\n"
|
13 |
+
]
|
14 |
+
}
|
15 |
+
],
|
16 |
+
"source": [
|
17 |
+
"%%writefile app.py\n",
|
18 |
+
"import streamlit as st\n",
|
19 |
+
"import tensorflow as tf\n",
|
20 |
+
"import cv2\n",
|
21 |
+
"from PIL import Image, ImageOps\n",
|
22 |
+
"import numpy as np\n",
|
23 |
+
"import matplotlib.pyplot as plt\n",
|
24 |
+
"import numpy as np\n",
|
25 |
+
"import os\n",
|
26 |
+
"import PIL\n",
|
27 |
+
"import tensorflow as tf\n",
|
28 |
+
"\n",
|
29 |
+
"from tensorflow import keras\n",
|
30 |
+
"from tensorflow.keras import layers\n",
|
31 |
+
"from tensorflow.keras.models import Sequential\n",
|
32 |
+
"\n",
|
33 |
+
"st.title(\"Cat-Dog Classification\")\n",
|
34 |
+
"st.header(\"Please input an image to be classified:\")\n",
|
35 |
+
"st.text(\"Created by Saksham Gulati\")\n",
|
36 |
+
"\n",
|
37 |
+
"@st.cache(allow_output_mutation=True)\n",
|
38 |
+
"\n",
|
39 |
+
"def teachable_machine_classification(img, weights_file):\n",
|
40 |
+
" # Load the model\n",
|
41 |
+
" model = keras.models.load_model(weights_file)\n",
|
42 |
+
"\n",
|
43 |
+
" # Create the array of the right shape to feed into the keras model\n",
|
44 |
+
" data = np.ndarray(shape=(1, 200, 200, 3), dtype=np.float32)\n",
|
45 |
+
" image = img\n",
|
46 |
+
" #image sizing\n",
|
47 |
+
" size = (200, 200)\n",
|
48 |
+
" image = ImageOps.fit(image, size, Image.ANTIALIAS)\n",
|
49 |
+
"\n",
|
50 |
+
" #turn the image into a numpy array\n",
|
51 |
+
" image_array = np.asarray(image)\n",
|
52 |
+
" # Normalize the image\n",
|
53 |
+
" normalized_image_array = (image_array.astype(np.float32) / 255)\n",
|
54 |
+
"\n",
|
55 |
+
" # Load the image into the array\n",
|
56 |
+
" data[0] = normalized_image_array\n",
|
57 |
+
"\n",
|
58 |
+
" # run the inference\n",
|
59 |
+
" prediction_percentage = model.predict(data)\n",
|
60 |
+
" prediction=prediction_percentage.round()\n",
|
61 |
+
" \n",
|
62 |
+
" return prediction,prediction_percentage\n",
|
63 |
+
"\n",
|
64 |
+
"\n",
|
65 |
+
"uploaded_file = st.file_uploader(\"Choose an Cat or Dog Image...\", type=\"jpg\")\n",
|
66 |
+
"\n",
|
67 |
+
"if uploaded_file is not None:\n",
|
68 |
+
" image = Image.open(uploaded_file)\n",
|
69 |
+
" st.image(image, caption='Uploaded file', use_column_width=True)\n",
|
70 |
+
" st.write(\"\")\n",
|
71 |
+
" st.write(\"Classifying...\")\n",
|
72 |
+
" label,perc = teachable_machine_classification(image, 'catdog.h5')\n",
|
73 |
+
" if label == 1:\n",
|
74 |
+
" st.write(\"Its a Dog, confidence level:\",perc)\n",
|
75 |
+
" else:\n",
|
76 |
+
" st.write(\"Its a Cat, confidence level:\",1-perc)"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": null,
|
82 |
+
"metadata": {},
|
83 |
+
"outputs": [],
|
84 |
+
"source": []
|
85 |
+
}
|
86 |
+
],
|
87 |
+
"metadata": {
|
88 |
+
"kernelspec": {
|
89 |
+
"display_name": "Python 3 (ipykernel)",
|
90 |
+
"language": "python",
|
91 |
+
"name": "python3"
|
92 |
+
},
|
93 |
+
"language_info": {
|
94 |
+
"codemirror_mode": {
|
95 |
+
"name": "ipython",
|
96 |
+
"version": 3
|
97 |
+
},
|
98 |
+
"file_extension": ".py",
|
99 |
+
"mimetype": "text/x-python",
|
100 |
+
"name": "python",
|
101 |
+
"nbconvert_exporter": "python",
|
102 |
+
"pygments_lexer": "ipython3",
|
103 |
+
"version": "3.9.12"
|
104 |
+
}
|
105 |
+
},
|
106 |
+
"nbformat": 4,
|
107 |
+
"nbformat_minor": 2
|
108 |
+
}
|
Streamlit/app.py
ADDED
@@ -0,0 +1,365 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
3 |
+
#st.markdown()
|
4 |
+
st.title("Document Title")
|
5 |
+
st.header("Article header")
|
6 |
+
st.subheader("Article subheader")
|
7 |
+
st.code("y = mx + c")
|
8 |
+
st.latex("\ int a y^2 \ , dy")
|
9 |
+
st.text("This is a chair!")
|
10 |
+
st.markdown('Staying hydrated is **_very_ cool**.')
|
11 |
+
|
12 |
+
students = ["Amelia Kami", "Antoinne Mark", "Peter Zen", "North Kim"]
|
13 |
+
|
14 |
+
marks = [82, 76, 96, 68]
|
15 |
+
|
16 |
+
import pandas as pd
|
17 |
+
|
18 |
+
df = pd.DataFrame()
|
19 |
+
|
20 |
+
df["Student Name"] = students
|
21 |
+
|
22 |
+
df["Marks"] = marks
|
23 |
+
#save to dataframe
|
24 |
+
df.to_csv("students.csv", index = False)
|
25 |
+
#display
|
26 |
+
st.dataframe(df)
|
27 |
+
|
28 |
+
#Static table
|
29 |
+
st.table(df)
|
30 |
+
|
31 |
+
#Metrics
|
32 |
+
st.metric("KPI", 56, 3)
|
33 |
+
#Json
|
34 |
+
st.json(df.to_dict())
|
35 |
+
|
36 |
+
#Code
|
37 |
+
#average of a list
|
38 |
+
code = '''def cal_average(numbers):
|
39 |
+
sum_number = 0
|
40 |
+
for t in numbers:
|
41 |
+
sum_number = sum_number + t
|
42 |
+
|
43 |
+
average = sum_number / len(numbers)
|
44 |
+
return average'''
|
45 |
+
st.code(code, language='python')
|
46 |
+
#progress bar
|
47 |
+
|
48 |
+
import streamlit as st
|
49 |
+
import time
|
50 |
+
|
51 |
+
# Sample Progress bar
|
52 |
+
#bar_p = st.progress(0)
|
53 |
+
|
54 |
+
#for percentage_complete in range(100):
|
55 |
+
#time.sleep(0.1)
|
56 |
+
#bar_p.progress(percentage_complete + 1)
|
57 |
+
|
58 |
+
#with st.spinner('Please wait...'):
|
59 |
+
#time.sleep(5)
|
60 |
+
#st.write('Complete!')
|
61 |
+
|
62 |
+
|
63 |
+
#Displaying an image using Streamlit
|
64 |
+
from PIL import Image
|
65 |
+
image = Image.open('media/ann-savchenko-H0h_89iFsWs-unsplash.jpg')
|
66 |
+
|
67 |
+
#st.image(image, caption='Sunset grass backgrounds')
|
68 |
+
|
69 |
+
#plotly
|
70 |
+
import plotly.express as px
|
71 |
+
# This dataframe has 244 rows, but 4 unique entries for the `day` variable
|
72 |
+
df = px.data.tips()
|
73 |
+
figx = px.pie(df, values='tip', names='day', title='Tips per day')
|
74 |
+
# Plot!
|
75 |
+
st.plotly_chart(figx, use_container_width=True)
|
76 |
+
|
77 |
+
#Altair
|
78 |
+
import altair as alt
|
79 |
+
import streamlit as st
|
80 |
+
import numpy as np
|
81 |
+
|
82 |
+
df = pd.DataFrame(
|
83 |
+
np.random.randn(300, 4),
|
84 |
+
columns=['a', 'b', 'c', 'd'])
|
85 |
+
|
86 |
+
chrt = alt.Chart(df).mark_circle().encode(
|
87 |
+
x='a', y='b', size='c', color='c', tooltip=['a', 'b', 'c', 'd'])
|
88 |
+
|
89 |
+
st.altair_chart(chrt, use_container_width=True)
|
90 |
+
|
91 |
+
#Matplotlib
|
92 |
+
import matplotlib.pyplot as plt
|
93 |
+
import numpy as np
|
94 |
+
|
95 |
+
arr = np.random.normal(1, 1, size=1000)
|
96 |
+
fig, ax = plt.subplots()
|
97 |
+
ax.hist(arr, bins=30)
|
98 |
+
plt.grid()
|
99 |
+
st.pyplot(fig)
|
100 |
+
|
101 |
+
#Interactive widgets
|
102 |
+
st.button("Click here")
|
103 |
+
#st.download_button("Download audio", file)
|
104 |
+
selected = st.checkbox("Accept terms")
|
105 |
+
choice = st.radio("Select one", ["Apples", "Oranges"])
|
106 |
+
|
107 |
+
|
108 |
+
option = st.selectbox(
|
109 |
+
'How would you like to receive your package?',
|
110 |
+
('By air', 'By sea', 'By rail'))
|
111 |
+
|
112 |
+
st.write('You selected:', option)
|
113 |
+
import datetime
|
114 |
+
day = st.date_input(
|
115 |
+
"When is your birthday?",
|
116 |
+
datetime.date(2022, 7, 6))
|
117 |
+
st.write('Your birthday is:', day)
|
118 |
+
|
119 |
+
color = st.color_picker('Choose A Color', '#00FFAA')
|
120 |
+
st.write('The selected color is', color)
|
121 |
+
|
122 |
+
|
123 |
+
@st.cache
|
124 |
+
def fetch_data():
|
125 |
+
df = pd.read_csv("students.csv")
|
126 |
+
return df
|
127 |
+
|
128 |
+
#data = fetch_data()
|
129 |
+
|
130 |
+
#Visualization
|
131 |
+
|
132 |
+
import matplotlib.pyplot as plt
|
133 |
+
import numpy as np
|
134 |
+
|
135 |
+
#Matplotlib
|
136 |
+
|
137 |
+
import matplotlib.pyplot as plt
|
138 |
+
import numpy as np
|
139 |
+
fig = plt.figure()
|
140 |
+
ax = fig.add_axes([0,0,1,1])
|
141 |
+
animals = ["Zebras", "Elephants", "Rhinos", "Leopards"]
|
142 |
+
number = [65, 72, 77, 59]
|
143 |
+
ax.bar(animals, number)
|
144 |
+
fig = plt.show()
|
145 |
+
st.pyplot(fig)
|
146 |
+
#Seaborn
|
147 |
+
import seaborn as sns
|
148 |
+
fig = plt.figure()
|
149 |
+
ax = sns.barplot(x = animals, y = number)
|
150 |
+
fig = plt.show()
|
151 |
+
st.pyplot(fig)
|
152 |
+
|
153 |
+
#Altair
|
154 |
+
#define data
|
155 |
+
df = pd.DataFrame()
|
156 |
+
|
157 |
+
df["Animals"] = animals
|
158 |
+
df["Number"] = number
|
159 |
+
#create chart
|
160 |
+
chrt = alt.Chart(df, title="Ploting using Altair in Streamlit").mark_bar().encode(
|
161 |
+
x='Animals',
|
162 |
+
y='Number'
|
163 |
+
)
|
164 |
+
#render with Streamlit
|
165 |
+
st.altair_chart(chrt, use_container_width=True)
|
166 |
+
#Plotly
|
167 |
+
#define data
|
168 |
+
df = pd.DataFrame()
|
169 |
+
df["Animals"] = animals
|
170 |
+
df["Number"] = number
|
171 |
+
#create plot
|
172 |
+
fig1 = px.bar(df, x='Animals', y='Number', title="Ploting using Plotly in Streamlit")
|
173 |
+
# Plot!
|
174 |
+
st.plotly_chart(fig1, use_container_width=True)
|
175 |
+
|
176 |
+
#data
|
177 |
+
df = pd.DataFrame()
|
178 |
+
df["Animals"] = animals
|
179 |
+
df["Number"] = number
|
180 |
+
#visualization
|
181 |
+
st.vega_lite_chart(df, {
|
182 |
+
'mark': {'type': 'bar', 'tooltip': True},
|
183 |
+
'encoding': {
|
184 |
+
'x': {'field': 'Animals', 'type': 'nominal'},
|
185 |
+
'y': {'field': 'Number', 'type': 'quantitative'},
|
186 |
+
},
|
187 |
+
}, use_container_width=True)
|
188 |
+
|
189 |
+
#Maps
|
190 |
+
import pandas as pd
|
191 |
+
states = pd.read_html('https://developers.google.com/public-data/docs/canonical/states_csv')[0]
|
192 |
+
states.columns = ['state', 'lat', 'lon', 'name']
|
193 |
+
states = states.drop(['state', 'name'], axis = 1)
|
194 |
+
|
195 |
+
st.map(states)
|
196 |
+
|
197 |
+
#Components
|
198 |
+
from st_aggrid import AgGrid
|
199 |
+
AgGrid(df)
|
200 |
+
|
201 |
+
#Statefulnness
|
202 |
+
import streamlit as st
|
203 |
+
|
204 |
+
st.title('Streamlit Counter Example')
|
205 |
+
count = 0
|
206 |
+
|
207 |
+
add = st.button('Addition')
|
208 |
+
if add:
|
209 |
+
count += 1
|
210 |
+
|
211 |
+
st.write('Count = ', count)
|
212 |
+
|
213 |
+
|
214 |
+
import streamlit as st
|
215 |
+
|
216 |
+
st.title('Counter Session State')
|
217 |
+
if 'count' not in st.session_state:
|
218 |
+
st.session_state.count = 0
|
219 |
+
|
220 |
+
increment = st.button('Add')
|
221 |
+
if increment:
|
222 |
+
st.session_state.count += 1
|
223 |
+
|
224 |
+
st.write('Count = ', st.session_state.count)
|
225 |
+
|
226 |
+
#Layout
|
227 |
+
col1, col2 = st.columns(2)
|
228 |
+
|
229 |
+
with col1:
|
230 |
+
st.altair_chart(chrt)
|
231 |
+
with col2:
|
232 |
+
st.plotly_chart(fig1, use_container_width=True)
|
233 |
+
with st.beta_container():
|
234 |
+
st.plotly_chart(figx, use_container_width=True)
|
235 |
+
|
236 |
+
|
237 |
+
|
238 |
+
#Add side widget
|
239 |
+
|
240 |
+
def your_widget(key):
|
241 |
+
st.subheader('Hi! Welcome')
|
242 |
+
return st.button(key + "Step")
|
243 |
+
|
244 |
+
# Displayed in the main area
|
245 |
+
clicked = your_widget("First")
|
246 |
+
|
247 |
+
# Shown within an expander
|
248 |
+
your_expander = st.expander("Expand", expanded=True)
|
249 |
+
with your_expander:
|
250 |
+
clicked = your_widget("Second")
|
251 |
+
|
252 |
+
# Shown in the st.sidebar!
|
253 |
+
with st.sidebar:
|
254 |
+
clicked = your_widget("Last")
|
255 |
+
#Session State
|
256 |
+
# Initialization
|
257 |
+
if 'key' not in st.session_state:
|
258 |
+
st.session_state['key'] = 'value'
|
259 |
+
|
260 |
+
|
261 |
+
|
262 |
+
# Session State also supports attribute based syntax
|
263 |
+
if 'key' not in st.session_state:
|
264 |
+
st.session_state.key = 'value'
|
265 |
+
|
266 |
+
st.session_state.key = 'value x' # New Attribute API
|
267 |
+
st.session_state['key'] = 'value x' # New Dictionary like API
|
268 |
+
|
269 |
+
st.write(st.session_state)
|
270 |
+
|
271 |
+
#Uploading files
|
272 |
+
|
273 |
+
import streamlit as st
|
274 |
+
|
275 |
+
#upload single file
|
276 |
+
file = st.file_uploader("Please select a file to upload")
|
277 |
+
if file is not None:
|
278 |
+
#Can be used wherever a "file-like" object is accepted:
|
279 |
+
df= pd.read_csv(file)
|
280 |
+
st.dataframe(df)
|
281 |
+
|
282 |
+
#Multiple files
|
283 |
+
#adding a file uploader to accept multiple CSV file
|
284 |
+
uploaded_files = st.file_uploader("Please select a CSV file", accept_multiple_files=True)
|
285 |
+
for file in uploaded_files:
|
286 |
+
df = pd.read_csv(file)
|
287 |
+
st.write("File uploaded:", file.name)
|
288 |
+
st.dataframe(df)
|
289 |
+
#Uploading and Processing
|
290 |
+
#upload single file
|
291 |
+
from PIL import Image
|
292 |
+
from PIL import ImageEnhance
|
293 |
+
def load_image(image):
|
294 |
+
img = Image.open(image)
|
295 |
+
return img
|
296 |
+
|
297 |
+
file = st.file_uploader("Please select image to upload and process")
|
298 |
+
if file is not None:
|
299 |
+
image = Image.open(file)
|
300 |
+
fig = plt.figure()
|
301 |
+
st.subheader("Original Image")
|
302 |
+
plt.imshow(image)
|
303 |
+
st.pyplot(fig)
|
304 |
+
fig = plt.figure()
|
305 |
+
contrast = ImageEnhance.Contrast(image).enhance(12)
|
306 |
+
plt.imshow(contrast)
|
307 |
+
st.subheader("Preprocessed Image")
|
308 |
+
st.pyplot(fig)
|
309 |
+
|
310 |
+
|
311 |
+
#Image classification
|
312 |
+
import keras
|
313 |
+
from PIL import Image, ImageOps
|
314 |
+
import numpy as np
|
315 |
+
|
316 |
+
|
317 |
+
|
318 |
+
import streamlit as st
|
319 |
+
import streamlit as st
|
320 |
+
from transformers import pipeline
|
321 |
+
|
322 |
+
'''Hugging Face'''
|
323 |
+
|
324 |
+
import streamlit as st
|
325 |
+
from transformers import pipeline
|
326 |
+
|
327 |
+
if __name__ == "__main__":
|
328 |
+
|
329 |
+
# Define the title of the and its description
|
330 |
+
st.title("Answering questions using NLP through Streamlit interface")
|
331 |
+
st.write("Pose questions, get answers")
|
332 |
+
|
333 |
+
# Load file
|
334 |
+
|
335 |
+
raw_text = st.text_area(label="Enter a text here")
|
336 |
+
if raw_text != None and raw_text != '':
|
337 |
+
|
338 |
+
# Display text
|
339 |
+
with st.expander("Show question"):
|
340 |
+
st.write(raw_text)
|
341 |
+
|
342 |
+
# Conduct question answering using the pipeline
|
343 |
+
question_answerer = pipeline('question-answering')
|
344 |
+
|
345 |
+
answer = ''
|
346 |
+
question = st.text_input('Ask a question')
|
347 |
+
|
348 |
+
if question != '' and raw_text != '':
|
349 |
+
answer = question_answerer({
|
350 |
+
'question': question,
|
351 |
+
'context': raw_text
|
352 |
+
})
|
353 |
+
|
354 |
+
st.write(answer)
|
355 |
+
|
356 |
+
|
357 |
+
|
358 |
+
|
359 |
+
|
360 |
+
|
361 |
+
|
362 |
+
|
363 |
+
|
364 |
+
|
365 |
+
|
Streamlit/hugginface.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''Hugging Face'''
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
if __name__ == "__main__":
|
7 |
+
|
8 |
+
# Define the title of the and its description
|
9 |
+
st.title("Answering questions using NLP through Streamlit interface")
|
10 |
+
st.write("Pose questions, get answers")
|
11 |
+
|
12 |
+
# Load file
|
13 |
+
|
14 |
+
raw_text = st.text_area(label="Enter a text here")
|
15 |
+
if raw_text != None and raw_text != '':
|
16 |
+
|
17 |
+
# Display text
|
18 |
+
with st.expander("Show question"):
|
19 |
+
st.write(raw_text)
|
20 |
+
|
21 |
+
# Conduct question answering using the pipeline
|
22 |
+
question_answerer = pipeline('question-answering')
|
23 |
+
|
24 |
+
answer = ''
|
25 |
+
question = st.text_input('Ask a question')
|
26 |
+
|
27 |
+
if question != '' and raw_text != '':
|
28 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"id": "f2054953",
|
6 |
+
"metadata": {},
|
7 |
+
"source": [
|
8 |
+
"### Introduction"
|
9 |
+
]
|
10 |
+
},
|
11 |
+
{
|
12 |
+
"cell_type": "markdown",
|
13 |
+
"id": "defdb7b1",
|
14 |
+
"metadata": {},
|
15 |
+
"source": [
|
16 |
+
"#### Installing streamlit\n",
|
17 |
+
"Streamlit allows fast conversion of data scripts into web apps.\n",
|
18 |
+
"\n",
|
19 |
+
"TO get started you first need to have Streamlit installed. To do so, just run the command below:"
|
20 |
+
]
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"cell_type": "code",
|
24 |
+
"execution_count": null,
|
25 |
+
"id": "b89854fb",
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [],
|
28 |
+
"source": [
|
29 |
+
"pip install streamlit\n",
|
30 |
+
"#using conda\n",
|
31 |
+
"conda install streamlit"
|
32 |
+
]
|
33 |
+
},
|
34 |
+
{
|
35 |
+
"cell_type": "markdown",
|
36 |
+
"id": "1506fa44",
|
37 |
+
"metadata": {},
|
38 |
+
"source": [
|
39 |
+
"#### How to run Streamlit applications\n",
|
40 |
+
"\n",
|
41 |
+
"To execute Streamlit apps, the following command is used:"
|
42 |
+
]
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"cell_type": "code",
|
46 |
+
"execution_count": null,
|
47 |
+
"id": "e9c700f5",
|
48 |
+
"metadata": {},
|
49 |
+
"outputs": [],
|
50 |
+
"source": [
|
51 |
+
"\n",
|
52 |
+
"file = st.file_uploader(\"Please select a file to upload\")\n",
|
53 |
+
"\n",
|
54 |
+
"if file is not None:\n",
|
55 |
+
"\n",
|
56 |
+
" #Read the file as bytes:\n",
|
57 |
+
"\n",
|
58 |
+
" bytes_df = file.getvalue()\n",
|
59 |
+
"\n",
|
60 |
+
" st.write(bytes_df)\n",
|
61 |
+
"\n",
|
62 |
+
" #Convert the file to a string-based IO:\n",
|
63 |
+
"\n",
|
64 |
+
" stringio = StringIO(file.getvalue().decode(\"utf-8\"))\n",
|
65 |
+
"\n",
|
66 |
+
" st.write(stringio)\n",
|
67 |
+
"\n",
|
68 |
+
" #Read the file as a string file:\n",
|
69 |
+
"\n",
|
70 |
+
" string_df = stringio.read()\n",
|
71 |
+
"\n",
|
72 |
+
" st.write(string_df)\n",
|
73 |
+
"\n",
|
74 |
+
" #Usable whenever a \"file-like\" object is accepted:\n",
|
75 |
+
"\n",
|
76 |
+
" data = pd.read_csv(file)\n",
|
77 |
+
"\n",
|
78 |
+
" st.write(data)\n"
|
79 |
+
]
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"cell_type": "code",
|
83 |
+
"execution_count": null,
|
84 |
+
"id": "ea8d494d",
|
85 |
+
"metadata": {},
|
86 |
+
"outputs": [],
|
87 |
+
"source": [
|
88 |
+
"#Add side widget\n",
|
89 |
+
"\n",
|
90 |
+
"def your_widget(key):\n",
|
91 |
+
" st.subheader('Hi! Welcome')\n",
|
92 |
+
" return st.button(key + \"Step\")\n",
|
93 |
+
"\n",
|
94 |
+
"# Displayed in the main area\n",
|
95 |
+
"clicked = your_widget(\"First\")\n",
|
96 |
+
"\n",
|
97 |
+
"# Shown within an expander\n",
|
98 |
+
"your_expander = st.expander(\"Expand\", expanded=True)\n",
|
99 |
+
"with your_expander:\n",
|
100 |
+
" clicked = your_widget(\"Second\")\n",
|
101 |
+
"\n",
|
102 |
+
"# Shown in the st.sidebar!\n",
|
103 |
+
"with st.sidebar:\n",
|
104 |
+
" clicked = your_widget(\"Last\")\n"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
{
|
108 |
+
"cell_type": "markdown",
|
109 |
+
"id": "ad037296",
|
110 |
+
"metadata": {},
|
111 |
+
"source": [
|
112 |
+
"### Streamlit Widgets"
|
113 |
+
]
|
114 |
+
},
|
115 |
+
{
|
116 |
+
"cell_type": "code",
|
117 |
+
"execution_count": 3,
|
118 |
+
"id": "c05b2e95",
|
119 |
+
"metadata": {},
|
120 |
+
"outputs": [
|
121 |
+
{
|
122 |
+
"name": "stderr",
|
123 |
+
"output_type": "stream",
|
124 |
+
"text": [
|
125 |
+
"2022-08-07 19:15:58.314 INFO numexpr.utils: NumExpr defaulting to 4 threads.\n"
|
126 |
+
]
|
127 |
+
}
|
128 |
+
],
|
129 |
+
"source": [
|
130 |
+
"import streamlit as st"
|
131 |
+
]
|
132 |
+
},
|
133 |
+
{
|
134 |
+
"cell_type": "code",
|
135 |
+
"execution_count": null,
|
136 |
+
"id": "839cd69d",
|
137 |
+
"metadata": {},
|
138 |
+
"outputs": [
|
139 |
+
{
|
140 |
+
"name": "stdout",
|
141 |
+
"output_type": "stream",
|
142 |
+
"text": [
|
143 |
+
"2022-08-08 14:43:23.739 INFO numexpr.utils: NumExpr defaulting to 4 threads.\n",
|
144 |
+
"\u001b[0m\n",
|
145 |
+
"\u001b[34m\u001b[1m You can now view your Streamlit app in your browser.\u001b[0m\n",
|
146 |
+
"\u001b[0m\n",
|
147 |
+
"\u001b[34m Local URL: \u001b[0m\u001b[1mhttp://localhost:8501\u001b[0m\n",
|
148 |
+
"\u001b[34m Network URL: \u001b[0m\u001b[1mhttp://10.16.0.5:8501\u001b[0m\n",
|
149 |
+
"\u001b[0m\n",
|
150 |
+
"app.py:144: UserWarning:\n",
|
151 |
+
"\n",
|
152 |
+
"Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n",
|
153 |
+
"\n",
|
154 |
+
"app.py:144: UserWarning:\n",
|
155 |
+
"\n",
|
156 |
+
"Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n",
|
157 |
+
"\n",
|
158 |
+
"app.py:150: UserWarning:\n",
|
159 |
+
"\n",
|
160 |
+
"Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n",
|
161 |
+
"\n",
|
162 |
+
"app.py:150: UserWarning:\n",
|
163 |
+
"\n",
|
164 |
+
"Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.\n",
|
165 |
+
"\n",
|
166 |
+
"2022-08-08 14:44:02.778127: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n",
|
167 |
+
"2022-08-08 14:44:02.778176: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n",
|
168 |
+
"2022-08-08 14:44:09.662 Uncaught app exception\n",
|
169 |
+
"Traceback (most recent call last):\n",
|
170 |
+
" File \"/home/kamanda/anaconda/lib/python3.9/site-packages/streamlit/scriptrunner/script_runner.py\", line 557, in _run_script\n",
|
171 |
+
" exec(code, module.__dict__)\n",
|
172 |
+
" File \"app.py\", line 320, in <module>\n",
|
173 |
+
" from transformers import pipeline\n",
|
174 |
+
"ImportError: cannot import name 'pipeline' from 'transformers' (/home/kamanda/anaconda/lib/python3.9/site-packages/transformers/__init__.py)\n"
|
175 |
+
]
|
176 |
+
}
|
177 |
+
],
|
178 |
+
"source": [
|
179 |
+
" #run an app\n",
|
180 |
+
"!streamlit run app.py"
|
181 |
+
]
|
182 |
+
},
|
183 |
+
{
|
184 |
+
"cell_type": "code",
|
185 |
+
"execution_count": 4,
|
186 |
+
"id": "393dfc57",
|
187 |
+
"metadata": {},
|
188 |
+
"outputs": [
|
189 |
+
{
|
190 |
+
"data": {
|
191 |
+
"text/html": [
|
192 |
+
"<div>\n",
|
193 |
+
"<style scoped>\n",
|
194 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
195 |
+
" vertical-align: middle;\n",
|
196 |
+
" }\n",
|
197 |
+
"\n",
|
198 |
+
" .dataframe tbody tr th {\n",
|
199 |
+
" vertical-align: top;\n",
|
200 |
+
" }\n",
|
201 |
+
"\n",
|
202 |
+
" .dataframe thead th {\n",
|
203 |
+
" text-align: right;\n",
|
204 |
+
" }\n",
|
205 |
+
"</style>\n",
|
206 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
207 |
+
" <thead>\n",
|
208 |
+
" <tr style=\"text-align: right;\">\n",
|
209 |
+
" <th></th>\n",
|
210 |
+
" <th>Student Name</th>\n",
|
211 |
+
" <th>Marks</th>\n",
|
212 |
+
" </tr>\n",
|
213 |
+
" </thead>\n",
|
214 |
+
" <tbody>\n",
|
215 |
+
" <tr>\n",
|
216 |
+
" <th>0</th>\n",
|
217 |
+
" <td>Amelia Kami</td>\n",
|
218 |
+
" <td>82</td>\n",
|
219 |
+
" </tr>\n",
|
220 |
+
" <tr>\n",
|
221 |
+
" <th>1</th>\n",
|
222 |
+
" <td>Antoinne Mark</td>\n",
|
223 |
+
" <td>76</td>\n",
|
224 |
+
" </tr>\n",
|
225 |
+
" <tr>\n",
|
226 |
+
" <th>2</th>\n",
|
227 |
+
" <td>Peter Zen</td>\n",
|
228 |
+
" <td>96</td>\n",
|
229 |
+
" </tr>\n",
|
230 |
+
" <tr>\n",
|
231 |
+
" <th>3</th>\n",
|
232 |
+
" <td>North Kim</td>\n",
|
233 |
+
" <td>68</td>\n",
|
234 |
+
" </tr>\n",
|
235 |
+
" </tbody>\n",
|
236 |
+
"</table>\n",
|
237 |
+
"</div>"
|
238 |
+
],
|
239 |
+
"text/plain": [
|
240 |
+
" Student Name Marks\n",
|
241 |
+
"0 Amelia Kami 82\n",
|
242 |
+
"1 Antoinne Mark 76\n",
|
243 |
+
"2 Peter Zen 96\n",
|
244 |
+
"3 North Kim 68"
|
245 |
+
]
|
246 |
+
},
|
247 |
+
"execution_count": 4,
|
248 |
+
"metadata": {},
|
249 |
+
"output_type": "execute_result"
|
250 |
+
}
|
251 |
+
],
|
252 |
+
"source": [
|
253 |
+
"students = [\"Amelia Kami\", \"Antoinne Mark\", \"Peter Zen\", \"North Kim\"]\n",
|
254 |
+
"\n",
|
255 |
+
"marks = [82, 76, 96, 68]\n",
|
256 |
+
"\n",
|
257 |
+
"import pandas as pd\n",
|
258 |
+
"\n",
|
259 |
+
"df = pd.DataFrame()\n",
|
260 |
+
"\n",
|
261 |
+
"df[\"Student Name\"] = students\n",
|
262 |
+
"\n",
|
263 |
+
"df[\"Marks\"] = marks\n",
|
264 |
+
"df"
|
265 |
+
]
|
266 |
+
},
|
267 |
+
{
|
268 |
+
"cell_type": "code",
|
269 |
+
"execution_count": 16,
|
270 |
+
"id": "e0965273",
|
271 |
+
"metadata": {},
|
272 |
+
"outputs": [
|
273 |
+
{
|
274 |
+
"data": {
|
275 |
+
"text/plain": [
|
276 |
+
"AgGridReturn(data= Animals Number\n",
|
277 |
+
"0 Zebras 65\n",
|
278 |
+
"1 Elephants 72\n",
|
279 |
+
"2 Rhinos 77\n",
|
280 |
+
"3 Leopards 59, selected_rows=[])"
|
281 |
+
]
|
282 |
+
},
|
283 |
+
"execution_count": 16,
|
284 |
+
"metadata": {},
|
285 |
+
"output_type": "execute_result"
|
286 |
+
}
|
287 |
+
],
|
288 |
+
"source": [
|
289 |
+
"import matplotlib.pyplot as plt\n",
|
290 |
+
"import numpy as np\n",
|
291 |
+
"fig = plt.figure()\n",
|
292 |
+
"ax = fig.add_axes([0,0,1,1])\n",
|
293 |
+
"animals = [\"Zebras\", \"Elephants\", \"Rhinos\", \"Leopards\"]\n",
|
294 |
+
"number = [65, 72, 77, 59]\n",
|
295 |
+
"ax.bar(animals, number)\n",
|
296 |
+
"fig = plt.show()\n",
|
297 |
+
"st.pyplot(fig)\n",
|
298 |
+
"#Seaborn\n",
|
299 |
+
"import seaborn as sns\n",
|
300 |
+
"fig = plt.figure()\n",
|
301 |
+
"ax = sns.barplot(x = animals, y = number)\n",
|
302 |
+
"fig = plt.show()\n",
|
303 |
+
"st.pyplot(fig)\n",
|
304 |
+
"\n",
|
305 |
+
"#Altair\n",
|
306 |
+
"#define data\n",
|
307 |
+
"df = pd.DataFrame()\n",
|
308 |
+
"\n",
|
309 |
+
"df[\"Animals\"] = animals\n",
|
310 |
+
"df[\"Number\"] = number\n",
|
311 |
+
"#Components\n",
|
312 |
+
"from st_aggrid import AgGrid\n",
|
313 |
+
"AgGrid(df)\n"
|
314 |
+
]
|
315 |
+
},
|
316 |
+
{
|
317 |
+
"cell_type": "code",
|
318 |
+
"execution_count": 6,
|
319 |
+
"id": "c21f896b",
|
320 |
+
"metadata": {},
|
321 |
+
"outputs": [
|
322 |
+
{
|
323 |
+
"name": "stderr",
|
324 |
+
"output_type": "stream",
|
325 |
+
"text": [
|
326 |
+
"2022-08-07 19:30:27.154 \n",
|
327 |
+
" \u001b[33m\u001b[1mWarning:\u001b[0m to view this Streamlit app on a browser, run it with the following\n",
|
328 |
+
" command:\n",
|
329 |
+
"\n",
|
330 |
+
" streamlit run /home/kamanda/anaconda/lib/python3.9/site-packages/ipykernel_launcher.py [ARGUMENTS]\n"
|
331 |
+
]
|
332 |
+
}
|
333 |
+
],
|
334 |
+
"source": []
|
335 |
+
}
|
336 |
+
],
|
337 |
+
"metadata": {
|
338 |
+
"kernelspec": {
|
339 |
+
"display_name": "Python 3 (ipykernel)",
|
340 |
+
"language": "python",
|
341 |
+
"name": "python3"
|
342 |
+
},
|
343 |
+
"language_info": {
|
344 |
+
"codemirror_mode": {
|
345 |
+
"name": "ipython",
|
346 |
+
"version": 3
|
347 |
+
},
|
348 |
+
"file_extension": ".py",
|
349 |
+
"mimetype": "text/x-python",
|
350 |
+
"name": "python",
|
351 |
+
"nbconvert_exporter": "python",
|
352 |
+
"pygments_lexer": "ipython3",
|
353 |
+
"version": "3.9.12"
|
354 |
+
}
|
355 |
+
},
|
356 |
+
"nbformat": 4,
|
357 |
+
"nbformat_minor": 5
|
358 |
+
}
|
Untitled1.ipynb
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [],
|
3 |
+
"metadata": {},
|
4 |
+
"nbformat": 4,
|
5 |
+
"nbformat_minor": 5
|
6 |
+
}
|
Untitled2.ipynb
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [],
|
3 |
+
"metadata": {},
|
4 |
+
"nbformat": 4,
|
5 |
+
"nbformat_minor": 5
|
6 |
+
}
|
app.py
ADDED
@@ -0,0 +1,365 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
3 |
+
#st.markdown()
|
4 |
+
st.title("Document Title")
|
5 |
+
st.header("Article header")
|
6 |
+
st.subheader("Article subheader")
|
7 |
+
st.code("y = mx + c")
|
8 |
+
st.latex("\ int a y^2 \ , dy")
|
9 |
+
st.text("This is a chair!")
|
10 |
+
st.markdown('Staying hydrated is **_very_ cool**.')
|
11 |
+
|
12 |
+
students = ["Amelia Kami", "Antoinne Mark", "Peter Zen", "North Kim"]
|
13 |
+
|
14 |
+
marks = [82, 76, 96, 68]
|
15 |
+
|
16 |
+
import pandas as pd
|
17 |
+
|
18 |
+
df = pd.DataFrame()
|
19 |
+
|
20 |
+
df["Student Name"] = students
|
21 |
+
|
22 |
+
df["Marks"] = marks
|
23 |
+
#save to dataframe
|
24 |
+
df.to_csv("students.csv", index = False)
|
25 |
+
#display
|
26 |
+
st.dataframe(df)
|
27 |
+
|
28 |
+
#Static table
|
29 |
+
st.table(df)
|
30 |
+
|
31 |
+
#Metrics
|
32 |
+
st.metric("KPI", 56, 3)
|
33 |
+
#Json
|
34 |
+
st.json(df.to_dict())
|
35 |
+
|
36 |
+
#Code
|
37 |
+
#average of a list
|
38 |
+
code = '''def cal_average(numbers):
|
39 |
+
sum_number = 0
|
40 |
+
for t in numbers:
|
41 |
+
sum_number = sum_number + t
|
42 |
+
|
43 |
+
average = sum_number / len(numbers)
|
44 |
+
return average'''
|
45 |
+
st.code(code, language='python')
|
46 |
+
#progress bar
|
47 |
+
|
48 |
+
import streamlit as st
|
49 |
+
import time
|
50 |
+
|
51 |
+
# Sample Progress bar
|
52 |
+
#bar_p = st.progress(0)
|
53 |
+
|
54 |
+
#for percentage_complete in range(100):
|
55 |
+
#time.sleep(0.1)
|
56 |
+
#bar_p.progress(percentage_complete + 1)
|
57 |
+
|
58 |
+
#with st.spinner('Please wait...'):
|
59 |
+
#time.sleep(5)
|
60 |
+
#st.write('Complete!')
|
61 |
+
|
62 |
+
|
63 |
+
#Displaying an image using Streamlit
|
64 |
+
from PIL import Image
|
65 |
+
image = Image.open('media/ann-savchenko-H0h_89iFsWs-unsplash.jpg')
|
66 |
+
|
67 |
+
#st.image(image, caption='Sunset grass backgrounds')
|
68 |
+
|
69 |
+
#plotly
|
70 |
+
import plotly.express as px
|
71 |
+
# This dataframe has 244 rows, but 4 unique entries for the `day` variable
|
72 |
+
df = px.data.tips()
|
73 |
+
figx = px.pie(df, values='tip', names='day', title='Tips per day')
|
74 |
+
# Plot!
|
75 |
+
st.plotly_chart(figx, use_container_width=True)
|
76 |
+
|
77 |
+
#Altair
|
78 |
+
import altair as alt
|
79 |
+
import streamlit as st
|
80 |
+
import numpy as np
|
81 |
+
|
82 |
+
df = pd.DataFrame(
|
83 |
+
np.random.randn(300, 4),
|
84 |
+
columns=['a', 'b', 'c', 'd'])
|
85 |
+
|
86 |
+
chrt = alt.Chart(df).mark_circle().encode(
|
87 |
+
x='a', y='b', size='c', color='c', tooltip=['a', 'b', 'c', 'd'])
|
88 |
+
|
89 |
+
st.altair_chart(chrt, use_container_width=True)
|
90 |
+
|
91 |
+
#Matplotlib
|
92 |
+
import matplotlib.pyplot as plt
|
93 |
+
import numpy as np
|
94 |
+
|
95 |
+
arr = np.random.normal(1, 1, size=1000)
|
96 |
+
fig, ax = plt.subplots()
|
97 |
+
ax.hist(arr, bins=30)
|
98 |
+
plt.grid()
|
99 |
+
st.pyplot(fig)
|
100 |
+
|
101 |
+
#Interactive widgets
|
102 |
+
st.button("Click here")
|
103 |
+
#st.download_button("Download audio", file)
|
104 |
+
selected = st.checkbox("Accept terms")
|
105 |
+
choice = st.radio("Select one", ["Apples", "Oranges"])
|
106 |
+
|
107 |
+
|
108 |
+
option = st.selectbox(
|
109 |
+
'How would you like to receive your package?',
|
110 |
+
('By air', 'By sea', 'By rail'))
|
111 |
+
|
112 |
+
st.write('You selected:', option)
|
113 |
+
import datetime
|
114 |
+
day = st.date_input(
|
115 |
+
"When is your birthday?",
|
116 |
+
datetime.date(2022, 7, 6))
|
117 |
+
st.write('Your birthday is:', day)
|
118 |
+
|
119 |
+
color = st.color_picker('Choose A Color', '#00FFAA')
|
120 |
+
st.write('The selected color is', color)
|
121 |
+
|
122 |
+
|
123 |
+
@st.cache
|
124 |
+
def fetch_data():
|
125 |
+
df = pd.read_csv("students.csv")
|
126 |
+
return df
|
127 |
+
|
128 |
+
#data = fetch_data()
|
129 |
+
|
130 |
+
#Visualization
|
131 |
+
|
132 |
+
import matplotlib.pyplot as plt
|
133 |
+
import numpy as np
|
134 |
+
|
135 |
+
#Matplotlib
|
136 |
+
|
137 |
+
import matplotlib.pyplot as plt
|
138 |
+
import numpy as np
|
139 |
+
fig = plt.figure()
|
140 |
+
ax = fig.add_axes([0,0,1,1])
|
141 |
+
animals = ["Zebras", "Elephants", "Rhinos", "Leopards"]
|
142 |
+
number = [65, 72, 77, 59]
|
143 |
+
ax.bar(animals, number)
|
144 |
+
fig = plt.show()
|
145 |
+
st.pyplot(fig)
|
146 |
+
#Seaborn
|
147 |
+
import seaborn as sns
|
148 |
+
fig = plt.figure()
|
149 |
+
ax = sns.barplot(x = animals, y = number)
|
150 |
+
fig = plt.show()
|
151 |
+
st.pyplot(fig)
|
152 |
+
|
153 |
+
#Altair
|
154 |
+
#define data
|
155 |
+
df = pd.DataFrame()
|
156 |
+
|
157 |
+
df["Animals"] = animals
|
158 |
+
df["Number"] = number
|
159 |
+
#create chart
|
160 |
+
chrt = alt.Chart(df, title="Ploting using Altair in Streamlit").mark_bar().encode(
|
161 |
+
x='Animals',
|
162 |
+
y='Number'
|
163 |
+
)
|
164 |
+
#render with Streamlit
|
165 |
+
st.altair_chart(chrt, use_container_width=True)
|
166 |
+
#Plotly
|
167 |
+
#define data
|
168 |
+
df = pd.DataFrame()
|
169 |
+
df["Animals"] = animals
|
170 |
+
df["Number"] = number
|
171 |
+
#create plot
|
172 |
+
fig1 = px.bar(df, x='Animals', y='Number', title="Ploting using Plotly in Streamlit")
|
173 |
+
# Plot!
|
174 |
+
st.plotly_chart(fig1, use_container_width=True)
|
175 |
+
|
176 |
+
#data
|
177 |
+
df = pd.DataFrame()
|
178 |
+
df["Animals"] = animals
|
179 |
+
df["Number"] = number
|
180 |
+
#visualization
|
181 |
+
st.vega_lite_chart(df, {
|
182 |
+
'mark': {'type': 'bar', 'tooltip': True},
|
183 |
+
'encoding': {
|
184 |
+
'x': {'field': 'Animals', 'type': 'nominal'},
|
185 |
+
'y': {'field': 'Number', 'type': 'quantitative'},
|
186 |
+
},
|
187 |
+
}, use_container_width=True)
|
188 |
+
|
189 |
+
#Maps
|
190 |
+
import pandas as pd
|
191 |
+
states = pd.read_html('https://developers.google.com/public-data/docs/canonical/states_csv')[0]
|
192 |
+
states.columns = ['state', 'lat', 'lon', 'name']
|
193 |
+
states = states.drop(['state', 'name'], axis = 1)
|
194 |
+
|
195 |
+
st.map(states)
|
196 |
+
|
197 |
+
#Components
|
198 |
+
from st_aggrid import AgGrid
|
199 |
+
AgGrid(df)
|
200 |
+
|
201 |
+
#Statefulnness
|
202 |
+
import streamlit as st
|
203 |
+
|
204 |
+
st.title('Streamlit Counter Example')
|
205 |
+
count = 0
|
206 |
+
|
207 |
+
add = st.button('Addition')
|
208 |
+
if add:
|
209 |
+
count += 1
|
210 |
+
|
211 |
+
st.write('Count = ', count)
|
212 |
+
|
213 |
+
|
214 |
+
import streamlit as st
|
215 |
+
|
216 |
+
st.title('Counter Session State')
|
217 |
+
if 'count' not in st.session_state:
|
218 |
+
st.session_state.count = 0
|
219 |
+
|
220 |
+
increment = st.button('Add')
|
221 |
+
if increment:
|
222 |
+
st.session_state.count += 1
|
223 |
+
|
224 |
+
st.write('Count = ', st.session_state.count)
|
225 |
+
|
226 |
+
#Layout
|
227 |
+
col1, col2 = st.columns(2)
|
228 |
+
|
229 |
+
with col1:
|
230 |
+
st.altair_chart(chrt)
|
231 |
+
with col2:
|
232 |
+
st.plotly_chart(fig1, use_container_width=True)
|
233 |
+
with st.beta_container():
|
234 |
+
st.plotly_chart(figx, use_container_width=True)
|
235 |
+
|
236 |
+
|
237 |
+
|
238 |
+
#Add side widget
|
239 |
+
|
240 |
+
def your_widget(key):
|
241 |
+
st.subheader('Hi! Welcome')
|
242 |
+
return st.button(key + "Step")
|
243 |
+
|
244 |
+
# Displayed in the main area
|
245 |
+
clicked = your_widget("First")
|
246 |
+
|
247 |
+
# Shown within an expander
|
248 |
+
your_expander = st.expander("Expand", expanded=True)
|
249 |
+
with your_expander:
|
250 |
+
clicked = your_widget("Second")
|
251 |
+
|
252 |
+
# Shown in the st.sidebar!
|
253 |
+
with st.sidebar:
|
254 |
+
clicked = your_widget("Last")
|
255 |
+
#Session State
|
256 |
+
# Initialization
|
257 |
+
if 'key' not in st.session_state:
|
258 |
+
st.session_state['key'] = 'value'
|
259 |
+
|
260 |
+
|
261 |
+
|
262 |
+
# Session State also supports attribute based syntax
|
263 |
+
if 'key' not in st.session_state:
|
264 |
+
st.session_state.key = 'value'
|
265 |
+
|
266 |
+
st.session_state.key = 'value x' # New Attribute API
|
267 |
+
st.session_state['key'] = 'value x' # New Dictionary like API
|
268 |
+
|
269 |
+
st.write(st.session_state)
|
270 |
+
|
271 |
+
#Uploading files
|
272 |
+
|
273 |
+
import streamlit as st
|
274 |
+
|
275 |
+
#upload single file
|
276 |
+
file = st.file_uploader("Please select a file to upload")
|
277 |
+
if file is not None:
|
278 |
+
#Can be used wherever a "file-like" object is accepted:
|
279 |
+
df= pd.read_csv(file)
|
280 |
+
st.dataframe(df)
|
281 |
+
|
282 |
+
#Multiple files
|
283 |
+
#adding a file uploader to accept multiple CSV file
|
284 |
+
uploaded_files = st.file_uploader("Please select a CSV file", accept_multiple_files=True)
|
285 |
+
for file in uploaded_files:
|
286 |
+
df = pd.read_csv(file)
|
287 |
+
st.write("File uploaded:", file.name)
|
288 |
+
st.dataframe(df)
|
289 |
+
#Uploading and Processing
|
290 |
+
#upload single file
|
291 |
+
from PIL import Image
|
292 |
+
from PIL import ImageEnhance
|
293 |
+
def load_image(image):
|
294 |
+
img = Image.open(image)
|
295 |
+
return img
|
296 |
+
|
297 |
+
file = st.file_uploader("Please select image to upload and process")
|
298 |
+
if file is not None:
|
299 |
+
image = Image.open(file)
|
300 |
+
fig = plt.figure()
|
301 |
+
st.subheader("Original Image")
|
302 |
+
plt.imshow(image)
|
303 |
+
st.pyplot(fig)
|
304 |
+
fig = plt.figure()
|
305 |
+
contrast = ImageEnhance.Contrast(image).enhance(12)
|
306 |
+
plt.imshow(contrast)
|
307 |
+
st.subheader("Preprocessed Image")
|
308 |
+
st.pyplot(fig)
|
309 |
+
|
310 |
+
|
311 |
+
#Image classification
|
312 |
+
import keras
|
313 |
+
from PIL import Image, ImageOps
|
314 |
+
import numpy as np
|
315 |
+
|
316 |
+
|
317 |
+
|
318 |
+
import streamlit as st
|
319 |
+
import streamlit as st
|
320 |
+
from transformers import pipeline
|
321 |
+
|
322 |
+
'''Hugging Face'''
|
323 |
+
|
324 |
+
import streamlit as st
|
325 |
+
from transformers import pipeline
|
326 |
+
|
327 |
+
if __name__ == "__main__":
|
328 |
+
|
329 |
+
# Define the title of the and its description
|
330 |
+
st.title("Answering questions using NLP through Streamlit interface")
|
331 |
+
st.write("Pose questions, get answers")
|
332 |
+
|
333 |
+
# Load file
|
334 |
+
|
335 |
+
raw_text = st.text_area(label="Enter a text here")
|
336 |
+
if raw_text != None and raw_text != '':
|
337 |
+
|
338 |
+
# Display text
|
339 |
+
with st.expander("Show question"):
|
340 |
+
st.write(raw_text)
|
341 |
+
|
342 |
+
# Conduct question answering using the pipeline
|
343 |
+
question_answerer = pipeline('question-answering')
|
344 |
+
|
345 |
+
answer = ''
|
346 |
+
question = st.text_input('Ask a question')
|
347 |
+
|
348 |
+
if question != '' and raw_text != '':
|
349 |
+
answer = question_answerer({
|
350 |
+
'question': question,
|
351 |
+
'context': raw_text
|
352 |
+
})
|
353 |
+
|
354 |
+
st.write(answer)
|
355 |
+
|
356 |
+
|
357 |
+
|
358 |
+
|
359 |
+
|
360 |
+
|
361 |
+
|
362 |
+
|
363 |
+
|
364 |
+
|
365 |
+
|
hugginface.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''Hugging Face'''
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
if __name__ == "__main__":
|
7 |
+
|
8 |
+
# Define the title of the and its description
|
9 |
+
st.title("Answering questions using NLP through Streamlit interface")
|
10 |
+
st.write("Pose questions, get answers")
|
11 |
+
|
12 |
+
# Load file
|
13 |
+
|
14 |
+
raw_text = st.text_area(label="Enter a text here")
|
15 |
+
if raw_text != None and raw_text != '':
|
16 |
+
|
17 |
+
# Display text
|
18 |
+
with st.expander("Show question"):
|
19 |
+
st.write(raw_text)
|
20 |
+
|
21 |
+
# Conduct question answering using the pipeline
|
22 |
+
question_answerer = pipeline('question-answering')
|
23 |
+
|
24 |
+
answer = ''
|
25 |
+
question = st.text_input('Ask a question')
|
26 |
+
|
27 |
+
if question != '' and raw_text != '':
|
28 |
+
answer = question_answerer({
|
29 |
+
'question': question,
|
30 |
+
'context': raw_text
|
31 |
+
})
|
32 |
+
|
33 |
+
st.write(answer)
|
media/ann-savchenko-H0h_89iFsWs-unsplash.jpg
ADDED
media/data science components
ADDED
Binary file (12 kB). View file
|
|
media/data science components.png
ADDED
students.csv
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Student Name,Marks
|
2 |
+
Amelia Kami,82
|
3 |
+
Antoinne Mark,76
|
4 |
+
Peter Zen,96
|
5 |
+
North Kim,68
|
untitled.txt
ADDED
File without changes
|