import streamlit as st import pandas as pd import requests import io # Configure Streamlit page st.set_page_config( page_title="SuperKart Sales Prediction", page_icon="🛒", layout="wide" ) # Add custom CSS st.markdown(""" """, unsafe_allow_html=True) st.title("SuperKart Sales Prediction") st.write("Upload your CSV file to get sales predictions") # File uploader uploaded_file = st.file_uploader("Choose a CSV file", type="csv") if uploaded_file is not None: try: # Display the uploaded data df = pd.read_csv(uploaded_file) st.write("Preview of uploaded data:") st.dataframe(df.head()) # Add a button for predictions if st.button("Get Predictions", key="predict_button"): with st.spinner("Getting predictions..."): # Prepare the file for API request files = {"file": ("data.csv", uploaded_file.getvalue(), "text/csv")} try: # Make request to the backend API backend_url = "https://abhishek-kumar-superkart-sales-backend.hf.space/predict" response = requests.post(backend_url, files=files, timeout=30) if response.status_code == 200: predictions = response.json()["predictions"] # Add predictions to the dataframe df["Predicted_Sales"] = predictions st.success("Predictions generated successfully!") st.write("Predictions:") st.dataframe(df) # Download button for results csv = df.to_csv(index=False) st.download_button( label="Download predictions", data=csv, file_name="predictions.csv", mime="text/csv" ) else: st.error(f"Error getting predictions from the API. Status code: {response.status_code}") st.error(f"Response: {response.text}") except requests.exceptions.Timeout: st.error("Request timed out. Please try again.") except requests.exceptions.RequestException as e: st.error(f"Error connecting to the API: {str(e)}") except Exception as e: st.error(f"Error: {str(e)}") except Exception as e: st.error(f"Error reading the CSV file: {str(e)}")