import streamlit as st import pandas as pd import requests import joblib import json import streamlit as st # The data contains the different attributes of the various products and stores.The detailed data dictionary is given below. # Product_Weight - weight of each product # Product_Sugar_Content - sugar content of each product like low sugar, regular and no sugar # Product_Allocated_Area - ratio of the allocated display area of each product to the total display area of all the products in a store # Product_Type - broad category for each product like meat, snack foods, hard drinks, dairy, canned, soft drinks, health and hygiene, baking goods, bread, breakfast, frozen foods, fruits and vegetables, household, seafood, starchy foods, others # Product_MRP - maximum retail price of each product # Store_Id - unique identifier of each store # Store_Establishment_Year - year in which the store was established # Store_Size - size of the store depending on sq. feet like high, medium and low # Store_Location_City_Type - type of city in which the store is located like Tier 1, Tier 2 and Tier 3. Tier 1 consists of cities where the standard of living is comparatively higher than its Tier 2 and Tier 3 counterparts. # Store_Type - type of store depending on the products that are being sold there like Departmental Store, Supermarket Type 1, Supermarket Type 2 and Food Mart # Product_Store_Sales_Total - total revenue generated by the sale of that particular product in that particular store # Streamlit UI for Customer Churn Prediction st.title("Predict_product_price App") st.write("Predict_product_price_ Enter the required information below.") # Collect user input based on dataset columns Product_Sugar_Content = st.selectbox("Sugar content", ["Low Sugar", "Regular", "No Sugar"]) Product_Type = st.selectbox("Product_Type", ['Frozen Foods', 'Dairy', 'Canned', 'Baking Goods','Health and Hygiene', 'Snack Foods', 'Meat', 'Household', 'Hard Drinks', 'Fruits and Vegetables', 'Breads', 'Soft Drinks','Breakfast', 'Others', 'Starchy Foods', 'Seafood']) Store_Size = st.selectbox("Store Size", ['Medium', 'High', 'Small']) Store_Location_City_Type = st.selectbox("Store Location City Type", ['Tier 2', 'Tier 1', 'Tier 3']) Store_Type = st.selectbox("Type of store", ['Supermarket Type2', 'Departmental Store', 'Supermarket Type1','Food Mart']) Product_Weight = st.number_input("Enter product weight in kg", min_value=0, value=1) Product_Allocated_Area = st.number_input("Product Allocated Area", min_value=0.0, value=0.1) Product_MRP = st.number_input("MRP Price", min_value=0.0, value=10.0) # Convert categorical inputs to match model training input_data = pd.DataFrame([{ 'Product_Weight': Product_Weight, 'Product_Sugar_Content':Product_Sugar_Content, 'Product_Allocated_Area':Product_Allocated_Area, 'Product_Type':Product_Type, 'Product_MRP':Product_MRP, 'Store_Size':Store_Size, 'Store_Location_City_Type':Store_Location_City_Type, 'Store_Type':Store_Type }]) ##api_url = 'https://sga123/backend/predict_price' API_URL = "https://sga123/Backend" # Convert DataFrame to JSON df_json = input_data.to_json(orient="records") if st.button('Predict Price'): try: # Send POST request response = requests.post(f"{API_URL}/price", json=json.loads(df_json), timeout=10) # Check response status if response.status_code == 200: st.success("POST request successful!") st.json(response.json()) # Display JSON response else: st.error(f"Error: {response.status_code}") st.write(response.text) except requests.exceptions.RequestException as e: st.error(f"Request failed: {e}") # How it works: # User Input: The user enters data in Streamlit fields. # Button Click: When clicked, the app sends a POST request to the given API. # requests.post(): Sends JSON data to the endpoint. # Error Handling: Catches network errors and displays them in the UI. # Response Display: Shows the API’s JSON response in a pretty format. # ✅ Tips: # Use json=payload instead of data=payload if your API expects JSON. # Always set a timeout to avoid hanging requests. # For file uploads, use files={"file": open("path", "rb")} in requests.post(). # If you want, I can also give you an example where Streamlit uploads a file and sends it via POST to an API. # That’s common when integrating with ML models or FastAPI backends.