Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- src/streamlit_app.py +63 -34
src/streamlit_app.py
CHANGED
@@ -3,46 +3,75 @@ import pandas as pd
|
|
3 |
import requests
|
4 |
import io
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
st.title("SuperKart Sales Prediction")
|
9 |
-
st.write("Upload
|
10 |
|
11 |
# File uploader
|
12 |
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
13 |
|
14 |
if uploaded_file is not None:
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
if st.button("Get Predictions"):
|
21 |
-
# Prepare the file for API request
|
22 |
-
files = {"file": ("data.csv", uploaded_file.getvalue(), "text/csv")}
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
predictions = response.json()["predictions"]
|
30 |
-
|
31 |
-
# Add predictions to the dataframe
|
32 |
-
df["Predicted_Sales"] = predictions
|
33 |
-
|
34 |
-
st.write("Predictions:")
|
35 |
-
st.dataframe(df)
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import requests
|
4 |
import io
|
5 |
|
6 |
+
# Configure Streamlit page
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="SuperKart Sales Prediction",
|
9 |
+
page_icon="π",
|
10 |
+
layout="wide"
|
11 |
+
)
|
12 |
+
|
13 |
+
# Add custom CSS
|
14 |
+
st.markdown("""
|
15 |
+
<style>
|
16 |
+
.stButton>button {
|
17 |
+
width: 100%;
|
18 |
+
background-color: #4CAF50;
|
19 |
+
color: white;
|
20 |
+
padding: 10px;
|
21 |
+
border: none;
|
22 |
+
border-radius: 4px;
|
23 |
+
cursor: pointer;
|
24 |
+
}
|
25 |
+
.stButton>button:hover {
|
26 |
+
background-color: #45a049;
|
27 |
+
}
|
28 |
+
</style>
|
29 |
+
""", unsafe_allow_html=True)
|
30 |
|
31 |
st.title("SuperKart Sales Prediction")
|
32 |
+
st.write("Upload your CSV file to get sales predictions")
|
33 |
|
34 |
# File uploader
|
35 |
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
36 |
|
37 |
if uploaded_file is not None:
|
38 |
+
try:
|
39 |
+
# Display the uploaded data
|
40 |
+
df = pd.read_csv(uploaded_file)
|
41 |
+
st.write("Preview of uploaded data:")
|
42 |
+
st.dataframe(df.head())
|
|
|
|
|
|
|
43 |
|
44 |
+
# Add a button for predictions
|
45 |
+
if st.button("Get Predictions", key="predict_button"):
|
46 |
+
with st.spinner("Getting predictions..."):
|
47 |
+
# Prepare the file for API request
|
48 |
+
files = {"file": ("data.csv", uploaded_file.getvalue(), "text/csv")}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
try:
|
51 |
+
# Make request to the backend API
|
52 |
+
response = requests.post("ttps://huggingface.co/spaces/abhishek-kumar/superkart_sales_backend/predict", files=files)
|
53 |
+
|
54 |
+
if response.status_code == 200:
|
55 |
+
predictions = response.json()["predictions"]
|
56 |
+
|
57 |
+
# Add predictions to the dataframe
|
58 |
+
df["Predicted_Sales"] = predictions
|
59 |
+
|
60 |
+
st.success("Predictions generated successfully!")
|
61 |
+
st.write("Predictions:")
|
62 |
+
st.dataframe(df)
|
63 |
+
|
64 |
+
# Download button for results
|
65 |
+
csv = df.to_csv(index=False)
|
66 |
+
st.download_button(
|
67 |
+
label="Download predictions",
|
68 |
+
data=csv,
|
69 |
+
file_name="predictions.csv",
|
70 |
+
mime="text/csv"
|
71 |
+
)
|
72 |
+
else:
|
73 |
+
st.error(f"Error getting predictions from the API. Status code: {response.status_code}")
|
74 |
+
except Exception as e:
|
75 |
+
st.error(f"Error: {str(e)}")
|
76 |
+
except Exception as e:
|
77 |
+
st.error(f"Error reading the CSV file: {str(e)}")
|