Spaces:
Runtime error
Runtime error
Upload 6 files
Browse files- .DS_Store +0 -0
- Dockerfile +20 -0
- app.py +81 -0
- app.yaml +3 -0
- apps.py +140 -0
- requirements.txt +4 -0
.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Base Image to use
|
2 |
+
FROM python:3.7.9
|
3 |
+
|
4 |
+
#Expose port 8080
|
5 |
+
EXPOSE 8080
|
6 |
+
|
7 |
+
#Copy Requirements.txt file into app directory
|
8 |
+
COPY requirements.txt app/requirements.txt
|
9 |
+
|
10 |
+
#install all requirements in requirements.txt
|
11 |
+
RUN pip install -r app/requirements.txt
|
12 |
+
|
13 |
+
#Copy all files in current directory into app directory
|
14 |
+
COPY . /app
|
15 |
+
|
16 |
+
#Change Working Directory to app directory
|
17 |
+
WORKDIR /app
|
18 |
+
|
19 |
+
#Run the application on port 8080
|
20 |
+
ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8080", "--server.address=0.0.0.0"]
|
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
"""
|
4 |
+
|
5 |
+
|
6 |
+
@author: hamzafarooq@ MABA CLASS
|
7 |
+
"""
|
8 |
+
|
9 |
+
import streamlit as st
|
10 |
+
import pandas as pd
|
11 |
+
|
12 |
+
import plotly.express as px
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
st.title("Uma")
|
19 |
+
st.markdown("This is a demo Streamlit app.")
|
20 |
+
st.markdown("My name is UN, hello world!..")
|
21 |
+
st.markdown("This is v2")
|
22 |
+
|
23 |
+
@st.cache(persist=True)
|
24 |
+
def load_data():
|
25 |
+
df = pd.read_csv("https://datahub.io/machine-learning/iris/r/iris.csv")
|
26 |
+
return(df)
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
def run():
|
31 |
+
st.subheader("Iris Data Loaded into a Pandas Dataframe.")
|
32 |
+
|
33 |
+
df = load_data()
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
disp_head = st.sidebar.radio('Select DataFrame Display Option:',('Head', 'All'),index=0)
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
#Multi-Select
|
42 |
+
#sel_plot_cols = st.sidebar.multiselect("Select Columns For Scatter Plot",df.columns.to_list()[0:4],df.columns.to_list()[0:2])
|
43 |
+
|
44 |
+
#Select Box
|
45 |
+
#x_plot = st.sidebar.selectbox("Select X-axis Column For Scatter Plot",df.columns.to_list()[0:4],index=0)
|
46 |
+
#y_plot = st.sidebar.selectbox("Select Y-axis Column For Scatter Plot",df.columns.to_list()[0:4],index=1)
|
47 |
+
|
48 |
+
|
49 |
+
if disp_head=="Head":
|
50 |
+
st.dataframe(df.head())
|
51 |
+
else:
|
52 |
+
st.dataframe(df)
|
53 |
+
#st.table(df)
|
54 |
+
#st.write(df)
|
55 |
+
|
56 |
+
|
57 |
+
#Scatter Plot
|
58 |
+
fig = px.scatter(df, x=df["sepallength"], y=df["sepalwidth"], color="class",
|
59 |
+
size='petallength', hover_data=['petalwidth'])
|
60 |
+
|
61 |
+
fig.update_layout({
|
62 |
+
'plot_bgcolor': 'rgba(0, 0, 0, 0)'})
|
63 |
+
|
64 |
+
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='lightgray')
|
65 |
+
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='lightgray')
|
66 |
+
|
67 |
+
st.write("\n")
|
68 |
+
st.subheader("Scatter Plot")
|
69 |
+
st.plotly_chart(fig, use_container_width=True)
|
70 |
+
|
71 |
+
|
72 |
+
#Add images
|
73 |
+
#images = ["<image_url>"]
|
74 |
+
#st.image(images, width=600,use_container_width=True, caption=["Iris Flower"])
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
if __name__ == '__main__':
|
81 |
+
run()
|
app.yaml
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
service: default
|
2 |
+
runtime: custom
|
3 |
+
env: flex
|
apps.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
from sklearn.datasets import load_iris
|
6 |
+
from sklearn.ensemble import RandomForestClassifier
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
|
9 |
+
|
10 |
+
iris_data = load_iris()
|
11 |
+
# separate the data into features and target
|
12 |
+
features = pd.DataFrame(
|
13 |
+
iris_data.data, columns=iris_data.feature_names
|
14 |
+
)
|
15 |
+
target = pd.Series(iris_data.target)
|
16 |
+
|
17 |
+
# split the data into train and test
|
18 |
+
x_train, x_test, y_train, y_test = train_test_split(
|
19 |
+
features, target, test_size=0.2, stratify=target
|
20 |
+
)
|
21 |
+
|
22 |
+
|
23 |
+
class StreamlitApp:
|
24 |
+
|
25 |
+
def __init__(self):
|
26 |
+
self.model = RandomForestClassifier()
|
27 |
+
|
28 |
+
def train_data(self):
|
29 |
+
self.model.fit(x_train, y_train)
|
30 |
+
return self.model
|
31 |
+
|
32 |
+
def construct_sidebar(self):
|
33 |
+
|
34 |
+
cols = [col for col in features.columns]
|
35 |
+
|
36 |
+
st.sidebar.markdown(
|
37 |
+
'<p class="header-style">Iris Data Classification</p>',
|
38 |
+
unsafe_allow_html=True
|
39 |
+
)
|
40 |
+
sepal_length = st.sidebar.selectbox(
|
41 |
+
f"Select {cols[0]}",
|
42 |
+
sorted(features[cols[0]].unique())
|
43 |
+
)
|
44 |
+
|
45 |
+
sepal_width = st.sidebar.selectbox(
|
46 |
+
f"Select {cols[1]}",
|
47 |
+
sorted(features[cols[1]].unique())
|
48 |
+
)
|
49 |
+
|
50 |
+
petal_length = st.sidebar.selectbox(
|
51 |
+
f"Select {cols[2]}",
|
52 |
+
sorted(features[cols[2]].unique())
|
53 |
+
)
|
54 |
+
|
55 |
+
petal_width = st.sidebar.selectbox(
|
56 |
+
f"Select {cols[3]}",
|
57 |
+
sorted(features[cols[3]].unique())
|
58 |
+
)
|
59 |
+
values = [sepal_length, sepal_width, petal_length, petal_width]
|
60 |
+
|
61 |
+
return values
|
62 |
+
|
63 |
+
def plot_pie_chart(self, probabilities):
|
64 |
+
fig = go.Figure(
|
65 |
+
data=[go.Pie(
|
66 |
+
labels=list(iris_data.target_names),
|
67 |
+
values=probabilities[0]
|
68 |
+
)]
|
69 |
+
)
|
70 |
+
fig = fig.update_traces(
|
71 |
+
hoverinfo='label+percent',
|
72 |
+
textinfo='value',
|
73 |
+
textfont_size=15
|
74 |
+
)
|
75 |
+
return fig
|
76 |
+
|
77 |
+
def construct_app(self):
|
78 |
+
|
79 |
+
self.train_data()
|
80 |
+
values = self.construct_sidebar()
|
81 |
+
|
82 |
+
values_to_predict = np.array(values).reshape(1, -1)
|
83 |
+
|
84 |
+
prediction = self.model.predict(values_to_predict)
|
85 |
+
prediction_str = iris_data.target_names[prediction[0]]
|
86 |
+
probabilities = self.model.predict_proba(values_to_predict)
|
87 |
+
|
88 |
+
st.markdown(
|
89 |
+
"""
|
90 |
+
<style>
|
91 |
+
.header-style {
|
92 |
+
font-size:25px;
|
93 |
+
font-family:sans-serif;
|
94 |
+
}
|
95 |
+
</style>
|
96 |
+
""",
|
97 |
+
unsafe_allow_html=True
|
98 |
+
)
|
99 |
+
|
100 |
+
st.markdown(
|
101 |
+
"""
|
102 |
+
<style>
|
103 |
+
.font-style {
|
104 |
+
font-size:20px;
|
105 |
+
font-family:sans-serif;
|
106 |
+
}
|
107 |
+
</style>
|
108 |
+
""",
|
109 |
+
unsafe_allow_html=True
|
110 |
+
)
|
111 |
+
st.markdown(
|
112 |
+
'<p class="header-style"> Iris Data Predictions </p>',
|
113 |
+
unsafe_allow_html=True
|
114 |
+
)
|
115 |
+
|
116 |
+
column_1, column_2 = st.columns(2)
|
117 |
+
column_1.markdown(
|
118 |
+
f'<p class="font-style" >Prediction </p>',
|
119 |
+
unsafe_allow_html=True
|
120 |
+
)
|
121 |
+
column_1.write(f"{prediction_str}")
|
122 |
+
|
123 |
+
column_2.markdown(
|
124 |
+
'<p class="font-style" >Probability </p>',
|
125 |
+
unsafe_allow_html=True
|
126 |
+
)
|
127 |
+
column_2.write(f"{probabilities[0][prediction[0]]}")
|
128 |
+
|
129 |
+
fig = self.plot_pie_chart(probabilities)
|
130 |
+
st.markdown(
|
131 |
+
'<p class="font-style" >Probability Distribution</p>',
|
132 |
+
unsafe_allow_html=True
|
133 |
+
)
|
134 |
+
st.plotly_chart(fig, use_container_width=True)
|
135 |
+
|
136 |
+
return self
|
137 |
+
|
138 |
+
|
139 |
+
sa = StreamlitApp()
|
140 |
+
sa.construct_app()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
streamlit
|
3 |
+
plotly
|
4 |
+
sklearn
|