Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +125 -0
- automl.pkl +3 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from autosklearn.regression import AutoSklearnRegressor
|
4 |
+
import base64
|
5 |
+
import json
|
6 |
+
import pickle
|
7 |
+
import uuid
|
8 |
+
import re
|
9 |
+
from io import BytesIO
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
def to_excel(df:pd.DataFrame):
|
13 |
+
output = BytesIO()
|
14 |
+
writer = pd.ExcelWriter(output, engine='xlsxwriter')
|
15 |
+
df.to_excel(writer, index=False)
|
16 |
+
writer.save()
|
17 |
+
processed_data = output.getvalue()
|
18 |
+
return processed_data
|
19 |
+
|
20 |
+
def download_button(object_to_download, download_filename, button_text, file_extension,pickle_it=False):
|
21 |
+
"""
|
22 |
+
Generates a link to download the given object_to_download.
|
23 |
+
|
24 |
+
Params:
|
25 |
+
------
|
26 |
+
object_to_download: The object to be downloaded.
|
27 |
+
download_filename (str): filename and extension of file. e.g. mydata.csv,
|
28 |
+
some_txt_output.txt download_link_text (str): Text to display for download
|
29 |
+
link.
|
30 |
+
button_text (str): Text to display on download button (e.g. 'click here to download file')
|
31 |
+
pickle_it (bool): If True, pickle file.
|
32 |
+
|
33 |
+
Returns:
|
34 |
+
-------
|
35 |
+
(str): the anchor tag to download object_to_download
|
36 |
+
|
37 |
+
Examples:
|
38 |
+
--------
|
39 |
+
download_link(your_df, 'YOUR_DF.csv', 'Click to download data!')
|
40 |
+
download_link(your_str, 'YOUR_STRING.txt', 'Click to download text!')
|
41 |
+
|
42 |
+
"""
|
43 |
+
if pickle_it:
|
44 |
+
try:
|
45 |
+
object_to_download = pickle.dumps(object_to_download)
|
46 |
+
except pickle.PicklingError as e:
|
47 |
+
st.write(e)
|
48 |
+
return None
|
49 |
+
|
50 |
+
else:
|
51 |
+
if isinstance(object_to_download, bytes):
|
52 |
+
pass
|
53 |
+
|
54 |
+
elif isinstance(object_to_download, pd.DataFrame):
|
55 |
+
if file_extension == ".csv":
|
56 |
+
object_to_download = object_to_download.to_csv(index=False)
|
57 |
+
else:
|
58 |
+
object_to_download = to_excel(object_to_download)
|
59 |
+
# Try JSON encode for everything else
|
60 |
+
else:
|
61 |
+
object_to_download = json.dumps(object_to_download)
|
62 |
+
|
63 |
+
try:
|
64 |
+
# some strings <-> bytes conversions necessary here
|
65 |
+
b64 = base64.b64encode(object_to_download.encode()).decode()
|
66 |
+
|
67 |
+
except AttributeError as e:
|
68 |
+
b64 = base64.b64encode(object_to_download).decode()
|
69 |
+
|
70 |
+
button_uuid = str(uuid.uuid4()).replace('-', '')
|
71 |
+
button_id = re.sub('\d+', '', button_uuid)
|
72 |
+
|
73 |
+
custom_css = f"""
|
74 |
+
<style>
|
75 |
+
#{button_id} {{
|
76 |
+
display: inline-flex;
|
77 |
+
align-items: center;
|
78 |
+
justify-content: center;
|
79 |
+
background-color: rgb(255, 255, 255);
|
80 |
+
color: rgb(38, 39, 48);
|
81 |
+
padding: .5rem .75rem;
|
82 |
+
position: relative;
|
83 |
+
text-decoration: none;
|
84 |
+
border-radius: 4px;
|
85 |
+
border-width: 1px;
|
86 |
+
border-style: solid;
|
87 |
+
border-color: rgb(230, 234, 241);
|
88 |
+
border-image: initial;
|
89 |
+
}}
|
90 |
+
#{button_id}:hover {{
|
91 |
+
border-color: rgb(246, 51, 102);
|
92 |
+
color: rgb(246, 51, 102);
|
93 |
+
}}
|
94 |
+
#{button_id}:active {{
|
95 |
+
box-shadow: none;
|
96 |
+
background-color: rgb(246, 51, 102);
|
97 |
+
color: white;
|
98 |
+
}}
|
99 |
+
</style> """
|
100 |
+
|
101 |
+
dl_link = custom_css + f'<a download="{download_filename+file_extension}" id="{button_id}" href="data:file/txt;base64,{b64}">{button_text}</a><br></br>'
|
102 |
+
#dl_link = custom_css + f'<a download="{download_filename+file_extension}" id="{button_id}" data:application/octet-stream;base64,{b64}">{button_text}</a><br></br>'
|
103 |
+
|
104 |
+
return dl_link
|
105 |
+
|
106 |
+
|
107 |
+
file_upload = st.file_uploader("Upload a csv file", type="csv")
|
108 |
+
if file_upload is not None:
|
109 |
+
data = pd.read_csv(file_upload)
|
110 |
+
column = data["S11"].iloc[1:].values
|
111 |
+
with open("automl.pkl", "rb") as f:
|
112 |
+
model = pickle.load(f)
|
113 |
+
pred_clip = model.predict([column])
|
114 |
+
pred_clip = np.clip(pred_clip, [0.2,0.4,3.9,0.2,13.9,13.8,13.2],[1.01,1.21,4.71,0.8,14.701,14.201,14.001])
|
115 |
+
predictions = pd.DataFrame(pred_clip.tolist(), columns = ["w1","w2","w3","s1","l1","l2","l3"])
|
116 |
+
|
117 |
+
is_download = st.checkbox("Download predictions", value=False)
|
118 |
+
if is_download:
|
119 |
+
href = download_button(predictions, "predictions", "Download", ".csv")
|
120 |
+
st.markdown(href, unsafe_allow_html=True)
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
automl.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8804c4c6a5f5877ad53a98764007086727157d0bf7d1b85e284643b9ae9cd88e
|
3 |
+
size 157995814
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
auto-sklearn
|