Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import pandas as pd | |
| import requests | |
| # ======================================================= | |
| # SuperKart Price Prediction (Streamlit Frontend) | |
| # ======================================================= | |
| # Title of the Streamlit web app | |
| st.title("๐ SuperKart Price Prediction") | |
| # ------------------------------------------------------- | |
| # Section 1: Online (Single) Prediction | |
| # ------------------------------------------------------- | |
| st.subheader("๐ฎ Predict Price for a Single Product") | |
| # Collect user input for product features | |
| product_name = st.text_input("Product Name", "Sample Product") # Text input field for product name | |
| brand = st.text_input("Brand", "BrandX") # Text input for brand name | |
| category = st.selectbox("Category", ["Electronics", "Clothing", "Groceries", "Home", "Other"]) | |
| # Dropdown menu to select product category | |
| ratings = st.number_input("Customer Ratings (0 - 5)", min_value=0.0, max_value=5.0, value=4.0, step=0.1) | |
| # Numeric input for product rating | |
| discount = st.number_input("Discount (%)", min_value=0.0, max_value=100.0, value=10.0, step=0.5) | |
| # Numeric input for discount percentage | |
| # Create a DataFrame to format inputs for the backend API | |
| input_data = pd.DataFrame([{ | |
| "product_name": product_name, | |
| "brand": brand, | |
| "category": category, | |
| "ratings": ratings, | |
| "discount": discount | |
| }]) | |
| # When the "Predict Price" button is clicked โ send request to backend | |
| if st.button("Predict Price"): | |
| try: | |
| # Send JSON payload to the Flask backend API hosted on Hugging Face | |
| response = requests.post( | |
| "https://JefferyMendis-SuperKartBackend.hf.space/v1/predict", | |
| json=input_data.to_dict(orient="records")[0] | |
| ) | |
| # If successful โ display prediction | |
| if response.status_code == 200: | |
| prediction = response.json()["Predicted Price (in dollars)"] | |
| st.success(f"๐ฐ Predicted Selling Price: ${prediction:.2f}") | |
| else: | |
| st.error("โ ๏ธ Error making prediction. Please check backend logs.") | |
| except Exception as e: | |
| # Handle connection issues gracefully | |
| st.error(f"โ ๏ธ Connection error: {e}") | |
| # ------------------------------------------------------- | |
| # Section 2: Batch Prediction (CSV Upload) | |
| # ------------------------------------------------------- | |
| st.subheader("๐ Batch Prediction (Upload CSV)") | |
| # File uploader widget for batch prediction | |
| uploaded_file = st.file_uploader("Upload CSV file with product data", type=["csv"]) | |
| # When user uploads a file and clicks button โ send file to backend | |
| if uploaded_file is not None: | |
| if st.button("Predict Batch"): | |
| try: | |
| # Send file to backend API | |
| response = requests.post( | |
| "https://JefferyMendis-SuperKartBackend.hf.space/v1/predictbatch", | |
| files={"file": uploaded_file} | |
| ) | |
| # If successful โ display results | |
| if response.status_code == 200: | |
| predictions = response.json() | |
| st.success("โ Batch predictions completed!") | |
| st.write(predictions) # Display predictions in table format | |
| else: | |
| st.error("โ ๏ธ Error in batch prediction. Check backend.") | |
| except Exception as e: | |
| st.error(f"โ ๏ธ Connection error: {e}") | |